Cheap VPS & Xen Server


Residential Proxy Network - Hourly & Monthly Packages

MVC Form Example


Here, we will learn how to handle a form data in spring MVC without using database. Here, we will use @Controler, @RequestMapping and @ModelAttribute annotations.

To display the input form, we are going to use <form:form> tag of spring framework. Let’s see a simple example to store form data in a model object and display data of a list.


Required Jar files

To run this example, you need to load:

  • Spring Core jar files
  • Spring Web jar files

download all the jar files for spring including core, web, aop, mvc, j2ee, remoting, oxm, jdbc, orm etc.


index.jsp

  1. <a href=“empform”>Add Employee</a>
  2. <a href=“viewemp”>View Employees</a>

Emp.java

  1. package com.Kreationnext.beans;
  2. public class Emp {
  3. private int id;
  4. private String name;
  5. private float salary;
  6. private String designation;
  7. public Emp() {}
  8. public Emp(int id, String name, float salary, String designation) {
  9.     super();
  10.     this.id = id;
  11.     this.name = name;
  12.     this.salary = salary;
  13.     this.designation = designation;
  14. }
  15. public int getId() {
  16.     return id;
  17. }
  18. public void setId(int id) {
  19.     this.id = id;
  20. }
  21. public String getName() {
  22.     return name;
  23. }
  24. public void setName(String name) {
  25.     this.name = name;
  26. }
  27. public float getSalary() {
  28.     return salary;
  29. }
  30. public void setSalary(float salary) {
  31.     this.salary = salary;
  32. }
  33. public String getDesignation() {
  34.     return designation;
  35. }
  36. public void setDesignation(String designation) {
  37.     this.designation = designation;
  38. }
  39. }

EmpController.java

  1. package com.Kreationnext.controllers;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import org.springframework.stereotype.Controller;
  5. import org.springframework.web.bind.annotation.ModelAttribute;
  6. import org.springframework.web.bind.annotation.RequestMapping;
  7. import org.springframework.web.bind.annotation.RequestMethod;
  8. import org.springframework.web.servlet.ModelAndView;
  9. import com.Kreationnext.beans.Emp;
  10. @Controller
  11. public class EmpController {
  12.     @RequestMapping(“/empform”)
  13.     public ModelAndView showform(){
  14.          //command is a reserved request attribute name, now use <form> tag to show object data
  15.         return new ModelAndView(“empform”,“command”,new Emp());
  16.     }
  17.     @RequestMapping(value=“/save”,method = RequestMethod.POST)
  18.     public ModelAndView save(@ModelAttribute(“emp”) Emp emp){
  19.         //write code to save emp object
  20.         //here, we are displaying emp object to prove emp has data
  21.         System.out.println(emp.getName()+” “+emp.getSalary()+” “+emp.getDesignation());
  22.         //return new ModelAndView(“empform”,”command”,emp);//will display object data
  23.         return new ModelAndView(“redirect:/viewemp”);//will redirect to viewemp request mapping
  24.     }
  25.     @RequestMapping(“/viewemp”)
  26.     public ModelAndView viewemp(){
  27.         //write the code to get all employees from DAO
  28.         //here, we are writing manual code of list for easy understanding
  29.         List<Emp> list=new ArrayList<Emp>();
  30.         list.add(new Emp(1,“rahul”,35000f,“S.Engineer”));
  31.         list.add(new Emp(2,“aditya”,25000f,“IT Manager”));
  32.         list.add(new Emp(3,“sachin”,55000f,“Care Taker”));
  33.         return new ModelAndView(“viewemp”,“list”,list);
  34.     }
  35. }

web.xml

  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.     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd”>
  7.  <servlet>
  8.     <servlet-name>spring</servlet-name>
  9.     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  10.     <load-on-startup>1</load-on-startup>
  11. </servlet>
  12. <servlet-mapping>
  13.     <servlet-name>spring</servlet-name>
  14.     <url-pattern>/</url-pattern>
  15. </servlet-mapping>
  16. </web-app>

spring-servlet.xml

  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:p=“http://www.springframework.org/schema/p”
  5.     xmlns:context=“http://www.springframework.org/schema/context”
  6.     xsi:schemaLocation=”http://www.springframework.org/schema/beans  
  7. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
  8. http://www.springframework.org/schema/context  
  9. http://www.springframework.org/schema/context/spring-context-3.0.xsd”>  
  10. <context:component-scan base-package=“com.Kreationnext.controllers”></context:component-scan>
  11. <bean class=“org.springframework.web.servlet.view.InternalResourceViewResolver”>
  12. <property name=“prefix” value=“/WEB-INF/jsp/”></property>
  13. <property name=“suffix” value=“.jsp”></property>
  14. </bean>
  15. </beans>

empform.jsp

  1. <%@ taglib uri=“http://www.springframework.org/tags/form” prefix=“form”%>
  2. <%@ taglib uri=“http://java.sun.com/jsp/jstl/core” prefix=“c”%>
  3.        <form:form method=“post” action=“save”>
  4.         <table >
  5.          <tr>
  6.           <td>Name : </td>
  7.           <td><form:input path=“name”  /></td>
  8.          </tr>
  9.          <tr>
  10.           <td>Salary :</td>
  11.           <td><form:input path=“salary” /></td>
  12.          </tr>
  13.          <tr>
  14.           <td>Designation :</td>
  15.           <td><form:input path=“designation” /></td>
  16.          </tr>
  17.          <tr>
  18.           <td colspan=“2”><input type=“submit” value=“Save” /></td>
  19.          </tr>
  20.         </table>
  21.        </form:form>

viewemp.jsp

  1.    <%@ taglib uri=“http://www.springframework.org/tags/form” prefix=“form”%>
  2.    <%@ taglib uri=“http://java.sun.com/jsp/jstl/core” prefix=“c”%>
  3. <table border=“2” width=“70%” cellpadding=“2”>
  4. <tr><th>Id</th><th>Name</th><th>Salary</th><th>Designation</th></tr>
  5.    <c:forEach var=“emp” items=“${list}”>
  6.    <tr>
  7.    <td>${emp.id}</td>
  8.    <td>${emp.name}</td>
  9.    <td>${emp.salary}</td>
  10.    <td>${emp.designation}</td>
  11.    </tr>
  12.    </c:forEach>
  13.    </table>

Output

spring-mvc-form-output1

spring-mvc-form-output2

spring-mvc-form-output3

spring-mvc-form-output4

Download spring MVC example

We have created this application in MyEclipse IDE which already provides the jar files. If you use eclipse or other IDE’s, you need to load the jar file for spring MVC.

Comments

comments