|
The Sample Application
Here are snippets of the real Web application, which generates a mailing and keeps information regarding these mailings in a database (using the Hibernate).
Consider the following listing page (list_mailings.jsp):
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<html>
<head>
<title>List All Mailings</title>
</head>
<body>
<center>
<table border="1">
<tr>
<td align="center">ID</td>
<td align="center">FROM</td>
<td align="center">TO</td>
<td align="center">SUBJECT</td>
</tr>
<c:forEach var="m" items="${allMailings}">
<tr>
<td>${m.id}</td>
<td>${fn:escapeXml(m.from)}</td>
<td>${fn:escapeXml(m.to)}</td>
<td>${m.subject}</td>
/tr>
</c:forEach>
</table>
</center>
</body>
</html>
This is part of a simple Web application acts as a front-end for a mailing engine. This screen shows the list of all mailings made, the ID of the mailings, and the headers "From," "To," and "Subject."
The Spring MVC configuration looks like springmvc-servlet.xml:
<bean id="listMailingsController"
class="us.prokhorenko.samples.ListMailingsController">
...
</bean>
<bean id="urlMapping"
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
...
<prop key="/listMailings.html">listMailingsController</prop>
...
</props>
</property>
...
</bean>
The sessionFactory bean, in the applicationContext.xml file, is used to integrate Hibernate using Spring's code. It contains a complete Hibernate configuration, much like hibernate.cfg.xml:
...
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
...
<property name="mappingResources">
<list>
...
<value>Mailing.hbm.xml</value>
...
</list>
</property>
...
</bean>
...
Now take a look at the controller code for the list of mailings (ListMailingController.java):
package us.prokhorenko.samples;
import us.prokhorenko.samples.MailingManager;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ListMailingsController implements Controller {
private MailingManager mailingManager;
public void setMailingManager(MailingManager mailingManager) {
this.mailingManager = mailingManager;
}
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse
response) throws Exception {
request.setAttribute("allMailings",
mailingManager.listMailings("from Mailing m"));
return new ModelAndView("list_mailings");
}
}
MailingManager is a manager bean which provides service for working with Mailing object (mapped by Hibernate). The sample described above doesn't need any special configuration at all.
While it's a nice list screen, it's probably not good enough for this application. First of all, as the number of mailings grow, it will be harder to see them, so you need paging. Secondly, you will probably want to add search capabilities so you can find a particular recepient or filter by subject. Lastly, you will definitely want sorting.
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.
|