Spring mvc's Internal ResourceViewResolver configuration

Keywords: JSP Spring Java

1. View Resolution Internal ResourceViewResolver


The role of the InternalResourceViewResolver class:

Internal ResourceViewResolver resolves the view name into a JSP file. In addition, if you use the JSP Standard Tag Library (JSTL) in your JSP pages, the Internal ResourceViewResolver can parse the view name into JSP files in the form of JstlView, thereby exposing JSTL localization and resource bundle variables to JSTL formatting and letters. message label.
Spring provides two JSP tag libraries, one for form-to-model binding and the other for generic tool class features.
Consider a simple scenario, assuming that the logical view is named home. The general practice is to place JSP files in the WEB-INF directory of Web applications to prevent direct access to them. If we place all JSP files in the'/ WEB-INF/views/'directory and the JSP name of the home page is home.jsp, we can determine that the path of the physical view is the logical view name home plus the'/ WEB-INF/views/' prefix and'.jsp'suffix. As shown in the following figure:

2. Parameters

prefix is an example of a directory:

<property name="prefix" value="/WEB-INF/JSP/"/>

Suffix is an example of suffix:

<property name="suffix" value=".jsp"/>

The order represents the execution order of the view parser and executes instances in the order of values from small to large:

<property name="order" value="1"/>

3. View parsing in single format (jsp)

<bean id="viewResolver"
   <property name="prefix" value="/WEB-INF/page/" />
   <property name="suffix" value=".jsp" />
   <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />

4. Multi-format View Resolution (jsp)

<!-- To configure Html Trying to parse -->
<bean id="htmlViewResolver"
   <property name="order" value="0" />
   <property name="viewClass"
           value="com.jack.common.spring.HtmlResourceView" />
   <property name="prefix" value="/WEB-INF/html/" />
   <property name="suffix" value=".html"></property>
   <property name="contentType" value="text/html;charset=UTF-8"></property>

<!-- Jsp view resolver -->
<bean id="jspViewResolver"
   <property name="order" value="1" />
   <!-- If the configuration has html View parser, not available jstl That one -->
   <!-- <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> -->
   <property name="viewClass" value="org.springframework.web.servlet.view.InternalResourceView" />
   <property name="prefix" value="/WEB-INF/page/" />
   <property name="suffix" value=".jsp"></property>

You can see that the viewClass value of the html ViewResolver view parser is somewhat strange because we need to create a new html parser that inherits the Internal ResourceView and rewrites the checkResource method, code:

import org.springframework.web.servlet.view.InternalResourceView;
import java.io.File;
import java.util.Locale;

public class HtmlResourceView extends InternalResourceView {
    public boolean checkResource(Locale locale) throws Exception {
        File file = new File(this.getServletContext().getRealPath("/")+getUrl());
        return file.exists();

Because of the loadView method of UrlBasedViewResolver (parent class of InternalResourceViewResolver), the code:

protected View loadView(String viewName, Locale locale) throws Exception {
    AbstractUrlBasedView view = this.buildView(viewName);
    View result = this.applyLifecycleMethods(viewName, view);
    return view.checkResource(locale)?result:null;

public boolean checkResource(Locale locale) throws Exception {
	 return true;

To find this method, AbstractUrlBasedView.checkResource() always returns true, that is, if it cannot be found, it will return a result, but the result is not found.

To do this, you need to override the AbstractUrlBasedView class and change the checkResource method.

Posted by hughesa on Fri, 06 Sep 2019 22:44:06 -0700