View RSS Feed

Spring Framework

Defining the Page View with Tiles View Resolvers in Spring MVC

Rate this Entry
by , 11-27-2011 at 10:41 PM (2265 Views)
Following on from my previous tip, I will now define the page view from our previous tip. Using Tiles properly, the home page is composed of several distinct pieces. The main_template.jsp file describes the common layout for all pages in the Springexample application, while home.jsp displays the main content for the home page. Plus, carForm.jsp and login.jsp provide additional common elements.
The home.jsp is where the home page request finishes its journey. It picks up the list of Cars that HomeController placed into the model and renders them to be displayed in the userís browser. Below is the home.jsp:

Java Code:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 
<%@ taglib prefix="s" uri="http://www.springframework.org/tags"%> 
<%@ taglib prefix="t" uri="http://tiles.apache.org/tags-tiles"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<div> 
    <h2>The social community for cars on the web for everyone else to see..</h2>
 <h3>Look at the cars available right now...</h3>
<ol class="car-list"> <c:forEach var="car" items="${cars}">
<s:url value="/cars/{carName}" var="car_url" >
<s:param name="carModel" value="${car.model}" />
</s:url>
<li> <span class="carListImage">
<img src= "http://s3.amazonaws.com/carrImages/${cars.id}.jpg"
width="48" border="0" align="middle" onError=
"this.src='<s:url value="/resources/images"/>/car_model.png';"/>
</span>
<span class="carListText">
<a href="${car_url}">
<c:out value="${car.model}" /></a> - <c:out value="${car.text}" /><br/>
<small><fmt:formatDate value="${car.when}" pattern="hh:mma MMM d, yyyy" /></small>
</span>
</li> </c:forEach>
</ol> </div>
The home.jsp is contained in the <c:forEach> tag, which cycles through the list of Cars, rendering the key details of each one as it goes. Since the cars were placed into the model, the list is referenced in the JSP using ${car}. There are a number of ways for you to use Tiles with Spring. Consult your documentation for more information.

Submit "Defining the Page View with Tiles View Resolvers in Spring MVC" to Facebook Submit "Defining the Page View with Tiles View Resolvers in Spring MVC" to Digg Submit "Defining the Page View with Tiles View Resolvers in Spring MVC" to del.icio.us Submit "Defining the Page View with Tiles View Resolvers in Spring MVC" to StumbleUpon Submit "Defining the Page View with Tiles View Resolvers in Spring MVC" to Google

Comments