3 - springMVC the redirect redirect and forward forwarded difference --- solve some simple problems

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

SpringMvc divided into forwarding request, redirect two types are processed in the controller forward and redirect keywords layer.

  The following code implements two different request:

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";       
    
    @return
     *@param
     *name@param
     *
     * class used RedirectAttributes/ **
    }redirectAttributes
     */
    @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" ;       
    } 
    
    / ** 
     * conventional practice, before redirecting the parameters into the Session, the parameters taken out and put in from the Session controller after redirection ModelAndView 
     * @param name 
     * @param Request 
     * @return 
     * / 
    @RequestMapping ( "/ helloRedirect2" )
     public String helloRedirect2 (@RequestParam (value = "name", = required to false ) String name, the HttpServletRequest Request) { 
       
        Request.getSession () the setAttribute (. "name" , name); 
        
        return "the redirect: / of hello2" ; 
    } 
    
    
}
View Code

URL link request when using redirect redirect changed, and if the controller like a reward as model.addAttribute ( "name", name) Parameters placed, after the redirect can not get to, so the need to redirect otherwise passed parameters, the above procedure describes two ways to redirect parameter passing:

  ①, before redirecting the parameters into the Session, the parameter is removed from the controller after the Session and redirect into ModelAndView

  ②, use RedirectAttributes class, this implementation is relatively simple.

 

 

  To sum up the servlet forwards request.getRequestDispatcher () the difference between forward () and redirect response.sendRedirect () is:

  ①, is a forward request, a response, two requests are redirected twice response

  ②, forward: servlet and jsp share a request, redirection: request request two independent, so the front inside request setAttribute () anything in the back of the request which are not acquired

  ③, forward: the address bar does not change, redirection: the address bar changes.

 

 

Redirection of data transfer SpringMVC

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

Third, however! After redirect client with a new request to visit the main interface, so just stuffed ModelAndView those properties will not cool yet.

        Here, we can use the map: RedirectAttribute class to hold property acquired before, so you can avoid data loss after the redirect issue.

        That is to say: We are the original property to ModelAndView put in, but now we just put into this class is OK, you can let the user see the property before you want to save a bunch in the main interface.

Method One: Use RequestContextUtils.getInputFlashMap () directly to obtain FlashMap (focus)

        We Index-Controller, to move data using RedirectAttribute.addFlashAttribute () is stored in FlashMap, the redirection of time, the data FlashMap been added to the Request, so use this method can be obtained from the Request to FlashMap.

        On FlashMap still essentially a Map, so this method's return value is a Map <String,?>, We'll just use a Map to receive it, then we can extract those attributes in our store before the. (That is, can be used directly in the view $ {} to obtain the property name).

The second method to obtain the property value (recommended) by (@ModelAttribute (value = "attribute name") String xxx) by property name

        Has just been said, at the beginning of the data stored in the redirect when they are placed in the Request, and these data are used as a model initialization (colloquial terms that these data are shared by all controllers of this method, that is shared data), it is possible to obtain these properties @ModelAttribute method of any one of the Main-Controller's property by name. Above that is simpler than the more, it is recommended to use this method.

Guess you like

Origin www.cnblogs.com/woainixxx/p/11127194.html