Spring MVC Hello World Tutorial with Example


STEP 1:- Open Eclipse and Create Dynamic Web Project named SpringMVCExample

STEP 2:- Make sure you use Target Runtime as Apache Tomcat 7.0. 

STEP 3:- copy below jars to WEB-INF/lib folder.
  • commons-logging-1.2.jar
  • spring-aop-4.1.4.RELEASE.jar
  • spring-beans-4.1.4.RELEASE.jar
  • spring-context-4.1.4.RELEASE.jar
  • spring-core-4.1.4.RELEASE.jar
  • spring-expression-4.1.4.RELEASE.jar
  • spring-web-4.1.4.RELEASE.jar
  • spring-webmvc-4.1.4.RELEASE.jar
STEP 4:- Create Spring Configuration Bean file. /WebContent/WEB-INF/dispatcher-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:mvc="http://www.springframework.org/schema/mvc"
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.xsd
 http://www.springframework.org/schema/mvc 
 http://www.springframework.org/schema/mvc/spring-mvc.xsd
 http://www.springframework.org/schema/context 
 http://www.springframework.org/schema/context/spring-context.xsd">
 
 <context:component-scan base-package="com.tutorialsdesk.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/views/" />
 <property name="suffix" value=".jsp" />
 </bean>
 
</beans>

In the above dispatcher-servlet.xml configuration file, we have defined a tag . This will allow Spring to load all the components from package com.tutorialsdesk.controller and all its child packages. This will load our SpringMVCHelloWorld.class . 

Also we have defined a bean viewResolver. This bean will resolve the view and add prefix string /WEB-INF/views/ and suffix .jsp to the view in ModelAndView. Note that in our SpringMVCHelloWorld.class, we have return a ModelAndView object with view name welcome. This will be resolved to path /WEB-INF/views/welcome.jsp . 

STEP 5 :- Map Spring MVC in /WebContent/WEB-INF/web.xml file as below :-
<?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"
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>SpringMVCExample</display-name>
 <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>

One thing to note here is the name of servlet in tag in web.xml. Once the DispatcherServlet is initialized, it will looks for a file name [servlet-name]-servlet.xml in WEB-INF folder of web application. In this example, the framework will look for file called dispatcher-servlet.xml. 

STEP 6 :- Create Controller Class.
  • Package: com.tutorialsdesk.controller
  • Filename: SpringMVCHelloWorld.java
package com.tutorialsdesk.controller;

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

@Controller
public class SpringMVCHelloWorld {

 @RequestMapping("/index")
 public ModelAndView helloWorld(){
 String message = "<br><div style='text-align:center;'>"
 + "<h3>********** Hello World, Spring MVC Example by
TutorialsDesk**********</h3>This message is coming from
SpringMVCHelloWorld.java</div><br><br>";
 return new ModelAndView("welcome", "message", message);
 }
 
}

Note that we have annotated the SpringMVCHelloWorld class with @Controller and @RequestMapping("/index"). When Spring scans our package, it will recognize this bean as being a Controller bean for processing requests. The @RequestMapping annotation tells Spring that this Controller should process all requests beginning with /index in the URL path. That includes /index/* and /index.html. 

As we have declared index.html in web.xml as welcome-file, it will routed to our SpringMVCHelloWorld controller. 

The helloWorld() method returns ModelAndView object. The ModelAndView object tries to resolve to a view named “welcome” and the data model is being passed back to the browser so we can access the data within the JSP. The logical view name will resolve to /WEB-INF/views/welcome.jsp . Logical name “welcome” which is return in ModelAndView object is mapped to path /WEB-INF/views/welcome.jsp. The ModelAndView object also contains a message with key “message” and Detailed value. This is the data that we are passing to our view. Normally this will be a value object in form of java bean that will contain the data to be displayed on our view. Here we are simply passing a string. 

STEP 7 :- Create /WebContent/WEB-INF/views/welcome.jsp file.
<html>
<head>
<title>Spring MVC Example by TutorialsDesk - Hello World Spring MVC
 Example</title>

</head>
<body>${message}
 
 <br>
 <br>
 <div style="font-family: verdana; padding: 10px; border-radius: 10px;
font-size: 12px; text-align:center;">
 
 Spring MCV Tutorial by <a
href="http://www.tutorialsdesk.com/">TutorialsDesk</a>.
 Click <a
 href="http://www.tutorialsdesk.com/search/label/Java"
 target="_blank">here</a> for all Java and <a
 href="http://www.tutorialsdesk.com/search/label/Spring-MVC"
target='_blank'>here</a>
 for all Spring MVC, Web Development examples.<br>
 </div>
</body>
</html>

STEP 8 :- Run your project enter below URL in your browser 
http://localhost:8080/SpringMVCExample 
or 
http://localhost:8080/SpringMVCExample/index.html 

Keep visiting TutorialsDesk for more tutorials and practical programming examples on Spring MVC. Hope we are able to explain you Spring MVC Hello World Example, if you have any questions or suggestions please write to us using contact us form.(Second Menu from top left). 

Please share us on social media if you like the tutorial.
SHARE
    Blogger Comment
    Facebook Comment