Spring MVC Hello World Example

This post will describe step by step process of creating spring MVC project in eclipse with following technologies:

  • Eclipse Mars (4.5.0)
  • Java 1.8
  • Maven
  • Spring framework (core and web)

Following are the steps to create web based project with spring MVC:

Create Maven project in eclipse

First step to create maven based project with Archetype web and update pom.xml file as below:

<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>spring</groupId>
	<artifactId>mvc</artifactId>
	<packaging>war</packaging>
	<version>0.0.1-SNAPSHOT</version>
	<name>Spring MVC</name>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<spring.version>4.3.2.RELEASE</spring.version>
		<servlet.version>3.0.1</servlet.version>
	</properties>

	<dependencies>
		<!-- Spring MVC -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>${spring.version}</version>
		</dependency>

		<!-- Servlet API -->
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
			<version>${servlet.version}</version>
		</dependency>
	</dependencies>
	<build>
		<finalName>mvc</finalName>
	</build>
</project>

Update web.xml

Update web.xml file under webapp/WEB-INF/web.xml to configure dispatcher servlet as below:

<?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" version="3.0">
	<!-- Dispatcher servlet configuration -->
	<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>*.mvc</url-pattern>
	</servlet-mapping>

	<display-name>Archetype Created Web Application</display-name>
</web-app>

DispatcherServlet is a root servlet to receive all the request from client and delegate request to appropriate controllers according to URL.

dispatcher is a root servlet name and spring will look the configuration with the same name under webapp/WEB-INF/dispatcher-servlet.xml.

Create dispatcher-servlet.xml

Create same file under webapp/WEB-INF/ directory as below:

<?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">

	<!--Scan all the spring components including services, controllers etc. -->
	<context:component-scan base-package="com.threadminions" />
	<context:annotation-config />

	<!-- Resolver configuration to resolve a view -->
	<bean 		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="WEB-INF/html/" />
		<property name="suffix" value=".html" />
	</bean>
</beans>

Here, we have defined InternalResourceViewResolver which resolves view for a request and we have defined prefix as WEB-INF/html/ and suffix as .html which means that all the views should be in WEB-INF/html folder and every view will have suffix .html.

Create controllers

Create a package name com.threadminions.controllers and create a following class in this.

package com.threadminions.controllers;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class ExampleController {

	/***
	 * Returns a String message
	 */
	@RequestMapping(value = "getMessage", method =RequestMethod.GET)
	public @ResponseBody String getMessage()
	{
		return "Spring MVC Hello World Example";
	}

	/***
	 * Returns a view which is basically a HTML page
	 */
	@RequestMapping(value = "getHelloWorld", method = RequestMethod.GET)
	public ModelAndView getHelloWorld()
	{
		return new ModelAndView("hello_world");
	}
}

In above code, there are two request mappings getMessage and getHelloWorld which returns a string message and a html view respectively.

In getHelloWorld mapping, it returns a model namely hello_world which will be resolved by view resolver and spring will look this view as hello_world.html in WEB-INF/html directory.

Create HTML views

Create hello_world.html view under webapp/WEB-INF/ directory as below:

<!DOCTYPE html>
<html>
<head>
<title>Spring MVC</title>
</head>
<body>
<h1>Welcome to Spring MVC world</h1>
</body>
</html>

This is all done for web based hello_world application in spring framework.

Run this project
Right click on project -> Run As -> Run on server using tomcat.

After running,

Hit http://localhost:port_number/mvc/getMessage.mvc on your local browser and you will see the following output:

spring_message

And to get the view, hit http://localhost:port_number/mvc/getHelloWorld.mvc and you will see following output:

spring_mvc

This is all about how to create spring mvc program in eclipse.

 

One thought on “Spring MVC Hello World Example

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s