lishman levelup
«previous  next»


Getting Started
Form Processing
IoC Container
Hibernate ORM



The Spring MVC View


Once the controller has finished processing the request, the view can present the data from the model. We use JSPs and JSTL to display the data for our application.

The Logical View Name

To do this, first we must establish the logical view name, to inform Spring which particular view is to be displayed. By default, this is derived from the URL path of our request. For example:
  • /countryList.html produces a view name of countryList
  • /countryDetails.html produces a view name of countryDetails
We can override the default by returning a specific view name from our controller, as we will see later in the Form Processing section, but for now we can just rely on the default.

View Resolvers

Once we have our logical view name, a view resolver is used to render the display. Spring provides several view resolvers for technologies such as Velocity templates and XSLT, but we will use Spring's InternalResourceViewResolver class to map the view name to a physical JSP file. For example:
  • countryList is mapped to /WEB-INF/jsp/countryList.jsp
  • countryDetails is mapped to /WEB-INF/jsp/countryDetails.jsp
This separation of logical view names from view resolvers allows the controllers to produce data models, without being tied to a particular view technology. Soon, we will see how to configure all this in Spring, but for now we just need to produce the JSP pages that will display our data.

The Country List Page

The countryList.jsp page displays a list of countries. Each name is a link to a page which shows further details about that country. We use JSTL custom tags and the JSP expression language to retrieve data from the model and construct the HTML table.
<table>
  <c:forEach items="${countries}" var="country">
    <tr>
      <td>
        <a href="countryDetails.html?id=${country.id}">
          ${country.name}
        </a>
      </td>
    </tr>
  </c:forEach>
</table>
Info Note that the JSP page does not contain any business logic. All the data has been prepared by the controller, allowing the view to concentrate solely on the presentation.
The countries collection, which was added to the model by the getCountries() method in the CountryController class, is now accessible by the JSP expression language. The forEach tag iterates over the collection of Country JavaBeans, using the name property for the display value, and the id property in the link to the details page.

The Country Details Page

The countryDetails.jsp page displays the details of an individual country.
<table>
  <tr>
    <td>Name</td>
    <td>${country.name}</td>
  </tr>
  <tr>
    <td>Area</td>
    <td>${country.area}</td>
  </tr>
  <tr>
    <td>Population</td>
    <td>${country.population}</td>
  </tr>
  <tr>
    <td>Last Updated</td>
    <td>${country.populationLastUpdated}</td>
  </tr>
  <tr>
    <td>Currency</td>
    <td>${country.currency}</td>
  </tr>
</table>
The data is retrieved from the Country JavaBean, which was added to the model by the getCountry() method in the CountryController class.
»