In this tutorial we will create simple Spring 3 MVC application.
Technologies used:
- Spring 3.2.9.RELEASE
- JDK 1.6
- Maven 3.2.5 (the last version, that is compatible with Java 6)
- Eclipse
1. Project setup
We will use maven to create appplication from template:
>mvn archetype:generate -DgroupId=com.devcases-DartifactId=HelloWorldApp -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false
This command will create the appropriate directory structure:
CounterWebApp |-src |---main |-----resources |-----webapp |-------index.jsp |-------WEB-INF |---------web.xml |-pom.xml
We have to add another directory “java” under “main” directory
Next, we have to modify the default configuration in Maven pom.xml file (add necessary dependencies or remove unnecessary if any exists). The final file should looks like this:
<?xml version="1.0"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.devcases</groupId> <artifactId>HelloWorldApp</artifactId> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <name>HelloWorldApp Maven Webapp</name> <url>http://maven.apache.org</url> <properties> <spring.version>3.2.9.RELEASE</spring.version> </properties> <dependencies> <!-- Spring 3 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> <build> <finalName>HelloWorldApp</finalName> </build> </project>
To import maven project into eclipse, just use:
mvn eclipse:eclipse
This will create eclipse project, which you can import to eclipse.
2. Spring configuration
To integrate web application with Spring, in web.xml we have to declare DispatcherServlet:
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <display-name>Spring 3 MVC Web Application</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> </web-app>
DispatcherServlet is responsible for loading web related components (like controllers), if we would have any services or other middle-tier services, we should use ContextLoaderListener to load them.
Then we should create dispatcher-servlet.xml with the following content:
<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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="com.devcases.helloworldapp" /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix"> <value>/WEB-INF/views/</value> </property> <property name="suffix"> <value>.jsp</value> </property> </bean> </beans>
<context:component-scan> enables Spring to scan packages, find and register beans within the application context.
InternalResourceViewResolver resolves logical names of views to physical views.
Finally we will add controller HelloWorldController:
package com.devcases.helloworldapp; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class HelloWorldController { @RequestMapping("/welcome") public String hello(Model model) { model.addAttribute("message", "Spring 3 MVC Hello World Application"); return "hello"; } }
3. JSP View
Lets create hello.jsp page (in /WEB-INF/views directory) to display message:
<html> <body> <h1>Message : ${message}</h1> </body> </html>
4. Deploy and run
Finally we can build project with maven:
mvn clean install
Generated war we can deploy on server (for example Tomcat) and go to the url:
http://localhost:8080/HelloWorldApp/welcome
The response should be: