【SpringMVC】| View of SpringMVC

Table of contents

Views of Spring MVC

1. ThymeleafView

2. Forward view

3. Redirect view

4. View controller view-controller


Views of Spring MVC

(1) The view in SpringMVC is the View interface. The function of the view is to render data and display the data in the model to the user.

(2) There are many types of SpringMVC views. By default, there are forward views (forward) and redirect views (redirect).

(3) If the project introduces the dependency of jstl (jstl tag library), the forwarding view will be automatically converted to JstlView.

(4) If the view technology used is Thymeleaf, the Thymeleaf view resolver is configured in the SpringMVC configuration file, and the ThymeleafView is obtained after the view resolver resolves.

1. ThymeleafView

When the view name set in the controller method does not have any prefix (prefixes are forward: and redirct:) , the view name at this time will be parsed by the view resolver configured in the SpringMVC configuration file , the view name The final path obtained by splicing the view prefix and view suffix will realize the jump by forwarding

In fact, this is the way I have been using before!

view resolver

Send the request and splice the prefix and suffix in the view resolver 

2. Forward view

(1) The default forwarding view in SpringMVC is InternalResourceView.

(2) The case of creating a forwarding view in SpringMVC: When the view name set in the controller method is prefixed with " forward: ", the InternalResourceView view is created, and the view name at this time will not be the view configured in the SpringMVC configuration file The parser parses , but removes the prefix "forward:", and the remaining part is used as the final path to realize the jump by forwarding!

For example: we jump from two to one, no view resolver is needed at this time

At this time, forward: will be adjusted to jump directly to the /one path, which will not be parsed by the view parser, and the prefix and suffix will not be added!

Note: At this time, the ThymeleafView view is not created, but the InternalResourceView!

Note: If jsp is used, then InternalResourceView is configured in the view parser (this is the same without any prefix), and there is no ThymeleafView!

    @RequestMapping("/one")
    public String one(){
        return "success";
    }
    @RequestMapping("/two")
    public String two(){
        return "forward:/one";
    }

3. Redirect view

(1) The default redirection view in SpringMVC is RedirectView.

(2) When the view name set in the controller method is prefixed with " redirect: ", create a RedirectView view. At this time, the view name will not be parsed by the view resolver configured in the SpringMVC configuration file, but will be The prefix "redirect:" is removed, and the remaining part is used as the final path to realize the jump through redirection.

Review: Forwarding and Redirecting

①Forwarding is actually a request (relative to the browser) , the first time the browser sends the request, and the second time it happens inside the server; so the address bar is still the address where the first request was made . Redirection is two requests . The first visit is to the servlet, and the second visit is to visit our redirected address; the address bar is the redirected address .

②Forwarding can carry data , because it is a request, and the same request object is called. Redirection is not possible , and data will be lost, because there are two requests, corresponding to two different request objects.

Forwarding can access resources under WEB-INF . Redirection cannot access resources under WEB-INF .

Forwarding cannot cross domains. Redirects can cross domains .

Compare forwarding and redirecting address bar changes

    @RequestMapping("/one")
    public String one(){
        return "success";
    }
    @RequestMapping("/two")
    public String two(){
        return "forward:/one";
    }

    @RequestMapping("/three")
    public String three(){
        return "redirect:/one";
    }

 Use forwarding: /two forwards to /one (the address bar should be two), and then jumps to success after view analysis

Use redirection: /three redirects to /one (the address bar should be one), and then jumps to success after view resolution

4. View controller view-controller

When the controller method is only used to realize page jumping , that is, when only the view name needs to be set, the processor method can be represented by the view-controller tag of the mvc namespace !

Add the following configuration in springmvc.xml

①path: Set the request address for processing;
②view-name: Set the view name corresponding to the request address.

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

The above code is equivalent to:

    @RequestMapping("/")
    public String demo(){
        return "index";
    }

But there will be a new problem at this time: when any view-controller is set in SpringMVC, the request mapping in other controllers will all be invalid! At this point, you need to set the tag to enable the mvc annotation driver in the core configuration file of SpringMVC :

<mvc:view-controller path="/" view-name="index"></mvc:view-controller>
<!--开启mvc的注解驱动-->
<mvc:annotation-driven/>

Summarize:

(1) For html , the Thymeleaf view parser is generally configured , because java code cannot be used for html; for example, when we write a path to send a request, we need to use the namespace of thymeleaf to process it!

Note: At this time, the context path name of the project will be added in front of the one path according to thymeleaf!

<a th:href="@{/one}"></a>

(2) For jsp , the general configuration is the InternalResourceViewResolver view resolver , because java code can be used in jsp, we can dynamically obtain the path name of the context through java code!

Note: At this time, use pageContext.request.contextPath to dynamically obtain the path name of the context!

<a href="${pageContext.request.contextPath}/one"></a>

Guess you like

Origin blog.csdn.net/m0_61933976/article/details/130917385