Spring MVC Hello World Example

Ads
 
Tutorials   

Spring MVC Hello World Example

Develop Hello World example in Spring MVC.

Develop Hello World example in Spring MVC.

Spring MVC Hello World Example

     

Spring MVC Hello World Example in Spring 2.5

In this tutorial we will develop the Spring MVC Example and then run on the Tomcat server.

We are using Spring 2.5 for this tutorial. We will also explain you the process of creating the dynamic web application in Eclipse IDE version 3.5.2. We will use tomcat to run the example. Eclipse provides the way to easily run the example from the IDE.

Step 1:

The initial step is to create Dynamic Web Project in eclipse. To create a new Dynamic project in Eclipse IDE select File -> Dynamic Web Project as shown below.

Spring MVC New Project

Step 2:

After that New Dynamic Project Window will appear on the screen and you will enter the web application name in the Project name text box. Enter Hello World in the Project Name and then click Next button.

Spring MVC New Project

We will enter the project name and set the project location and set the target runtime (Apache Tomcat server) just click on New button after that a New Server Runtime Environment window will appear, select the latest version of runtime environment and click Next button and Download and install the Tomcat Server and click on Finish button and also click on Finish button in Dynamic Web Project creation window.

Select Tomcat

Tomcat Server path

Step 3:

After that we will set the nature of the project just right click on the project and select Spring Tools with Add Spring Project Nature.

New Hello World Project

Step 4:

We will add all Spring Web MVC 2.5 jar files.

Adding Spring Libraries

After that Build Path window will appear on the IDE screen look like

Select Jar files

After that we will select the Libraries tab and click on Add External Jars button for add jar files. After selecting all Spring 2.5 Web MVC jar file we will click on Ok button and jar files is appears in your project lib folder.

Spring libraries in eclipse project

Now your project will created  in Eclipse IDE.

Step 5:

Now we will created a index.jsp in project's WebContent  folder. In the jsp file we will create a new hyperlink "Hello World !" that will be linked to hello.html page.  The code of index.jsp is:

<%@page contentType="text/html" pageEncoding="UTF-8"%>

<html>

<body>

<a href="hello.html">Hello World</a>

</body>

</html>

Step 6:

Now we will configure the DispatcherServlet in web.xml file. The DispatcherServlet will be configured to process all the request ending with .html.

Here is the code of configuring DispatcherServlet in web.xml file:

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

The full code of web.xml file is:

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="2.5"

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_5.xsd" >

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

<welcome-file-list>

<welcome-file>index.jsp</welcome-file>

</welcome-file-list>

</web-app>

Step 7:

Now we will create a new xml file called dispatcher-servlet.xml in the WEB-INF folder of the web application. This is the main configuration file for the Spring MVC. Here we will define the ViewResolver, the code for defining the view resolver is as shown below:

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

The view resolver looks for the jsp files in the /WEB-INF/jsp/ directory.

The we have to define bean for org.springframework.web.servlet.handler.SimpleUrlHandlerMapping  as shown below:
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">

Then we will define the url mapping with the Controller bean as shown below:

<map>
<entry key="/hello.html">
<ref bean="helloController"/>
</entry>
</map>

Finally we will define the Controller bean as shown below:

<bean id="helloController" class="net.roseindia.web.HelloWorldController"> </bean>

 

The full code of dispatcher-servlet.xml file is:

<?xml version="1.0" encoding="UTF-8"?>

<beans

xmlns="http://www.springframework.org/schema/beans"

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-2.5.xsd"

xmlns:p="http://www.springframework.org/schema/p">

<bean id="viewResolver"

class="org.springframework.web.servlet.view.InternalResourceViewResolver">

<property name="prefix">

<value>/WEB-INF/jsp/</value>

</property>

<property name="suffix">

<value>.jsp</value>

</property>

</bean>

<bean id="urlMapping"

class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">

<property name="interceptors">

<list>

<ref local="localeChangeInterceptor"/>

</list>

</property>

<property name="urlMap">

<map>

<entry key="/hello.html">

<ref bean="helloController"/>

</entry>

</map>

</property>

</bean>

<bean id="helloController" class="net.roseindia.web.HelloWorldController"> </bean>

<bean id="localeChangeInterceptor" class="
org.springframework.web.servlet.i18n.LocaleChangeInterceptor"
>

<property name="paramName" value="hl"/>

</bean>

<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver"/>

</beans>

Step 8:

After that we will creates the controller class in the project src folder. This will be used as the controller for the hello.html request. The code of the  HelloWorldController.java calss is:

package net.roseindia.web;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
 
public class HelloWorldController implements Controller {
 
 public ModelAndView handleRequest(HttpServletRequest request,
  HttpServletResponse responsethrows ServletException, IOException {
 
  String Mess = "Hello World!";
 
  ModelAndView modelAndView = new ModelAndView("hello");
  modelAndView.addObject("message", Mess);
 
  return modelAndView;
  }
}

After that we will create a hello.jsp in the WEB-INF folder by create a jsp folder. The WEB-INF/jsp/hello.jsp code is:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<html>

<body>

<c:out value="${message}"/>

</body>

</html>

Step 9:

To run the example select Run -> Run As -> Run On Server from the menu and then run the application on tomcat server. Eclipse will start Tomcat server and deploy the application on the Tomcat. Ellipse will also open the application in it internal browser as shown below:

Click on the "Hello World" link to test the application. It should display the "Hello World" message as shown below.

Download code:

Download this example code

You can download the code from the above url and then import the project in Eclipse IDE.

Ads

Ads