Spring MVC form handling tutorial

Let us discuss spring mvc form handling example in eclipse.

Example Explanation:

Use http://localhost:8080/SpringMVCExample5/student url to start the application. A request for respective resource will generate. Request will be handled by DispatcherServlet. It delegates the request to the FormHandlingController controller. The LoginController controller resolve the request with help of RequestMapping annotation, executes the specific functionality and returns the ModelAndView object to the DispatcherServlet. The DispatcherServlet then take the help of InternalResourceViewResolver to get the actual view name. In our example it will return the “/WEB-INF/jsp/student.jsp. The DispatcherServlet then insert the model data into view and render response. Same request response cycle will execute when we click on submit button after entering the student details.

Example:

student.jsp


<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>

  
    Spring MVC form handling example.
  
  
  

   
Name
Class
RollNo

web.xml

  

   
   
      FormHandling
      
         org.springframework.web.servlet.DispatcherServlet
      
      1
   

   
      FormHandling
      /
   
 

FormHandling-servlet.xml

  


   

   
      
      
   
 

Student.java

public class Student {
	private String name;
	private String className;
	private String rollNo;
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getClassName() {
		return className;
	}
	public void setClassName(String className) {
		this.className = className;
	}
	public String getRollNo() {
		return rollNo;
	}
	public void setRollNo(String rollNo) {
		this.rollNo = rollNo;
	}
	
}

FormHandlingController.java

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class FormHandlingController {
   @RequestMapping(value = "/student", method = RequestMethod.GET)
   public ModelAndView student() {
      return new ModelAndView("student", "command", new Student());
   }
   
   @RequestMapping(value = "/addStudent", method = RequestMethod.POST)
   public String addStudent(@ModelAttribute("SpringWeb")Student student, 
   ModelMap model) {
      model.addAttribute("name", student.getName());
      model.addAttribute("className", student.getClassName());
      model.addAttribute("rollNo", student.getRollNo());
      
      return "welcome";
   }
}

welcome.jsp



  
    Spring MVC form handling example.
  
  
    

Student Details

Name ${name}
Class ${className}
RollNo ${rollNo}

Output:

Spring example5-1
Enter student detail.
Spring example5-2
Click on Submit button.
Spring example5-3
 
Download this example.