Spring MVC Tutorial - Create Hello World Example

In this tutorial, we will create a small web application using Spring MVC hello world example to show how to setup and develop Spring MVC based applications. See Setup development environment using Eclipse and Tomcat article in order to setup development environment for Spring MVC using eclipse and tomcat.

Spring's web MVC framework is, like many other web MVC frameworks, request-driven, designed around a central servlet that dispatches requests to controllers and offers other functionality facilitating the development of web applications.

You would need following jars in your classpath (i.e WEB-INF/lib) in order to run this example.
  • org.springframework.aop-3.1.1.RELEASE.jar
  • org.springframework.asm-3.1.1.RELEASE.jar
  • org.springframework.aspects-3.1.1.RELEASE.jar
  • org.springframework.beans-3.1.1.RELEASE.jar
  • org.springframework.context.support-3.1.1.RELEASE.jar
  • org.springframework.context-3.1.1.RELEASE.jar
  • org.springframework.core-3.1.1.RELEASE.jar
  • org.springframework.expression-3.1.1.RELEASE.jar
  • org.springframework.web.servlet-3.1.1.RELEASE.jar
  • org.springframework.web-3.1.1.RELEASE.jar
  • slf4j-api-1.5.0.jar
  • slf4j-log4j12-1.5.0.jar
  • log4j-1.2.15.jar
The DispatcherServlet is an actual Servlet (it inherits from the HttpServlet base class), and as such is declared in the web.xml of your web application. Requests that you want the DispatcherServlet to handle will have to be mapped using a URL mapping in the same web.xml file. This is standard J2EE servlet configuration; an example of such a DispatcherServlet declaration and mapping can be found below.


Let's create web.xml under WEB-INF directory to declare and configure DispatcherServlet.


In the example above, all requests ending with .do will be handled by the 'example' DispatcherServlet.This is only the first step in setting up Spring Web MVC.

The framework will, on initialization of a DispatcherServlet, look for a file named [servlet-name]-servlet.xml in the WEB-INF directory of your web application and create the beans defined there. With the above servlet configuration in place, you will need to have a file called '/WEB-INF/example-servlet.xml' in your application; this file will contain all of your Spring Web MVC-specific components (beans).


Let's create example-servlet.xml under WEB-INF directory.

<beans xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans      http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context    http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
<!-- This will enable annotations -->
<context:annotation-config />
<!-- This will scan all classes with this and its sub packages for any annotation. For this example, it will scan classes for @Controller annoation -->
<context:component-scan base-package="com.mycompany.myapp.controller" />
<!--  This would look for jsps under WEB-INF/jsp folder. Provide a name with path relative to this path while creating ModelAndView in controller-->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" >        <property name="prefix">
        <property name="suffix">
example of login.jsp (put this file under WEB-INF/jsp folder)

Let's develop a sample login jsp which will submit a POST request.

    <title>Spring 3 MVC Series - Login</title>
<form method="post" action="login.do"  >
        <td>Username :</td>
input type="text" name="username" /></td>
        <td>Password : </td>
        <td><input type="text" name="password" /></td>
        <td colspan="2">
            <input type="submit" value="Login"/>

example of Controller

Lets' develop a sample login controller which will handle a POST request when submit button is clicked on login.jsp

package com.mycompany.myapp.controller;

import javax.servlet.http.HttpServletRequest;

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

public class LoginController {

  // This method will be invoked when user submits the post request with url ending with login.do
 // value parameter in this annotation is relative to the context path
 protected ModelAndView showLogin(HttpServletRequest httpRequest){
  // Use above variables in loginService in order to authenticate the user
  String userName = httpRequest.getParameter("username");
  String password = httpRequest.getParameter("password");
  //Directs the user to home.jsp on successful authentication 
  ModelAndView model = new ModelAndView("home");
  return model;

And that's it. Please open the url http://{hostname}:{port}/{contextname}/jsp/login.jsp. Provide username/password and press login button. This would invoke LoginController and would take you to home.jsp (create your home.jsp under WEB-INF/jsp folder)

No comments:

Post a Comment