spring controller default forward, forward forward, the difference between the redirect forwards

  1. Default Forwarding
@RequestMapping("/123")
    public String test(HttpSession session) {
        System.out.println("123");
        return "456";
}

After the call server obtained test method request / 123, 123 and then output to find 456.jsp (this view has been configured, the full path /WEB-INF/jsp/456.jsp) file

  1. forward forwarding
@RequestMapping("/123")
    public String test(HttpSession session) {
        System.out.println("123");
        return "forward:456";
}

@RequestMapping("/456")
    public String test1(HttpSession session) {
        System.out.println("456");
        return "456";
}

After the call server requesting for the test method / 123, the output 123, but the default forwardingdifferentIt is, forward forward will continue to look for parsing / 456 rather than looking 456.jsp files,
404 errors if test1 method does not exist will be reported, even if the file exists 456.jsp

  1. redirect forwards
@RequestMapping("/123")
    public String test(HttpSession session, HttpServletRequest hr) {
        System.out.println("123");
        hr.setAttribute("test", "123");
        session.setAttribute("session", 123);
        return "redirect:456";
    }

@RequestMapping("/456")
public String test1(HttpSession session, HttpServletRequest hr) {
    System.out.println("456");
    System.out.println(hr.getAttribute("123"));
    System.out.println(session.getAttribute("session"));
    return "456";
}

Similar effect and forward forward, but the browser's address will be changed to the end of / 456, as well as original content after the redirect request will be lost, the contents of the session will not be lost, but will not forward

Guess you like

Origin www.cnblogs.com/d-i-p/p/11001806.html