Spring Maven Hello World

Spring Maven Hello World

This tutorial will show you how to run spring hello world using Maven. We will use spring latest version as of today (08/11/2014) is 4.0.6.RELEASE.

Below are required:

After all all set-up you will see below welcome page:

Spring Maven Hello World

Please follow below steps:

Spring Maven Hello World

  • Copy paste below pom.xml file:
<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.javahonk</groupId>
	<artifactId>SpringMVCAngularJS</artifactId>
	<packaging>war</packaging>
	<version>0.0.1-SNAPSHOT</version>
	<name>SpringMVCAngularJS Maven Webapp</name>
	<url>http://maven.apache.org</url>
	
	<properties>
		<SpringVersion>4.0.6.RELEASE</SpringVersion>
	</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>${SpringVersion}</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>${SpringVersion}</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>${SpringVersion}</version>
		</dependency>
	</dependencies>
	<build>
		<finalName>SpringMVCAngularJS</finalName>
	</build>
</project>

 

  • Copy paste web.xml file:
<?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_2_5.xsd" id="WebApp_ID" version="2.5">
	<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>*.web</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>

	<welcome-file-list>	 
	   <welcome-file>helloWorld.web</welcome-file>
	</welcome-file-list>

</web-app>

 

  • Create dispatcher-servlet.xml inside WEB-INF folder and copy paste below code:
<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.javahonk.controller" />

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

 

  • Create jsp folder inside WEB-INF folder.
  • Create helloWorld.jsp inside jsp folder and copy paste below code:
<%@ 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 3 MVC</title>
</head>
<body>
<h2>Spring MVC 4  Maven welcome message: ${message}</h2>
</body>
</html>

 

  • Create controller class SpringMVCController.java inside com.javahonk.controller package and copy paste below code:
package com.javahonk.controller;

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

@Controller
public class SpringMVCController {
	@RequestMapping(value="/helloWorld.web",method = RequestMethod.GET)
	public String printWelcome(ModelMap model) {

		model.addAttribute("message", "Hello World!!!");
		return "helloWorld";

	}
}

 

  • Please configure tomcat server in eclipse using this tutorial. Now we are all set to run this project. To Run: Right click project –> Run As –> Run on Server you will see below welcome page:

Spring Maven Hello World

download Download Project: SpringMVCAngularJS

2 thoughts on “Spring Maven Hello World”
  1. I followed same steps but I am getting following error,
    ————————————————————————–
    HTTP Status 404 – /SpringMVCAngularJS/helloWorld.jsp
    type Status report
    message /SpringMVCAngularJS/helloWorld.jsp
    description The requested resource is not available.
    ————————————————————————–

    Requesting to help on the same

  2. ignore previous error message as I was trying things and was checking randomly
    please find below error message
    ——————————————————-
    HTTP Status 404 –
    type Status report
    message
    description The requested resource is not available.
    ——————————————————-

Leave a Reply

Your email address will not be published. Required fields are marked *