How to create a web application in Spring MVC

The Spring web MVC framework is based on MVC design pattern.Spring Web MVC uses Distpathcer Servlet as FrontController. It dispatches the requests to the handlers with the configurable handler mappings.

Dispatcher Servlet manages the flow of the application. @Controller annotation is used to mark a Class as Controller which acts as handler to process the requests.

The following jars need to be add to the WEB-INF/lib.

  • Spring Core module jar files
  • Spring Web module jar files
  • JSTL jar

Dowload Jars

Step 1:Create a Dynamic web project using Eclipse IDE.
Step 2:Add the above jars to lib directory.
Step 3:Make the following entry in web.xml for Dispatcher Servlet.


<web-app id="StartWithJava" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Spring MVC Application</display-name>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

Now we need to add spring web configuration and the file name as below.

dispatcherservletname-servlet.xml

In our case we have name spring for dispatcher servlet so the file will be spring-servlet.xml under WEB-INF directory.


<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" 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-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="com.startwithjava" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>

Step 4: Now define a controller class using @Controller annotation.@RequestMapping is used to bind a request to a handler class or method.

TestController.java


@Controller
public class CalculateController{
   @RequestMapping(value="/sum",method = RequestMethod.GET)
   public String sum(ModelMap model,HttpServletRequest request) {

     int num1 = Integer.parseInt(request.getParameter("num1"));
int num2 = Integer.parseInt(request.getParameter("num2"));

     int result = num1+num2;
      model.addAttribute("result",result );
      return "result";
   }
}

index.jsp


<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Spring Web MVC by StartWithJava</title>
</head>
<body>


<form action="sum" method="post">

   <input type="text" name="num1">

   <input type="text" name="num1">

   <input type="submit" name="Add" Value="Add">

</form>


</body>
</html>

result.jsp


<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Result Page</title>
</head>
<body>
Sum =${result}
</body>
</html>

Leave a Reply