Spring MVC - View

Spring MVC View Resolver

View resolver resolve a view name to its real implmentation

Map a Spring MVC View using prefix or suffix

Map a view name to a JSP file of the same name in a specific directory

Edit

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

or

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

It maps to a JSP file of the same name in the directory set in the "prefix" parameter

Example Map to
home /WEB-INF/jsp/home.jsp
account/add /WEB-INF/jsp/account/add.jsp

Map a Spring MVC View Using ResourceBundle

Create a resource bundle view resolver

<!-- the ResourceBundleViewResolver -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.ResourceBundleViewResolver">
  <property name="basename" value="views"/>
</bean>

Create WEB-INF/classes/views.properties

# If a method return a Class Obj1, the view resolver will use
obj1.class=org.springframework.web.servlet.view.JstlView
obj1.url=/WEB-INF/jsp/page1.jsp

obj2.class=org.springframework.web.servlet.view.JstlView
obj2.url=/WEB-INF/jsp/page2.jsp

Use Tile for Spring MVC views

Add Tile Definition & Tile View Resolver

Add to webmvc-config.xml

<!-- Configures Tiles layout -->
<bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
    <!-- Location of all tile definitions and layout -->
    <property name="definitions">
	<list>
	    <value>/WEB-INF/layouts/layouts.xml</value>
	    <value>/WEB-INF/jsp/views.xml</value>
	</list>
    </property>

    <!-- Resolves views by delegating to the Tiles layout system -->
    <!-- Map view name to tile definition with the same name-->
    <bean id="tilesViewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.tiles2.TilesView"/>
    </bean>

</bean>
  • Tile definition and layout file location
  • Map the view name to the tile definition with the same name

Define a Tile layout /WEB-INF/layouts/layouts.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE tiles-definitions PUBLIC
       "-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN"
       "http://tiles.apache.org/dtds/tiles-config_2_0.dtd">

<tiles-definitions>
	
    <definition name="standardLayout" template="/WEB-INF/layouts/standard.jsp" />
	
</tiles-definitions>

Define a Tile template /WEB-INF/layouts/standard.jsp

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="tiles" uri="http://tiles.apache.org/tags-tiles" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
	<title>Spring Application</title>
</head>
<body>
	<tiles:insertAttribute name="body" />
</body>
</html>

Create a Tile definition "intro"

Add /WEB-INF/jsp/views.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE tiles-definitions PUBLIC
	"-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN"
	"http://tiles.apache.org/dtds/tiles-config_2_0.dtd">
	
<tiles-definitions>
	
   <definition name="intro" extends="standardLayout">
	<put-attribute name="body" value="/WEB-INF/jsp/intro.jsp" />
   </definition>
		
</tiles-definitions>

Add /WEB-INF/jsp/intro.jsp

Using tiles

Testing

  • Add a Controller forward the request to view "intro"
       mav.setViewName("intro");
    
  • Hit the Controller's URL address

View Resolver Chaining

Spring supports multiple resolver. It will try the lower order view resolver before the higher order.

<bean id="tilesViewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.tiles2.TilesView"/>
    <property name="order" value="0"/>
</bean>
 
<bean id="viewResolver"
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass"
       value="org.springframework.web.servlet.view.JstlView" />
    <property name="prefix" value="/WEB-INF/jsp/" />
    <property name="suffix" value=".jsp" />
    <property name="order" value="1"/>
</bean>

Redirect/Forward View

A controller can forward/redirect a view by returning one of the following String value

redirect:/path/to/something will redirect relative to servlet context
redirect:http://host.domain.com/path/to/something
forward:/path/to/something will redirect relative to servlet context
forward:http://host.domain.com/path/to/something

Use Spring form tag

Use form tag to post Html form data

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>

<form:form commandName="profile">
      <table>
          <tr>
              <td>name:</td>
              <td><form:input path="name" /></td>
          </tr>
          <tr>
              <td>
                  <input type="submit" value="Submit" />
              </td>
          </tr>
      </table>
 </form:form>