Basic Spring MVC Project By Maven

basic-spring-mvc-maven


aim

Any one who are in java world know more or less about Spring MVC. In this article we will create very basic Spring MVC project by Maven. This is just a quick start guide for those who want to starts Spring MVC. However learning Maven is not the scope of this article.

[example title=”Prerequisites “]
To follow this article you should have basic knowledge of Java Web Application and Maven
[/example]


Step 1: Open your command prompt and execute the below command

[example title=”All Should Go in Single Line”]
mvn archetype:generate
-DgroupId=com.proliferay
-DartifactId=HelloWorldSpring
-DarchetypeArtifactId=maven-archetype-webapp
-DinteractiveMode=false
[/example]

Note 1: Once the above command is executed it will create below project structure

hello-world-spring-project-structure

Note 2: The above project is a simple java web application and its not a spring project. Below steps will make it Spring MVC project

Step 2: Add Spring Dependency in pom.xml

[example title=”pom.xml”]
<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.proliferay</groupId>
<artifactId>HelloWorldSpring</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>HelloWorldSpring Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<spring.version>4.0.1.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!– Spring 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>HelloWorldSpring</finalName>
</build>
</project>

[/example]

Step 3: Configure dispatcher servlet in web.xml

[example title=”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”
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>Spring Hello World Example</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>
[/example]

Note 3: Every Spring MVC Project must have DispatcherServlet. Its a front controller in Spring Framework. Every requests goes through this front controller.

Note 4: Look back Step 3. We have given name of the dispatcher servlet as dispatcher. So by default it will try to load dispatcher-servlet.xml file under WEB-INF directory. Similarly if the name of the dispatcher servlet is foo then it will try to load foo-servlet.xml and so on.

Step 4: Create dispatcher-servlet.xml in WEB-INF directory

[example title=”dispatcher-servlet.xml”]
<?xml version=”1.0″ encoding=”UTF-8″?>
<beans xmlns=”http://www.springframework.org/schema/beans”
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xmlns:context=”http://www.springframework.org/schema/context”
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”>
<context:component-scan base-package=”com.proliferay.controller” />

<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>
[/example]

Note 5: In Spring controller we generally return logical name of the views. For example if a controller return a string “hello” then spring will try to resolve this logical view to physical file. On the basis of the above configuration in Step 4 it will look for hello.jsp under /WEB-INF/views. Similarly if the controller returns a string “helloworld” then it will look for helloworld.jsp /WEB-INF/views and so on.

Step 5: Create views folder under WEB-INF. The views folder contains helloworld.jsp

[example title=”helloworld.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>HelloWorld Page</title>
</head>
<body>
<center>
<h2>Hello World Page</h2>
</center>
</body>
</html>
[/example]

Step 6: Add  index page under webapp

[example title=”index.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>Index Page</title>
</head>
<body>
<center>
<h2>Hello World</h2>
<h3>
<a href=”hello”>Show Hello World Page</a>
</h3>
</center>
</body>
</html>
[/example]

Step 7: Add the controller

[example title=”HelloWorldController.java”]
package com.proliferay.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/**
*
* @author Hamidul Islam
*
*/

@Controller

public class HelloWorldController {

@RequestMapping(“/hello”)
public String showHelloPage() {
System.out.println(“#############showHelloPage method#############”);
/**
* Spring will display helloworld.jsp under /WEB-INF/views
*/

return “helloworld”;
}
}
[/example]

Running the example:

Download the source code from the below link and create the war file of the project by below command

mvn clean package

Download Source Code

About The Author

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top
%d