Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Spring Spring3.2 Spring 3.2 MVC Hello World Example

Related Tutorials


 
 

Share on Google+Share on Google+

Spring 3.2 MVC Hello World Example

Advertisement
In this section, you will learn about Hello World example in Spring 3.2.

Spring 3.2 MVC Hello World Example

In this section, you will learn about Hello World example in Spring 3.2.

Spring 3.2 is a open source application framework based on Java platform. Spring MVC is the Spring's web framework.
Given below a basic tutorial for displaying a "hello world" message in Spring 3.2. This will help you understanding the basic configuration for Spring MVC :

The project structure of this example is given below :

The list of the jar file used in this example is given below :

  • commons-logging-1.1.1.jar

  • jstl-1.2.jar

  • spring-beans-3.2.0.RELEASE.jar

  • spring-context-3.2.0.RELEASE.jar

  • spring-core-3.2.0.RELEASE.jar

  • spring-expression-3.2.0.RELEASE.jar

  • spring-web-3.2.0.RELEASE.jar

  • spring-webmvc-3.2.0.RELEASE.jar

First you need to configure web.xml  as follows :

web.xml( Spring3.2HelloWorld/WebContent/WEB-INF/web.xml )

<?xml version="1.0" encoding="UTF-8"?>
<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_3_0.xsd"
version="3.0">
	<display-name>Spring3.2HelloWorld</display-name>
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
	
	<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>*.html</url-pattern>
	</servlet-mapping>

</web-app>

As you can see the welcome file is "index.jsp", it means it will be the first page, which will show to you. The DispatcherServlet is configured  with <servlet-name> "dispatcher". You can choose any name for <servlet-name>. Once it will initialize, it will search for a configuration file   <servlet-name>-servlet.xml or dispatcher-servlet.xml in WEB-INF folder of web application. This fill is necessary to configure your MVC application. The dispatcher-servlet.xml used in this example is given below :

dispatcher-servlet.xml(  Spring3.2HelloWorld/WebContent/WEB-INF/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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
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="net.roseindia.spring3point2.controller" />
	
	<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
		<property name="viewClass"
		value="org.springframework.web.servlet.view.JstlView" />
		<property name="prefix" value="/WEB-INF/jsp/" />
		<property name="suffix" value=".jsp" />
	</bean>
</beans>

The <context:component-scan> tag scans the classpath for annotated components that will be auto-registered as Spring beans. UrlBasedViewResolver resolver is configured to resolve the views.

The first page that will appear to you is "index.jsp", its code is given below :

<%@ 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 or Home Page</title>
</head>
<body>
<a href="welcome/hello.html"><i>Click here to print message</i></a>
</body>
</html>

All the request having URL pattern "*.html" is handled by the controller HelloWorldController.java, the code of this controller is given below :

HelloWorldController.java(Spring3.2HelloWorld/src/net/roseindia/spring3point2/controller/HelloWorldController.java)

package net.roseindia.spring3point2.controller;

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

@Controller
@RequestMapping("/welcome")
public class HelloWorldController {
	@RequestMapping("/hello.html")
	public String sayHello(ModelMap model) {
		model.addAttribute("message", "Spring 3.2 MVC Hello World");
		return "hello";	
	}
}

In the above controller, you can see that the request through URL "/welcome/hello.html" is handled by the method sayHello(). This method will return string "hello" which is resolved by the view resolver configured in dispatcher-servlet.xml and it will look for hello.jsp file in "/WEB-INF/jsp/hello.jsp".

The code for the hello.jsp is given below :

hello.jsp(Spring3.2HelloWorld/WebContent/WEB-INF/jsp/hello.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>Spring 3.2 Hello World</title>
</head>
<body>
<h2><i>${message}</i></h2>
</body>
</html>

OUTPUT

When you execute your application, you will get the following page first :

When you click on the hyperlink, you will get the following page with message :

You can download the source code without jar file using below link.

Download Source Code

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: February 7, 2013

Related Tutorials

Discuss: Spring 3.2 MVC Hello World Example  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:1
Paulo
March 12, 2013
Hello World

great!
DMCA.com