Spring MVC Say Hello Example

Learn how to develop form based say hello example in Spring 2.5 MVC.



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"%>



<a href="entername.html">Say Hello</a>



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"




http://java.sun.com/xml/ns/j2ee/web-app_2_5.xsd" >


















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"?>




xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org


<bean id="viewResolver"


<property name="prefix">



<property name="suffix">




<bean id="urlMapping"


<property name="interceptors">


<ref local="localeChangeInterceptor"/>



<property name="urlMap">


<entry key="/entername.html">

<ref bean="enternameController"/>





<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 id="localeChangeInterceptor" class="

<property name="paramName" value="hl"/>


<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver"/>


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>


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>


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>


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" %>



<title>Enter Name</title>




<h1>Enter name</h1>


<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"/>





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" %>




<font size="16">

<c:out value="${prestatement}"/>

<c:out value=" "/>

<c:out value="${user.name}"/>





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;

public class UserFormController extends SimpleFormController {
  protected ModelAndView onSubmit(Object commandthrows ServletException {
  User user = (Usercommand;
  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 {
public boolean supports(Class clazz) {
return User.class.isAssignableFrom(clazz);
  public void validate(Object obj, Errors errors) {
 User user = (Userobj;
  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:

package net.roseindia.web;

public class User {
  public User(){}
  private String name;
  public String getName() {
  return name;
  public void setName(String name) {
  this.name = name;

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

Download example code