Spring MVC hello world tutorial

Spring mvc framework provides the facility to build flexible and loosely coupled web applications. MVC stands for Model-View-Controller design pattern which separates the business logic, presentation logic and navigation logic. Let us discuss spring mvc hello world example in eclipse.

Example Explanation:

Use http://localhost:8080/SpringMVCExample1/ url to start the application. When you click on “Say Hello” link, a request for sayHello.html will generate. Request will be handled by DispatcherServlet. It delegates the request to the HelloController controller. The HelloController controller resolve the request with help of RequestMapping annotation, executes the specific functionality and returns the ModelAndView object to the DispatcherServlet. The DispatcherServlet then take the help of InternalResourceViewResolver to get the actual view name. In our example it will return the “/WEB-INF/jsp/helloWorld.jsp”. The DispatcherServlet then insert the model data into view and render response.

Example:

index.jsp

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>Spring Hello World Example.</title>
  </head>
  <body>
    <a href="sayHello.html">Say Hello</a> 
  </body>
</html>

web.xml

<?xml version="1.0" encoding="UTF-8"?>  
<web-app 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">
 
   <servlet>
      <servlet-name>HelloWorld</servlet-name>
      <servlet-class>
         org.springframework.web.servlet.DispatcherServlet
      </servlet-class>
      <load-on-startup>1</load-on-startup>
   </servlet>
 
   <servlet-mapping>
      <servlet-name>HelloWorld</servlet-name>
      <url-pattern>*.html</url-pattern>
   </servlet-mapping>
 
</web-app>

HelloWorld-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>  
<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.javawithease.business" />
 
   <bean class=
     "org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name="prefix" value="/WEB-INF/jsp/" />
      <property name="suffix" value=".jsp" />
   </bean>
 
</beans>

HelloController.java

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
 
@Controller
public class HelloController {
   @RequestMapping("/sayHello")  
   public ModelAndView sayHello() {
      String message = "Spring MVC Hello World Example.";
      return new ModelAndView("helloWorld", "message", message);  
   }
}

helloWorld.jsp

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>Spring Hello World Example.</title>
  </head>
  <body>
    <h2>${message}</h2>
  </body>
</html>

Output:

Spring example1-1
Click on Say Hello link.
Spring example1-2
 
Download this example.