You are on page 1of 9

1.

Online Bookstore Admin Application

The admin application will be designed to allow administrators enter information about new
books. Here are the steps that implement this requirement:

Step 1. Create a web project and add all the required jars
I will be using Eclipse’s and here is a list of the required jars that need to be in the classpath:

org.springframework.asm-3.0.1.RELEASE.jar
org.springframework.beans-3.0.1.RELEASE.jar
org.springframework.context-3.0.1.RELEASE.jar
org.springframework.context.support-3.0.1.RELEASE.jar
org.springframework.core-3.0.1.RELEASE.jar
org.springframework.expression-3.0.1.RELEASE.jar
org.springframework.test-3.0.1.RELEASE.jar
org.springframework.web-3.0.1.RELEASE.jar
org.springframework.web.servlet-3.0.1.RELEASE.jar
commons-collections-3.1.jar
hibernate-validator-4.0.2.GA.jar
jcl-over-slf4j-1.5.10.jar
joda-time-1.6.jar
jstl-1.2.jar
log4j.jar
slf4j-api-1.5.8.jar
slf4j-log4j12.jar
validation-api-1.0.0.GA.jar

Step 2. Create a web-Context.xml file under WEB-INF folder to hold web layer spring bean
configuration. Here are the contents of the file:

view plaincopy to clipboardprint?

1. <?xml version="1.0" encoding="UTF-8"?>  
2. <beans xmlns="http://www.springframework.org/schema/beans"  
3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
4.     xmlns:context="http://www.springframework.org/schema/context"  
5.     xmlns:mvc="http://www.springframework.org/schema/mvc"  
6.     xsi:schemaLocation="  
7.         http://www.springframework.org/schema/beans http://www.springframework.org/sc
hema/beans/spring-beans-3.0.xsd  
8.         http://www.springframework.org/schema/context http://www.springframework.org/s
chema/context/spring-context-3.0.xsd  
9.         http://www.springframework.org/schema/mvc http://www.springframework.org/sch
ema/mvc/spring-mvc-3.0.xsd">  
10.   
11.     <!-- Enable annotation driven controllers, validation etc... -->  
12.     <mvc:annotation-driven />  
13.   
14.     <!-- Controllers package -->  
15.     <context:component-scan base-package="com.inflinx.blog.bookstore.web.controller" /
>  
16.   
17.     <!-- JSP page location -->  
18.     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
 
19.         <property name="suffix" value=".jsp"/>  
20.     </bean>  
21.   
22. </beans>  

Step 3. Add Spring MVC Dispatcher Servlet to web.xml file. In the declaration below, Spring
MVC will handle all URL requests to html pages.

view plaincopy to clipboardprint?

1. <?xml version="1.0" encoding="UTF-8"?>  
2. <web-app version="2.5"  
3.     xmlns="http://java.sun.com/xml/ns/javaee"  
4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
5.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
6.   
7. tp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  
8.   
9.   <servlet>  
10.         <servlet-name>bookstore</servlet-name>  
11.         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
12.         <init-param>  
13.             <param-name>contextConfigLocation</param-name>  
14.             <param-value>/WEB-INF/web-Context.xml</param-value>  
15.         </init-param>  
16.         <load-on-startup>1</load-on-startup>  
17.     </servlet>  
18.   
19.     <servlet-mapping>  
20.         <servlet-name>bookstore</servlet-name>  
21.         <url-pattern>*.html</url-pattern>  
22.     </servlet-mapping>  
23.   
24. </web-app>  

Step 4. Create a Book domain object to hold information about a book:


view plaincopy to clipboardprint?

1. package com.inflinx.blog.bookstore.domain;  
2.   
3. public class Book  
4. {  
5.     private String name;  
6.     private String description;  
7.   
8.     public String getName()  
9.     {  
10.         return name;  
11.     }  
12.     public void setName(String name)  
13.     {  
14.         this.name = name;  
15.     }  
16.     public String getDescription()  
17.     {  
18.         return description;  
19.     }  
20.     public void setDescription(String description)  
21.     {  
22.         this.description = description;  
23.     }  
24.   
25.     @Override  
26.     public String toString()  
27.     {  
28.         return "Name: " + name + ", Description: " + description ;  
29.     }  
30. }  

Step 5. Create a BookFormController to prepare the form and process form submission

view plaincopy to clipboardprint?

1. package com.inflinx.blog.bookstore.web.controller;  
2.   
3. import java.util.Map;  
4.   
5. import org.springframework.stereotype.Controller;  
6. import org.springframework.web.bind.annotation.RequestMapping;  
7. import org.springframework.web.bind.annotation.RequestMethod;  
8.   
9. import com.inflinx.blog.bookstore.domain.Book;  
10.   
11. @Controller  
12. @RequestMapping("/book.html")  
13. public class BookFormController  
14. {  
15.     // Display the form on the get request  
16.     @RequestMapping(method=RequestMethod.GET)  
17.     public String showForm(Map model)  
18.     {  
19.         Book book  = new Book();  
20.         model.put("book", book);  
21.         return "form";  
22.     }  
23.   
24. }  

Step 6. Create form.jsp page with a form to capture book details:

view plaincopy to clipboardprint?

1. <%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>  
2. <html>  
3.     <head></head>  
4.     <body>  
5.         <form:form method="post" action="book.html" commandName="book">  
6.             <table>  
7.                 <tr>  
8.                     <td>Name:</td> <td><form:input path="name" /></td>  
9.                 </tr>  
10.                 <tr>  
11.                     <td>Description:</td> <td><form:textarea path="description"/></td>  
12.                 </tr>  
13.                 </table>  
14.             <input type="submit" value="Create" />  
15.         </form:form>  
16.     </body>  
17. </html>  

The form will be submitted to the book.html page.

Step 7. Modify BookFormController to read the submitted data and process it.

view plaincopy to clipboardprint?

1. // Process the form.  
2. @RequestMapping(method=RequestMethod.POST)  
3. public String processForm(Book book, Map model)  
4. {  
5.     // Persistence logic to save the book will go here  
6.   
7.     // Add the saved book to the model  
8.     model.put("book", book);  
9.     return "result";  
10. }  

Step 8. Create a result.jsp page to display the form submission result:

view plaincopy to clipboardprint?

1. <html>  
2.     <body>  
3.         ${book.name} is stored in the database  
4.     </body>  
5. </html>  

Deploy the application and go to http://<yourserver:port>/bookstore/book.html. The page should


present a form shown below:

Form

Enter the book name “Test Book” and submit the form. You should see a screen that looks
similar to image below:
Form Submission Result

2 Adding JSR 303 validation

Consider the following validation requirements to the book class:


a. Name and Description cannot be null or blank
b. Description cannot be more than 50 characters

Here are the steps to add the above validation requirements:

Step 1. Add @NotEmpty and @Size annotations to the name and description files of the Book
class

view plaincopy to clipboardprint?

1. public class Book  
2. {  
3.     @NotEmpty  
4.     private String name;  
5.   
6.     @Size(min=1, max=50)  
7.     private String description;  
8.   
9.     // Getters and setters here  
10. }  

NotEmpty annotation is not part of JSR 303 specification. It is part of Hibernate Validator
framework which is the reference implementation for JSR 303.

Step 2. Modify BookFormController to have Spring validate the submitted data. This is done by
adding a @Valid annotation before the book method parameter. The result of the validation can
be accessed using the BindingResult method parameter.

view plaincopy to clipboardprint?

1. // Process the form.  
2. @RequestMapping(method=RequestMethod.POST)  
3. public String processForm(@Valid Book book, BindingResult result, Map model)  
4. {  
5.     if(result.hasErrors())  
6.     {  
7.         return "form";  
8.     }  
9.     // Persistence logic to save the book will go here  
10.   
11.     // Add the saved book to the model  
12.     model.put("book", book);  
13.     return "result";  
14. }  

Step 3. Modify the form.jsp so that error messages can be displayed:

view plaincopy to clipboardprint?

1.     <form:form method="post" action="book.html" commandName="book">  
2.             <table>  
3.                 <tr>  
4.                     <td>Name:</td> <td><form:input path="name" /></td> <td><form:errors p
ath="name" /></td>  
5.                 </tr>  
6.                 <tr>  
7.                     <td>Description:</td> <td><form:textarea path="description"/></td> <td><
form:errors path="description" /></td>  
8.                 </tr>  
9.                 </table>  
10.             <input type="submit" value="Create" />  
11. </form:form>  

Redeploy the application and when you submit an empty form, you should see validation failure
messages next to the fields:

Validation Failed

3 Customizing error messages


The validation failure messages in the image above are default to the framework. To make
custom error messages appear follow these steps:

Step 1. Create a messages.properties file under the WEB-INF folder. Add the following
messages:

view plaincopy to clipboardprint?

1. NotEmpty.book.name=Name is a required field  
2. e.book.description=Description must be between 1 and 50 characters  

Each message above follows this convention:

<CONSTRAINT_NAME>.<COMMAND_NAME>.<FIELD_NAME>

Step 2. Let Spring know about the newly created properties file. This is done by modifying the
web-Context.xml file to include this bean declaration:

view plaincopy to clipboardprint?

1.     <bean id="messageSource" class="org.springframework.context.support.ReloadableRe
sourceBundleMessageSource">  
2. property name="basename" value="/WEB-INF/messages">  
3. </property>  
4.    </bean>  

Redeploy the application and submit an empty form. This time you should see custom error
messages appear next to the fields:

Custom Validation Failed Messages

Download the source code for this post here. The application is tested on Glassfish 3.0 and
WebLogic 10.3.

You might also like