Spring MVC Say Hello Example

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

Spring MVC Say Hello Example

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


  1. How much time will it take to learn Spring and Hibernate?
  2. Advantages of Spring Framework
  3. Spring 4 Introduction and example
  4. Features of Spring Framework 5
  5. How to make Spring web Login form?
  6. How to make Simple form in Spring web?
  7. Spring, Hibernate login and registration application
  8. Spring Framework Tutorial for beginners with examples
  9. Spring Framework for Apache Hadoop 2.3.0 GA released
  10. Spring Framework 4.1 GA is released with major features
  11. Why to use Spring Framework?
  12. Spring Framework 4.1 - First Release candidate available
  13. Spring IO Platform 1.0.0 Released
  14. Spring 4: Login Form using Spring MVC and Hibernate Example
  15. Spring 4 MVC Login form Example with source code
  16. Spring 4 MVC Hello World Example: Spring 4 MVC Tutorial will full source code
  17. Spring Web MVC Application Error:ClassNotFoundException: DispatcherServlet on deploying
  18. Features of Spring 4
  19. Spring Framework 4.0 released
  20. Spring Framework 4: Spring Framework 4 Tutorials and Example
  21. Spring Integration 3.0 Release is released and available for download
  22. Spring Tutorial for Beginners
  23. Java Springs Framework Tutorial
  24. Spring Architecture
  25. Spring Framework Tutorials
  26. database spring registration form
  27. Spring Login Example
  28. Roseindia Spring Tutorial
  29. Spring Tutorial
  30. Spring 3.2 MVC insert and retrieve blob from the database
  31. The hidden tag
  32. The errors tag
  33. net.roseindia.dao
  34. net.roseindia.service
  35. net.roseindia.model
  36. net.roseindia.controller
  37. Spring 3.2 MVC Hibernate Example
  38. Spring 3.2 MVC, Upload File in a specific folder
  39. Spring 3.2 MVC Form Handling
  40. The textarea tag