Spring 3 MVC Example


 

Spring 3 MVC Example

Objective of this section is to demonstrate you how to create a simple MVC example in Spring.

Objective of this section is to demonstrate you how to create a simple MVC example in Spring.

Spring 3 MVC Example

In this tutorial we will read about how a spring application can be developed based on MVC architecture.

MVC architecture represents the Model View And Controller, is a design pattern. MVC facilitates to make the application in layers by dividing them into business, presentation and control flow. In the MVC design pattern Model is used to write the business logic, controller controls the interaction between model and view and view contains the presentation code. Among the three parts of MVC, controller manages the input of an application and passes to the model into the required format of the model. After processing the required operation on received input controller receives the response from model and it sends to the view for presentation.

DispatcherServlet

org.springframework.web.servlet.DispatcherServlet is a Servlet that acts as a controller that controls the dispatch of HTTP web requests. It takes the mapping information from the Spring configuration file (conventionally named as the <servlet-name>-servlet.xml, <servlet-name> is the value of element <servlet-name> in Servlet mapping of DispatcherServlet inside deployment descriptor file i.e. web.xml) situated inside the WEB-INF folder of the web application directory. You may also specify the name and location of Spring configuration file as you wish but, to do so you will have to explicitly specify the listener named org.springframework.web.context.ContextLoaderListener inside the web.xml file as follows :

<?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>dispatcher</servlet-name>
  <servlet-class>
     org.springframework.web.servlet.DispatcherServlet
  </servlet-class>
   <init-param>
     <param-name>contextConfigLocation</param-name>
     <param-value>/WEB-INF/springConfigFile/a-servlet.xml</param-value>
   </init-param>
  <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
   <servlet-name>dispatcher</servlet-name>
   <url-pattern>/</url-pattern>
</servlet-mapping> 

</web-app>

Servlet mapping of DispatcherServlet is required inside the web.xml file. You can see the snippet code for mapping DispatcherServlet in the example given below.

Example

Here I am giving a simple example which will demonstrate you about how to create a simple Spring MVC example. Here we will give you step-by-step description of how to create Spring MVC example. In this example we will use Eclipse Helios and Apache Tomcat 7 to compile and deploy the application.

Directory Structure

Below is the complete directory structure of the application :

ControllerClass.java

package net.roseindia.controller;

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

@Controller
@RequestMapping("/")
public class ControllerClass{
 
   @RequestMapping(method = RequestMethod.GET)
   public String displayMessage(ModelMap model) {
      model.addAttribute("message", "Spring Simple MVC Example");
      return "hello";
   }
}

dispatcher-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<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 ="net.roseindia.controller" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>

In the dispatcher-servlet.xml the element context:component-scan specifies that the annotated components will automatically scanned by the Spring container, started from the given package.

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>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>/</url-pattern>
</servlet-mapping> 

</web-app>

hello.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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 MVC Example</title>
</head>
<body>
<h2>${message}</h2>
</body>
</html>

Output

When you will execute the above application you will get the output as follows :

Download Source Code

Ads