Spring MVC Hello World Example

Tuesday, July 6, 2010

I have struggled a lot to set up first Spring MVC example. Usually the first application is the toughest one but once you are there you can progress rapidly. In this post I will show you how to set up and run the first Spring MVC example in Eclipse.

Earlier I tried it on MyEclipse but struggled a lot and then went to Eclipse Europa version on which I finally was able to run the application. This example was created with Spring 3.0 M3 and the application server used is Tomcat 5.0

This example is based on another readymade example Spring MVC Fast Tutorial: Hello World . I started with that example so you will find similarities. I also bumped into many issues which I finally was able to resolve.

Apart from fixing issues, I have extended the example with a small form which submits the user name and that name is printed with the Hello in another JSP.

Little Theory First
In spring the request should be passed through Dispatcher servlet which is part of Spring API. This servlet is provided by Spring directly and we just need to map the url to this servlet. When a request is receive by the Spring enabled application, it is received by the Dispatcher Servlet. Dispatcher Servlet looks for the appropriate controller and the view for the request in the configuration file. The request is then sent to Controller which processes the business logic and forwards the request to the appropriate view.


Workspace Setup

It is important to have all the required files (jars, tld, xml) otherwise you will face numerous issues. I learnt the hard way about the files required for this example hence I am showing the workspace snap which will help you to have all the required files.

In the lib folder copy the jar files shown here. In the WEB-INF directory the tlds are required for jstl, though in this example c.tld is the only one used. So other tld files are optional.

There is another file springmvc-servlet.xml. This file is the spring configuration file which spring uses to determine the controller and view for serving a request. The name of the file is significant here. springmvc should be same as the springmvc which is mentioned for declaring the servlet in the web.xml file.








Here are rest of the files and the Code.
1. HelloWorldController.java
This file is the controller for the hello-world.html resource. This is a virtual resource in the sense this file does not exists. The controller maps the spring configuration maps it to /jsp/hello_world.jsp


package headfirstspring.controller;

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 response) throws ServletException, IOException {

ModelAndView modelAndView = new ModelAndView("hello_world");
return modelAndView;
}
}


2. DisplayDetailsController.java
This controller intercepts the request when user submits the form in first page (hello_world.jsp) and creates a message object and attaches it to the view.


package headfirstspring.controller;

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 DisplayDetailsController implements Controller {

public ModelAndView handleRequest(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {

String name = request.getParameter("name");
String aMessage = "Hello "+name;

ModelAndView modelAndView = new ModelAndView("display_details");
modelAndView.addObject("message", aMessage);

return modelAndView;
}
}


3. hello-world.jsp
This is the initial jsp and shows the form where in user types in a name and submits it.


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



Name :






4. display_details.jsp

This jsp shows the welcome message with name of the person submitted from the previous jsp.

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


Hello Welcome to Spring






5. springmvc-servlet.xml
This is the spring configuration file. This file declares the various controllers. In the view declaration notice how the resource is prefixed with /jsp and suffixed with .jsp.

For example if the resource is hello_world it will become /jsp/hello_world.jsp














6. web.xml





springmvc
org.springframework.web.servlet.DispatcherServlet
1



springmvc
*.html



springmvc
*.do




jsp/index.jsp





http://java.sun.com/jsp/jstl/fmt
/WEB-INF/fmt.tld


http://java.sun.com/jsp/jstl/core
/WEB-INF/c.tld


http://java.sun.com/jsp/jstl/sql
/WEB-INF/sql.tld


http://java.sun.com/jsp/jstl/x
/WEB-INF/x.tld





Now You can Run the Application after deploying it on Tomcat Server.


Here is what should be the response.

0 comments:

Post a Comment

  © Blogger template On The Road by Ourblogtemplates.com 2009

Back to TOP