SpringMVC (seven)_view and view resolver

         Foreword: This article mainly introduces how SpringMVC parses views and implements view parsing through a custom view parser.

 

1. The process of SpringMVC parsing view

Analyzing the above process, the following three main points         can be concluded :

  • After the request processing method is executed, a ModelAndView object is finally returned . For those processing methods that return String, View or ModeMap, Spring MVC also internally assembles them into a ModelAndView object, which contains the logical name and the view of the model object;
  • Spring MVC obtains the final view object (View) with the help of the view resolver (ViewResolver), and the final view can be a JSP, or a view in various forms such as Excel and JFreeChart;
  • The processor does not care about which view object is finally used to render the model data, and the processor focuses on the work of producing the model data, so as to realize the full decoupling of MVC;

 2. Brief view

       The role of the view is to render the model data and present the data in the model to the client in some form. In order to realize the decoupling of the view model and the specific implementation technology, Spring defines a highly abstract View interface in the org.springframework.web.servlet package:

       View objects are instantiated by the view resolver. Since views are stateless, they do not have thread safety issues. Commonly used views use:

 

3. Briefly describe the view parser

        SpringMVC provides different strategies for parsing logical view names. You can configure one or more parsing strategies in the Spring WEB context and specify the order between them. Each mapping strategy corresponds to a specific view resolver implementation class. The role of the view resolver is relatively simple: to resolve the logical view into a specific view object, all view resolvers must implement the ViewResolver interface:

        Common view resolver implementation classes:

          A few notes on the above view resolver implementation classes:

  • Programmers can choose one view resolver or mix multiple view resolvers;
  • Each view parser implements the Ordered interface and exposes an order property, which can be used to specify the parser's priority order. The smaller the order, the higher the priority ;
  • SpringMVC will parse the logical view name in the priority order of the view resolver, until the parsing is successful and the view object is returned, otherwise a ServletException will be thrown.

4. Basic configuration

        The relevant demo is output below to realize forwarding, redirection, and combined use of view resolvers. First look at the relevant configuration in the SpringMVC configuration file:

<!-- Define the view parser (here for Jsp) -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <!-- Prefix for all URIs. -->
    <property name="prefix" value="/views/"></property>
    <!-- Suffix for all URIs. -->
    <property name="suffix" value=".jsp"></property>
</bean>

<!-- Configure the view BeanNameViewResolver resolver: use the name of the view to resolve the view -->
<!-- The priority of the view parser is defined by the order attribute, the smaller the order value, the higher the priority -->
<bean class="org.springframework.web.servlet.view.BeanNameViewResolver">
    <property name="order" value="100"></property>
</bean>

 

5. Test forwarding

       In general, controller methods return values ​​of type string that are treated as logical view names.

Front desk code:

<a href="handleView/testViewAndViewResolver.action">Test forwarding</a>

 Background code:

@RequestMapping("/handleView/testViewAndViewResolver.action")
public String testViewAndViewResolver(){
    System.out.println("[Forward] test forwarding");
    return SUCCESS;
}

 

6. Test redirection

      redirect:success.jsp: A redirection to success.jsp will be done.

Front desk code:

<a href="handleView/testRedirect.action">Test Redirect</a>

 Background code:

@RequestMapping("/handleView/testRedirect.action")
public String testRedirect(){
    System.out.println("[Redirect] Test redirection");
    return "redirect:/login.jsp";
}

 

6. Combining view resolvers

       As shown in the configuration in the above SpringMVC configuration file, the InternalResourceViewResolver view resolver is used in conjunction with the BeanNameViewResolver view resolver. The default priority of the view resolver is Integer.MAX_VALUE , so in the above configuration, the BeanNameViewResolver has a higher priority, and only searches by name After the view is not available, the JSP will be found.

  Custom View:

@Component
public class HelloView implements View {

    @Override
    public String getContentType() {
        return "text/html";
    }

    @Override
    public void render(Map<String, ?> model, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        response.getWriter().print("hello view, time: " + new Date());
    }
}

  Front desk code:

<a href="handleView/testView.action">Test configuration view resolver</a>

 Backend controller code:

@RequestMapping("/handleView/testView.action")
public String testView(){
    System.out.println("[BeanNameViewResolver] Test configuration view resolver");
    return "helloView";
}

 The test results are shown in the figure below. It can be found that when the BeanNameViewResolver can find the view by name, the BeanNameViewResolver is used to resolve the view.



Code download source: http://super-wangj.iteye.com/blog/2388430

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326341607&siteId=291194637