Spring MVC 4 Example
Spring MVC 4 Example explains step by step example of configuring Spring MVC with eclipse
Spring version 4.0 provides full support for the latest Java 8 features.
Spring MVC is a popular Java Model-View-Contraller (MVC) framework. Basically spring is request based framework, which revolve around DispatcherServlet that dispatches the requests with the help of different lifecycle interfaces
Spring MVC have following important interfaces to handle a request during the execution phases of a HTTP request.
- HandlerMapping
- HandlerAdapter
- Controller
- View
- ViewResolver
- HandlerInterceptor
- LocaleResolver
- MultipartResolver
We can also use annotations like @RequestMapping,@Controller etc. for getting maximum flexibility
Required Libraries
Following jar must be in classpath
- commons-logging-1.1.1.jar
- spring-aop-4.0.5.RELEASE.jar
- spring-beans-4.0.5.RELEASE.jar
- spring-context-4.0.5.RELEASE.jar
- spring-core-4.0.5.RELEASE.jar
- spring-expression-4.0.5.RELEASE.jar
- spring-web-4.0.5.RELEASE.jar
- spring-webmvc-4.0.5.RELEASE.jar
You can see the project structure below
studentForm.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%> <html> <head> <title>Spring MVC Form Handling</title> </head> <h2>Student Data Form</h2> <form:form action="addStudent" method="POST"> <table> <tbody> <tr> <td><form:label path="studId">Student ID:</form:label></td> <td><form:input path="studId"></form:input></td> </tr> <tr> <td><form:label path="name">StudentName :</form:label></td> <td><form:input path="name"></form:input></td> </tr> <tr> <td><form:label path="age">Student Age:</form:label></td> <td><form:input path="age"></form:input></td> </tr> <tr> <td colspan="2"><input type="submit" value="Submit" /> </td> </tr> </tbody> </table> </form:form> </body> </html>
studentDetail.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%> <html> <head> <title>Spring MVC Form Handling</title> </head> <body> <h2>Submitted Student Information</h2> <table border="1"> <tbody> <tr> <td>Student ID</td> <td>${studId}</td> </tr> <tr> <td>Student Name</td> <td>${name}</td> </tr> <tr> <td>Student Age</td> <td>${age}</td> </tr> </tbody> </table> </body> </html>
Student.java
public class Student {
private int studId;
private String name;
private int age;
public int getStudId() {
return studId;
}
public void setStudId(int studId) {
this.studId = studId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
StudentController.java
In StudentController class, we have added @Controller annotation which indicates that an annotated class is a "Controller" (e.g. a web controller)
@RequestMapping annotation is used for mapping web requests to corresponding handler classes and handler methods
@ModelAttribute annotation is used for backing up the model object and persisted across requests
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;
import com.student.bean.Student;
@Controller
public class StudentController {
@RequestMapping(value = "/student", method = RequestMethod.GET)
public ModelAndView student() {
return new ModelAndView("studentForm", "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("age", student.getAge());
model.addAttribute("studId", student.getStudId());
return "studentDetail";
}
}
spring-servlet.xml
The name for spring-servlet.xml is must match the servlet-name we have provided on web.xml. In our case we have provided servlet-name as spring, so we need to append "-servlet.xml"
Below code is used for Spring auto detection feature to find the packages where the controller is defined
<context:component-scan base-package="com.student.controller" />
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> <context:component-scan base-package="com.student.controller" /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="viewResolver"> <property name="prefix" value="/WEB-INF/views/"></property> <property name="suffix" value=".jsp"></property> </bean> </beans>
web.xml
<?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/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>SpringMVCExample</display-name> <welcome-file-list> <welcome-file>/</welcome-file> </welcome-file-list> <servlet> <servlet-name>spring</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> </servlet> <servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
Run