In this article we will learn how to create a spring mvc simple example.
Please install / setup the below softwares to get started
1. JDK 1.7
2. Maven
3. Tomcat
I will be using eclipse IDE.
1. Create a Maven project in eclipse
Create New Project and select Maven project and click next
Uncheck the “Create a simple project” checkbox and click next
Filter the archetypes using “maven-archetype-webapp” and then select the available archetype
2. Edit the pom.xml to add spring dependencies
Once the maven project is created , now edit the pom.xml to add spring dependencies
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<properties> <spring.version>4.0.1.RELEASE</spring.version> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> </dependencies> |
3. Create a package structure and a controller class
I created a package named com.kscodes.sampleproject and a controller class named FirstController.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
package com.kscodes.sampleproject; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.servlet.ModelAndView; @Controller @RequestMapping("/firstpage") public class FirstController { @RequestMapping(method = RequestMethod.GET) public ModelAndView print() { ModelAndView mv = new ModelAndView("firstpage"); mv.addObject("displayMessage", "This is the first page of Spring MVC"); return mv; } } |
We have used the ModelAndView class that will return the view name along with the message that we will be displaying in the jsp page.
View name in the case is “firstpage”
4. Create a jsp folder and add a view file
Create a jsp folder inside the WEB-INF folder. Inside the jsp folder create firstpage.jsp file. This jsp will act as a view to display the message that we send through the FirstController.java
1 2 3 4 5 |
<html> <body> <h1>${displayMessage}</h1> </body> </html> |
5. Create a spring configuration file.
Create a spring configuration file named “dispatcher-servlet.xml”
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<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.kscodes.sampleproject" /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix"> <value>/WEB-INF/jsp/</value> </property> <property name="suffix"> <value>.jsp</value> </property> </bean> </beans> |
6. Modify the web.xml to create integrate spring.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
<web-app id="WebApp_ID" 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 Sample Project</display-name> <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> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/dispatcher-servlet.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> </web-app> |
Thats it. Build the project and deploy it in tomcat.