Home > AOP, J2EE, Spring, XML > Spring MVC – Hello World

Spring MVC – Hello World

Spring mvc is a part of Spring Core framework. Here is how spring mvc behaves.spring3mvc

A separate dispatcher servlet is used for handling the requests and responses. Here are the technologies i have used.

  • Maven 3
  • Tomcat 7.0
  • Eclipse Indigo 3.7

First, create a sample webapp using maven.

mvn archetype:generate -DgroupId=com.supun.spring -DartifactId=Spring3MVC  -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false .

Now , add the following dependencies to the 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.supun.springmvc</groupId>
 <artifactId>Spring3MVC</artifactId>
 <packaging>war</packaging>
 <version>1.0-SNAPSHOT</version>
 <name>Spring3MVC Maven Webapp</name>
 <url>http://maven.apache.org</url>
 <properties>
 <spring.version>3.0.5.RELEASE</spring.version>
 </properties>
 <dependencies>
 <dependency>
 <groupId>junit</groupId>
 <artifactId>junit</artifactId>
 <version>3.8.1</version>
 <scope>test</scope>
 </dependency>
 <!-- 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>Spring3MVC</finalName>
 </build>
</project>

Then convert it to a eclipse project using mvn eclipse:eclipse -Dwtpversion=2.0. THen import it to eclipse as an existing maven project.Now add new source folder into src/main called java. Now create your controller as follows.


package com.supun.common.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
@RequestMapping("/welcome")
public class HelloController {

 @RequestMapping(method=RequestMethod.GET)
 public String printWelcome(ModelMap model){
 model.addAttribute("message","Spring 3 MVC Hello World!");
 return "hello";
 }

}

Now create mvc-dispatcher-servlet.xml inside the WEB-INF folder.

<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.supun.common.controller" />

 <bean
 class="org.springframework.web.servlet.view.InternalResourceViewResolver">
 <property name="prefix">
 <value>/WEB-INF/pages/</value>
 </property>
 <property name="suffix">
 <value>.jsp</value>
 </property>
 </bean>

</beans>

Then add hello.jsp into WEB-INF/pages folder. Add the following content to it.

<h1>Message : ${message}</h1>

Add index.jsp to WEB-INF folder with the following.


<!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>Insert title here</title>
</head>
<body>
This is hme page
</html>
</body>

Finally edit the web.xml as follows.


<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 Application</display-name>

 <servlet>
 <servlet-name>mvc-dispatcher</servlet-name>
 <servlet-class>
 org.springframework.web.servlet.DispatcherServlet
 </servlet-class>
 <load-on-startup>1</load-on-startup>
 </servlet>

 <servlet-mapping>
 <servlet-name>mvc-dispatcher</servlet-name>
 <url-pattern>/</url-pattern>
 </servlet-mapping>

 <context-param>
 <param-name>contextConfigLocation</param-name>
 <param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
 </context-param>

 <listener>
 <listener-class>
 org.springframework.web.context.ContextLoaderListener
 </listener-class>
 </listener>
</web-app>

Now run the application in tomcat. Go to

http://localhost:8080/Spring3MVC/

Here is the output.

Now goto 

http://localhost:8080/Spring3MVC/welcome

Here is the out put.

Advertisements
Categories: AOP, J2EE, Spring, XML
  1. July 9, 2013 at 11:30 pm

    wonderful, finally i got working Maven and Spring MVC tutorial, thanks bro

  1. July 10, 2013 at 1:27 am

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

%d bloggers like this: