Spring MVC learning summary (5): request forwarding and redirection

 In Web development, resource redirection is divided into two ways: request forwarding and redirection. Spring MVC uses forward and redirect keywords to process at the controller layer.

1. Request forwarding and redirection

(1) Request forwarding

When the client sends an HTTP request, the Web server receives the request and calls an internal method to complete the request processing and forwarding actions inside the container, and then sends the target resource to the client. It should be noted that the forwarding path must be a URL under the same web container, and it cannot be redirected to other web paths. At this time, the address bar of the client browser displays the path of the first visit, which means that the client cannot detect the server's forwarding operation. Request forwarding is when the browser only makes one request.

(2) Redirect

When the client sends an http request, the Web server sends a 302 status code response after receiving the request, and sends the new location to the client. When the client finds that it is a 302 response, it automatically sends a new http request. The requested URL is the new location address. The server searches for resources according to this request and sends them to the client.

The location here can be redirected to any URL. Because the browser reissues a new request, the redirected path is displayed in the address bar of the client browser, and the client can observe the address change. The redirect behavior is that the browser makes at least two visit requests.

(3) Difference

The same point: Both are the way of resource jump in web development.

Difference:
Request forwarding is a jump inside the server, and the address bar of the browser will not change. The jump from one page to another is still the same request, that is, there is only one request response. The object can be passed through the request field.
Redirection: The browser automatically initiates a request to the redirect target, and the address bar of the browser will change. Jumping from one page to another page is a different request, that is, a response to two or more different requests. The object cannot be passed through the request field.

  Request forwarding Redirect
Different ways to jump Jump inside the server The browser automatically initiates the request for the jump target
Whether the address bar has changed The address bar does not change The address bar changes
Number of requests Resource jump within a request There are two or more different requests
Whether the request field can pass objects Yes no

2. Request forwarding and redirection in Spring MVC

 In the Spring MVC framework, the return statement of the processing method in the controller class is forwarded by default, but it is forwarded to the view. Request forwarding and redirection can be realized by adding forward and redirect keywords in the return statement.

(1) Request forwarding and redirection in traditional Servlet

Request forwarding:

requst.getRequestDispatcher("页面").forword(request,response);

Redirect: 

response.sendRedirect("页面");

 (2) Request forwarding and redirection in Spring MVC

Use as follows:

    @RequestMapping("forwardHandler01")
    public String forwardHandler01(){
        return "forward:/success.jsp";
    }
    @RequestMapping("/forwardHandler02")
    public String forwardHandler02(){
        return "forward:/forwardHandler01";
    }
    @RequestMapping("/redirectHandler01")
    public String redirectHandler01(){
        return "redirect:/success.jsp";
    }

    @RequestMapping(value="/redirectHandler02")
    public String redirectHandler02(){
        return "redirect:/redirectHandler01";
    }

important point:

a. For the page requested to be forwarded, it can be a page in WEB-INF; for a redirected page, it cannot be a page in WEB-INF. Because redirection is equivalent to the user making a request again, and the user cannot directly access the resources in WEB-INF.

b./success.jsp represents the start from the current project, Spring MVC will automatically splice the project name for the path

c.forward and redirect can perform resource redirection and link redirection.

3. The use of mvc:view-controller

Usage scenario: Do ​​not do any processing for a request, just return a view. If the controller method of our front-end page request mapping does not require any business logic processing, but only returns a view object, you can use mvc:view-controller to simplify the code, for example:

    @RequestMapping(value = "/toSuccess")
    public String toSuccess(){
        return "success";
    }

Then you can do the following configuration in springmvc.xml to replace the above code:

    <mvc:view-controller path="/toSuccess" view-name="success"></mvc:view-controller>

Parameter Description:

path="/toSuccess" is the access path (equivalent to RequestMapping("/toSuccess"))

view-name=”success” is the name of the target page to jump to (such as success.jsp, which is equivalent to return “success”) After configuring this, the /success request will be directly handed over to dispatcherServlet and then resolved using ViewResolver .

Note: If mvc:view-controller is used in the configuration, Requestmapping will fail. If you want to coexist, you need to add mvc:annotation-driven , as follows:

    <mvc:annotation-driven></mvc:annotation-driven>

 

Guess you like

Origin blog.csdn.net/weixin_47382783/article/details/113511522