Mastering Spring 5.0
上QQ阅读APP看书,第一时间看更新

View resolver

A view resolver resolves a View name to the actual JSP page.

The View name in this example is welcome, and we would want it to resolve to /WEB-INF/views/welcome.jsp.

A view resolver can be configured in the spring context /WEB-INF/user-web-context.xml. Here's the code snippet for that:

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

A few important points to note:

  • org.springframework.web.servlet.view.InternalResourceViewResolver: A view resolver supporting JSPs. JstlView is typically used. It also supports tiles with a TilesView.
  • <property name="prefix"> <value>/WEB-INF/views/</value> </property><property name="suffix"> <value>.jsp</value> </property>: This maps the prefix and suffix to be used by view resolver. View resolver takes the string from the controller method and resolves to the view: prefix + viewname + suffix. So, the view name welcome is resolved to /WEB-INF/views/welcome.jsp.

Here is a screenshot of how this would look on the screen when the URL is hit: