Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Mastering Spring MVC

Enjoy Spring-based Web development with the Spring MVC module

  • Print
  • Feedback

Page 3 of 4

Listing 4 shows the source code for the home.jsp file.

Listing 4. home.jsp

<?xml version="1.0" encoding="UTF-8" ?>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" import="com.geekcap.geeknews.core.*,java.util.List"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>

<!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">
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
      <title>Geek News</title>
      <link type="text/css" rel="stylesheet" href="css/geeknews.css" />
   </head>

<body>

<%@ include file="header.jsp" %>

<div id="mainContent">

<p><a href="post.htm">Post</a></p>

<c:forEach items="${articles}" var="article">

<div class="article">
<p><a href="article.htm?id=<c:out value="${article.id}"/>"><c:out value="${article.title}" /></a> 
   by <span class="articleAuthor"><c:out value="${article.author}" /></span>
   on <span class="articleDate"><fmt:formatDate value="${article.date}" type="both" /></span>
</p>

<p class="articleSummary"><c:out value="${article.summary}" /></p>
</div>

</c:forEach>

</div>


<%@ include file="footer.jsp" %>

</body>
</html>

The details of the HTML and CSS presentation are unimportant for this discussion; what's important is that the request object has an articles variable in it. The following snippet demonstrates how the JSTL core library is used to iterate over all articles and display an article's author, title, publication date, and summary:

<c:forEach items="${articles}" var="article">

<p><a href="article.htm?id=<c:out value="${article.id}"/>">
       <c:out value="${article.title}" /></a> 

   by <c:out value="${article.author}" />
   on <fmt:formatDate value="${article.date}" type="both" />
</p>

<p class="articleSummary"><c:out value="${article.summary}" /></p>

</c:forEach>

Figure 1 shows a screenshot of the Geek News homepage.

Geek News home page

Figure 1. Geek News home page (Click to enlarge.)

The header consists of the words GEEK NEWS, hyperlinked to the home page. The body consists of a Post link, which brings up a form (which you'll implement later) for posting new articles, and then one section for each article that includes the title, author, publication date, and summary, followed by a horizontal line (implemented as a CSS bottom border on the article <div>). A footer (not shown) has copyright information (for "Fictitious Company," so don't worry, the code is yours to keep).

The header and footer are imported in the home.jsp page through the JSP include directive. They are both JSPs themselves. In their current state, they could be simple HTML documents, but in the future you might want to add a login link to the header or administration links to the footer.

Setup and deployment

Before you can compile and deploy the HomePageController, you need to download, decompress, and install the latest Spring Framework version (as of this writing, version 2.5.6) and its dependencies.

  • Print
  • Feedback

Resources

More from JavaWorld