Introduction - Spring Framework is a lightweight alternative to Java EE's complexity. Designed around a DispatcherServlet that dispatches requests to handlers, with configurable handler mappings, view resolution, locale and theme resolution as well as support for uploading files. I don't want you to read another bunch of lines and make you bore instead look at the flow diagram below which will explain the working of Spring MVC.
Image Source - Spring MVC Work Flow
Lets develop a small Hello World Program using Spring MVC 3
Jars Needed
commons-logging-1.0.4.jar
jstl-1.2.jar
org.springframework.asm-3.0.1.RELEASE-A.jar
org.springframework.beans-3.0.1.RELEASE-A.jar
org.springframework.context-3.0.1.RELEASE-A.jar
org.springframework.context-sources-3.0.1.RELEASE-A.jar
org.springframework.core-3.0.1.RELEASE-A.jar
org.springframework.expression-3.0.1.RELEASE-A.jar
org.springframework.jdbc-3.0.1.RELEASE-A.jar
org.springframework.web-3.0.1.RELEASE-A.jar
org.springframework.web.servlet-3.0.1.RELEASE-A.jar
web.xml
Define a servlet and named it as dispatcher. You can name it whatever you want.
<?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>helloworld</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <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>/</url-pattern> </servlet-mapping> </web-app>
dispatcher-servlet.xml
Create an xml and name it as <name of the dispatcher servet>-servlet.xml. This is the front controller which will handle all your request coming from web.xml. Since we have defined the url mappings above. Thus any request after '/' will be delegated to this dispatcher-servlet.xml.
With Spring MVC 3 we are using the power of annotations. <context:component-scan base-package="controller" /> will look out for the controller package (or you can name it whatever u want like handler) where it can map the incoming request using @RequestMapping annotation.
Define bean viewResolver which will show the jsp as per the request sent from controller.
<?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:flow="http://www.springframework.org/schema/webflow-config" xmlns:context="http://www.springframework.org/schema/context" 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="controller" /> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix"><value>/jsp/</value></property> <property name="suffix"><value>.jsp</value></property> </bean> </beans>
controller class
With the controller class we are using ModelAndView object to return the view name which is to be displayed. You can see that using the annotation we have mapped the incoming request to handleLoginRequest() method.
package controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; @Controller public class LoginController { @RequestMapping("/login") public ModelAndView handleLoginRequest() { return new ModelAndView("login"); } }
jsp
Create a jsp and name it as login.jsp and provide your content whatever you want to display.
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Suncity Health Club - Login </title> </head> <body> Hi Ankit !! Welcome to Spring MVC </body> </html>
output
open your browser and type http://localhost:8080/<projectName>/login
Also I have added a snapshot of folder hierarchy of this project so that it would be helpful for a newbie.
This is a small example. In the next post, I will explain the basics and advantages of Spring MVC.
Happy coding :)
Happy coding :)
