Spring 3 MVC hello world example in Eclipse

Spring is the most popular and widely used open source java application framework as of current date.  This blog will help you to create your very first Spring MVC hello world Application in Eclipse. In this part of our Spring tutorials series, we will try to create a simple Spring MVC application which would start with a welcome page displaying a link ?Say Hello?. On clicking this link, user will be redirected to another page displaying a message ?Hello World, Spring 3.0!?.
Open Java EE perspective in your Eclipse and create a new Dynamic Web Project. Project structure can be shown in fugure :


Now add all required jar files in lib folder of your application. These libraries are taken from spring-framework distribution.

WebContent > WEB-INF > lib

spring-hello-world-libraries

Now add following files one by one according to the application structure:

WebContent\WEB-INF\web.xml
This is the entry point for every web application, it tells the container about request handlers, welcome file and how the application is going to behave. A servlet named DispatcherServlet is registered with url pattern *.html , this configuration make sure that all the requests ending with .html are going to be handled by spring framework itself.
<?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">
   <display-name>Spring3MVC</display-name>
   <welcome-file-list>
      <welcome-file>index.jsp</welcome-file>
   </welcome-file-list>
   <servlet>
      <servlet-name>spring</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      <load-on-startup>1</load-on-startup>
   </servlet>
   <servlet-mapping>
      <servlet-name>spring</servlet-name>
      <url-pattern>*.html</url-pattern>
   </servlet-mapping>
</web-app>

WebContent\index.jsp
This is a simple jsp file that provides a link to hello.html , on clicked a url is requested and mapped with an appropriate controller.
<html>
   <head>
      <title>Java Guys | Home</title>
   </head>
   <body style="font-family:Segoe Print ">
      <center>
         <div > <a href="hello.html">Say Hello</a></div>
      </center>
   </body>
</html>

WebContent\WEB-INF\spring-servlet.xml
This is core of all spring applications , all configuration and implementation is done here. tag <context:component-scan>.is used to scan all content in package javaguys.tutorials.spring so that all controller can be made available to map exact url pattern. There is bean defined with id viewResolver , this is responsible to add suffix and prefix values to view names defined under ModelAndView object.
<?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/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsd">
   <context:component-scan base-package="javaguys.tutorials.spring" />
   <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>

src\javaguys\tutorials\spring\HelloWorldController.java
Actual work is done here as a request with /hello is arrived this controller is mapped and helloWorld() method is executed. A object of type ModelAndView is returned which takes two parameters "hello" is view name and a string value mapped to message key.
package javaguys.tutorials.spring;

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

@Controllerpublic
class HelloWorldController {
   @RequestMapping("/hello")
   public ModelAndView helloWorld() {
      String message = "Hello World, Spring 3.0!";
      return new ModelAndView("hello", "message", message);
   }
}

WebContent\WEB-INF\jsp\hello.jsp
This is a simple jsp  page that renders a string using expression ${message} the value of message is being set in controller.
<html>
   <head>
      <title>Java Guys | Home</title>
   </head>
   <body style="font-family:Segoe Print ">
      <center>
         <div > ${message}</a></div>
      </center>
   </body>
</html>
Now we are all done with our first Spring MVC application , run application on Server to see the Result .

spring-hello-world

spring-hello-world

In upcoming blogs we w'll see other concepts, tutorials, tips and tricks related to Struts2 , Spring and Hibernate Technologies .

Download "Struts3 MVC HelloWorld Example" from "SkyDrive"