For some controller processing methods, when the return value is of the string type, the returned results may contain the forward or redirect prefix;
For example:
1 @Controller 2 @RequestMapping("/user") 3 public class UserController { 4 @RequestMapping("/forward") 5 public String replyWithForward(HttpServletRequest request, String userId){ 6 request.setAttribute("userid", userId); 7 8 System.out.println("userId =" +userId); 9 10 return "forward:hello";11 }12 13 @RequestMapping("/redirect")14 public String replyWithRedirect(HttpServletRequest request, String userId){15 request.setAttribute("userid", userId);16 17 System.out.println("userId = "+userId);18 19 return "redirect:hello";20 }21 }
Test page hello. jsp:
1 <% @ page Language = "Java" contenttype = "text/html; charset = UTF-8" 2 pageencoding = "UTF-8" %> 3 <% 4 string userid = (string) request. getattribute ("userid"); 5 system. out. println ("the obtained userid is:" + userid); 6%> 7 <! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en" http://www.w3.org/TR/html4/loose.dtd "> 8 <HTML> 9
When the path is http: // localhost: 8080/crazysnilweb/user/forward? When userid = 123, the URL in the browser does not change and the page output is:
When the path is http: // localhost: 8080/crazysnilweb/user/redirect? When userid = 123, the URL in the browser changes to http: // localhost: 8080/crazysnilweb/user/Hello, And the page output is as follows:
Note: Redirect will allow the browser to initiate a new request, so the parameters in the original request object are lost, while the target address of forward is in the current request, and the parameters in the request will not be lost;