Say Hello application in Spring 2.5 MVC
This application will help you in getting started with the development of form based application in Spring MVC. You will learn how to create form and then retrieve the form data in another controller.
In this tutorial we will create a web application that will present a form to the user. Then user can enter some and press "Save" button. Then application displays name with "Hello" message. For example we will enter name "Brijesh" then the application will display with "Hello Brijesh".
Step 1:
Now we will create a index.jsp in project's WebContent folder. In the index.jsp file we will create a hyperlink "Say Hello" that will be linked "entername.html" file. The code of the index.jsp is:
<%@page contentType="text/html" pageEncoding="UTF-8"%> <html> <body> <a href="entername.html">Say Hello</a> </body> </html> |
Step 2:
Now we will configure the DispatcherServlet in web.xml. The full code of the web.xml is:
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_5.xsd" > <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>*.html</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <taglib> <taglib-uri>/spring</taglib-uri> <taglib-location>/WEB-INF/spring.tld</taglib-location> </taglib> </web-app> |
In this web.xml we have to used <taglib></taglib> tag for include the spring.tld file in this application. These <taglib></taglib> contains two tags one is <taglib-url> that gives a url for access the spring tag library and other is <taglib-location> tag that told us where is spring.tld file.
Step 3:
Now we will create a dispatcher-servlet.xml in the WEB.INF folder that is the main configuration file for this application. This file contains the all configuration beans for this application. The code of the dispatcher-servlet.xml is:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org xmlns:p="http://www.springframework.org/schema/p"> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix"> <value>/WEB-INF/jsp/</value> </property> <property name="suffix"> <value>.jsp</value> </property> </bean> <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="interceptors"> <list> <ref local="localeChangeInterceptor"/> </list> </property> <property name="urlMap"> <map> <entry key="/entername.html"> <ref bean="enternameController"/> </entry> </map> </property> </bean> <bean id="userValidator" class="net.roseindia.web.UserValidator"/> <bean id="enternameController" class="net.roseindia.web.UserFormController"> <property name="sessionForm"><value>false</value></property> <property name="commandName"><value>user</value></property> <property name="commandClass"><value>net.roseindia.web.User</value></property> <property name="validator"><ref bean="userValidator"/></property> <property name="formView"><value>entername</value></property> <property name="successView"><value>sayhello</value></property> </bean> <bean
id="localeChangeInterceptor"
class=" <property name="paramName" value="hl"/> </bean> <bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver"/> </beans> |
This file contains the urlMapping for the entername.html file and configure the controller for this file. It have also configure Validator, commandClass, commandName, formView, and successView etc.
The validator property gives the permission to set validator for the perticular request and add the validator class with this request controller. This is the code for UserValidator.java:
<bean id="userValidator" class="net.roseindia.web.UserValidator"/> <bean id="enternameController" class="net.roseindia.web.UserFormController"> <property name="validator"><ref bean="userValidator"/></property> </bean> |
The commandClass property is configure the model class with this request and controller. The code for the commandClass is:
<bean id="enternameController" class="net.roseindia.web.UserFormController"> <property name="commandClass"><value>net.roseindia.web.User</value></property> </bean> |
The commandName is the name of the command that is used to access data from the business layer and provided data to view layer for view. The code for the commandName property is:
<bean id="enternameController" class="net.roseindia.web.UserFormController"> <property name="commandName"><value>user</value></property> </bean> |
The successView property is used to display view after the process request for this request. The formView property provide view of the request.
Step 4:
Now we will create a entername.jsp file inside the WEB-INF/jsp/ folder for enter name by the user. The code of the entername.jsp is:
<%@ taglib prefix="core" uri="http://java.sun.com/jsp/jstl/core" %> <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> <%@ taglib prefix="spring" uri="/spring"%> <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> <html> <head> <title>Enter Name</title> </head> <body> <center> <h1>Enter name</h1> <br/> <form:form commandName="user" method="POST" name="user"> Name:<form:input path="name"/><br/> <font color="red"><form:errors path="name"/></font><br/> <input type="submit" value="Save"/> </form:form> </center> </body> </html> |
Step 5:
Now we will create a sayhello.jsp for display the name with "Hello". And after success the request of the name enter by the user. The code of the sayhello.jsp is:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <html> <body> <center> <font size="16"> <c:out value="${prestatement}"/> <c:out value=" "/> <c:out value="${user.name}"/> </font> </center> </body> </html> |
Step 6:
Now we will create a UserFormController.java class inside the application src folder that process the request and return the model data and formView for the application. The code of the UserFormController.java is:
package net.roseindia.web; import javax.servlet.http.HttpServletRequest;
import javax.servlet.ServletException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;
import org.springframework.web.servlet.mvc.SimpleFormController;
import net.roseindia.web.User;
@SuppressWarnings("deprecation")
public class UserFormController extends SimpleFormController {
@Override
protected ModelAndView onSubmit(Object command) throws ServletException {
User user = (User) command;
String name = user.getName();
String prestatement = "Hello";
ModelAndView modelAndView = new ModelAndView(getSuccessView());
modelAndView.addObject("user", user);
modelAndView.addObject("prestatement", prestatement);
return modelAndView;
}
}
Step 7:
Now we will create a UserValidator.java class for validate the entername.jsp file form fields. The code of the UserValidator.java is:
package net.roseindia.web;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;
import org.springframework.validation.ValidationUtils;
import net.roseindia.web.*;
public class UserValidator implements Validator {
@Override
public boolean supports(Class clazz) {
return User.class.isAssignableFrom(clazz);
}
public void validate(Object obj, Errors errors) {
User user = (User) obj;
if (user.getName() == null || user.getName().length() == 0) {
errors.rejectValue("name", "error.empty.field", "Please Enter Name");
}
}
}
Step 8:
Now we will create a Model class inside the project src folder. that provide the business functionality in the application. The code of the User.java is:
|
Step 9:
Now we will run this application and see the output that have a hyperlink:
Now we will be click on this hyperlink that link with the entername.html file. So server send us on the entername.html file.
Now we will enter name and save and final output is:
Download Code