Home Spring Simple Form Controlle Example
Questions:Ask|Latest

Related Tutorials


 
 

Share on Google+Share on Google+

Simple Form Controlle Example

Advertisement
Spring provides SimpleFromController for control a form data in the web application. If you want to handle form in spring then you need to use SimpleFormController by extending these in your Controller class.

Simple Form Controlle Example

     

SimpleFormController Example in Spring 2.5 Web MVC Framework

Spring provides SimpleFromController for control a form data in the web application. If you want to handle form in spring then you need to use SimpleFormController by extending these in your Controller class. It's provides formView, successView in the case of valid submission and provide validation errors if wrong data entry by the user in the form data. This is also handle model object for binding and fetching data. In this example we will discuss about SimpleFormController work flow. In this example we will create a form that accept user data and display.

Step 1:

Now we will create a index.jsp inside the WebContent folder that will have a hyperlink "Fill User Personal Details "for generate user request to enter user detail. The code of the index.jsp is:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<html>
<
head>
<
title>User Welcome Page</title>
</
head>
<body bgcolor="#EEEEEE">
<
center>
<
a href="user.html">Fill User Personal Details</a>
</
center>
</
body>
</
html>

Step 2:

Now we will configured web.xml for DispatcherServlet and spring.tld. The code of the web.xml file is:

<?xml version="1.0" encoding="UTF-8"?>
<
web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns
="http://java.sun.com/xml/ns/j2ee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_5.xsd"
version="2.5">
<
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>

Step 3:

Now we will create a dispatcher-servlet.xml file inside the project /WEB-INF/ folder. The dispatcher-servlet.xml file will have configuration code for the particular request and configure a controller that request with set some properties like formView, succesView, commandClass, CommandName, validator etc. 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/schema/beans/spring-beans-2.5.xsd"

  
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="/user.html">
<
ref bean="userController"/>
</
entry>
</
map>
</
property>
</bean>
<bean id="userValidator" class="net.roseindia.web.UserValidator"/>
<
bean id="userController" 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>user</value></property>
<
property name="successView"><value>usersuccess</value></property>
</
bean>
<
bean id="localeChangeInterceptor"
class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<
property name="paramName" value="hl"/>
</
bean>
<bean id="localeResolver"
class="org.springframework.web.servlet.i18n.SessionLocaleResolver"/>
</beans>

In this dispatcher-servlet.xml we have also declared beans for urlMapping and viewResolver.

Step 4:

Now we will create two jsp files user.jsp and usersuccess.jsp inside the /WEB-INF/jsp/ folder. The user.jsp contain a form and form fields for user interface. The code of the user.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>User Personal Details</title></head>
<
body>
<
center> <h3>User Personal Details</h3>
<
br/>
<
form:form commandName="user" method="POST" name="user">
<
table border="0">
<
tr>
<
td>Name:</td>
<
td><form:input path="name"/></td>
<
td><font color="red"><form:errors path="name"/></font></td>
</
tr>
<
tr>
<
td>Email ID:</td>
<
td><form:input path="emailid"/></td>
<
td><font color="red"><form:errors path="emailid"/></font></td>
</
tr>
<
tr>
<
td>Date of birth:</td>
<
td><form:input path="dob"/></td>
<
td><font color="red"><form:errors path="dob"/></font></td>
</
tr>
<
tr>
<
td>Qualification:</td>
<
td><form:input path="qualification"/></td>
<
td><font color="red"><form:errors path="qualification"/></font></td></tr>
<
tr>
<
td>Contact Number:</td>
<
td><form:input path="contact"/></td>
<
td><font color="red"><form:errors path="contact"/></font></td>
</
tr>
<tr>
<
td>Address:</td>
<
td><form:input path="address"/></td>
<
td><font color="red"><form:errors path="address"/></font></td>
</
tr>
<
tr>
<td colspan="3" align="center"><input type="submit" value="Save"/></td>
</tr>
</
table>
</
form:form>
</
center>
</body>
</
html>

The usersuccess.jsp file display data entered by the user in the user.jsp form. The code of the usersuccess.jsp is: 

<%@ page session="false"%>
<%@
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" %>
<html>
<
head>
<
title>User Personal Details Display</title>
</
head>
<
body>
<
center>
<
h3>User Personal Details Display</h3><br>
<
table>
<
tr>
<
td colspan="2" align="center"><font size="5">User Information</font></td>
</tr>
<
tr>
<
td>Name:</td>
<
td><core:out value="${user.name}"/></td>
</tr>
<
tr>
<
td>Email ID:</td>
<
td><core:out value="${user.emailid}"/></td>
</tr>
<
tr>
<
td>Date Of Birth:</td>
<
td><core:out value="${user.dob}"/></td>
</tr>
<
tr>
<
td>Qualification:</td>
<
td><core:out value="${user.qualification}"/></td>
</tr>
<
tr>
<
td>Contact Number:</td>
<
td><core:out value="${user.contact}"/></td>
</tr>
<
tr>
<
td>Address:</td>
<
td><core:out value="${user.address}"/></td>
</tr>
</
table>
<
a href="user.html">Back</a>
</
center>
</
body>
</
html>

Step 5:

Now we will create User.java class inside the project src folder that will have some variables and setter and getter for these variables. we have used this class instance as a commandName and use this class as a command class for request controller. The code of the User.java class is:

package net.roseindia.web;

public class User {
  public User(){}
  private String name;  
  private String emailid;
  private String dob;
  private String address;
  private String qualification;
  private String contact;

  public String getDob() {
  return dob;
  }
  public void setDob(String dob) {
  this.dob = dob;
  }
  public String getQualification() {
  return qualification;
  }

  public void setQualification(String qualification) {
  this.qualification = qualification;
  }
  public String getContact() {
  return contact;
  }

  public void setContact(String contact) {
  this.contact = contact;
  }

  public String getName() {
  return name;
  }

  public void setName(String name) {
  this.name = name;

  }
  public String getEmailid() {
  return emailid;
  }

  public void setEmailid(String emailid) {
  this.emailid = emailid;
  }
  public String getAddress() {
  return address;
  }

  public void setAddress(String address) {
  this.address = address;
  }
}

Step 6:

Now we will create UserFormController.java class that extends SimpleFormController inside the project src folder for control the request. It will have override onSubmit() method that accept class object as a parameter and after proceed these object return ModelAndView for the user display. The code of the UserFormController.java class 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 = (Usercommand;  
  
  ModelAndView modelAndView = new ModelAndView(getSuccessView());
  modelAndView.addObject("user", user);
  return modelAndView;  
  }
}

Page 2

     

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: October 6, 2010

Related Tutorials

Ask Questions?    Discuss: Simple Form Controlle Example  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
helen
June 4, 2011
missing download code and files

Discuss: Simple Form Controlle Example Could you send to me the download code and the rest part of this tutorial starting from Step 7.
karthick
May 3, 2012
plz any one clear my doubt

hey i need simple program in simple form controller plz any one help me...... i am not understand this example
DMCA.com