3--springMVC中redirect重定向和forward转发的区别---一些简单的问题的解决

https://www.cnblogs.com/conswin/p/6973528.html

SpringMvc请求方式分为转发、重定向两种,是用forward和redirect关键字在controller层进行处理。

  下面代码实现了这两种不同的请求方式:

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

@Controller
public class HelloController {
    
    /**
     * 转发形式
     * @param name
     * @param model
     * @return
     */
    @RequestMapping("/helloForward")
    public String helloForward(@RequestParam(value="name", required=false, defaultValue="World2017") String name, Model model) {
        model.addAttribute("name", name);
        return "hello";
    }
    
    
    
    @RequestMapping("/hello")
    public String hello() {        
        return "hello";       
    }
    
    /**
     * 使用RedirectAttributes类
     * @param name
     * @param redirectAttributes
     * @return
     */
    @RequestMapping("/helloRedirect")
    public String helloRedirect(@RequestParam(value="name", required=false ) String name, RedirectAttributes redirectAttributes) {
       
        redirectAttributes.addFlashAttribute("name", name);
        
        return "redirect:/hello";
    }
    
    
    @RequestMapping("/hello2")
    public String hello2(Model model,HttpServletRequest request) {
        
        HttpSession session = request.getSession();
        model.addAttribute("name",session.getAttribute("name"));
        return "hello";      
    }
    
    /**
     * 常规做法,重定向之前把参数放进Session中,在重定向之后的controller中把参数从Session中取出并放进ModelAndView
     * @param name
     * @param request
     * @return
     */
    @RequestMapping("/helloRedirect2")
    public String helloRedirect2(@RequestParam(value="name", required=false ) String name, HttpServletRequest request) {
       
        request.getSession().setAttribute("name", name);
        
        return "redirect:/hello2";
    }
    
    
}
View Code

在使用redirect进行重定向时请求的URL链接发生了改变,并且在controller中如果像reward一样 model.addAttribute("name", name)放置的参数,重定向之后是无法获取到的,所以重定向需要另外的方式进行参数的传递,上面的程序介绍了两种重定向传参的方式:

  ①、重定向之前把参数放进Session中,在重定向之后的controller中把参数从Session中取出并放进ModelAndView

  ②、使用RedirectAttributes类,这种实现方式比较简单。

  再总结一下servlet中转发request.getRequestDispatcher().forward()和重定向response.sendRedirect()的区别:

  ①、转发是一次请求,一次响应,而重定向是两次请求,两次响应

  ②、转发:servlet和jsp共享一个request,重定向:两次请求request独立,所以前面request里面setAttribute()的任何东西,在后面的request里面都获取不到

  ③、转发:地址栏不会改变,重定向:地址栏发生变化。

SpringMVC中的重定向数据传递

https://blog.csdn.net/qq_38449518/article/details/82560347

三、但是!重定向之后客户端用新请求去访问主界面,这样的话刚刚塞到ModelAndView那些属性不就凉了么。

        在这里,我们可以使用上图的:RedirectAttribute类来保存之前获得的属性,这样就可以避免重定向之后数据丢失的问题。

        就是说:原来我们是向ModelAndView中放属性的,但是现在我们只要往这个类里放就OK了,就可以让用户在主界面看到之前想保存的那一堆属性了。

方法一:使用RequestContextUtils.getInputFlashMap()来直接获取FlashMap(重点)

        我们在Index-Controller中,把数据使用RedirectAttribute.addFlashAttribute()保存在FlashMap中,在重定向的时候,FlashMap中的数据被添加到了Request中,所以使用这个方法可以从Request中获取到FlashMap。

        FlashMap本质上还是一个Map,所以这个方法的返回值是Map<String,?>,我们只要用一个Map来接收就可以了,然后我们就可以提取我们之前存储下的那些属性了。(就是可以直接在视图上使用 ${属性名} 来获取)。

方法二、通过(@ModelAttribute(value="属性名")String xxx)通过属性名称获取属性值(推荐)

        刚刚已经说过了,一开始储存的数据在重定向的时候都放在Request里了,这些数据被作为初始化的模型(通俗的来讲就是这些数据被这个控制器的所有方法共享,即为共享数据),所以通过@ModelAttribute可以在Main-Controller中的任意一个方法里通过属性名获取这些属性。比上面的那个要简单多了,所以推荐使用这个方法。

原文地址:https://www.cnblogs.com/woainixxx/p/11127194.html