Spring MVC view resolver

Posted by g_p_java on Tue, 21 Dec 2021 15:45:49 +0100

1, Overview

View resolver is an important part of Spring MVC, which is responsible for resolving logical view names to specific view objects.

1.1,URLBasedViewResolver

UrlBasedViewResolver is a simple implementation of ViewResolver. It provides a way to splice URL s to parse views.

The UrlBasedViewResolver specifies a prefix through the prefix property and a suffix through the suffix property. When the ModelAndView object returns a specific View name, it will splice the prefix prefix and suffix suffix with the View name to get a specific loading path of the View resource file and feed back to the user.

<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.InternalResourceViewResolver"/><!--Cannot be omitted-->
    <!--prefix-->
    <property name="prefix" value="/WEB-INF/jsp/" />
    <!--suffix-->
    <property name="suffix" value=".jsp" />
</bean>

The above view parser is configured with prefix and suffix attributes, which shortens the view path. The control class only needs to provide register and login to return the view path. The view parser will automatically add prefix and suffix and resolve them to / WEB-INF / JSP / register JSP and / WEB-INF / JSP / login jsp.

The above viewClass value is InternalResourceViewResolver, which is used to display JSP pages. If you need to use the jstl tag to display data, you can specify the viewClass attribute value as jstview.

In addition, the contents stored in the / WEB-INF / directory cannot be obtained directly through the request. Therefore, for security reasons, jsp files are usually placed in the WEB-INF directory.

1.2,InternalResourceViewResolver

Internal resource view parser is the most commonly used view parser type in daily development. It is a subclass of URLBasedViewResolver and has all the features of URLBasedViewResolver.

InternalResourceViewResolver can automatically resolve the returned view name to an object of type InternalResourceView. InternalResourceView will store the model attributes returned by the Controller processor method in the corresponding request attribute, and then redirect the request forword to the target URL on the server side through the RequestDispatcher. When using InternalResourceViewResolver view resolution, there is no need to specify the viewClass attribute separately

<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
    <!--prefix-->
    <property name="prefix" value="/WEB-INF/jsp/" />
    <!--suffix-->
    <property name="suffix" value=".jsp" />
</bean>

1.3,FreeMarkerViewResolver

FreeMarkerViewResolver is a subclass of UrlBasedViewResolver. You can specify a prefix through the prefix attribute and a suffix through the suffix attribute.

FreeMarkerViewResolver will eventually parse the logical view configuration and return to the feemaker template. You do not need to specify the viewClass attribute.

<bean class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
    <property name="prefix" value="fm_"/>
    <property name="suffix" value=".ftl" />
</bean>

<bean class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
    <property name="templateLoaderPath" value="/WEB-INF/ftl" />
</bean>
@Controller
@RequestMapping("viewtest")
public class TestController { 
    @RequestMapping("freemarker")
    public ModelAndView freemarker(){
        ModelAndView mv=new ModelAndView();
        mv.addObject("username","JSONLiu");
        mv.setViewName("freemarker");
        return mv;
    } 
}

Topics: Java Spring MVC mvc