To understand Spring MVC, let's take the example of a simple hello world program.
Let us start our Hello World program with a controller. Any user request is first taken by the
DispatcherServlet. The DispatcherServlet reads the URL, and executes the appropriate method in
the controller. The class has been annotated with the @Controller annotation. This indicates that
this class is a spring mvc controller and it is capable of handling web requests. This class will
be recognized as a controller for processing requests, by the spring container when it scans the
packages.
The helloWorld() method has been annotated with a @RequestMapping annotation. This annotation tells
spring that this controller should process all the requests beginning with "/hello" in the URL. The
URL may include "/hello.html" or "/hello.jsp" etc. Instead of using "/hello" in the helloWorld() we
can use "/" as well. The helloWorld() method returns ModelAndView object. This object contains a message
with key "message" and its related value "Hello World!". This data is passed to the view.
File: com.ram.controller.HelloWorldController.java
package com.ram.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; @Controller public class HelloWorldController { @RequestMapping("/hello") public ModelAndView helloWorld(){ String message = "Hello World!"; return new ModelAndView("hello","message", message); } }
File: WEB-INF/jsp/hello.jsp
<html> <head> <title>Spring 3.0 Hello World</title> </head> <body> ${message} </body> </html>
This hello.jsp will display the value in the message that is passed by the controller.
File: WebContent/index.jsp
<html> <head> <title>Spring 3.0 Hello World</title> </head> <body> <a href="hello.html">Click here</a> </body> </html>
File: WEB-INF/web.xml
<?xml version="1.0" encoding="UTF-8"?> <vweb-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>Spring 3 MVC Hello World</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>*.html</url-pattern> </servlet-mapping> </web-app>
In the "web.xml" we define DispatcherServlet which acts as front controller or the entry
point for the request.
File: WEB-INF/dispatcher-servlet.xml
<?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:p="http://www.springframework.org/schema/p" 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="com.ram.controller" /> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp" /> </bean> </beans>
In the "dispatcher-servlet.xml" file context:component-scan which will allow spring to
load all the components from the package given. Next comes the bean id "view resolver".
ViewResolver decides which View is responsible for rendering the response and also adds
prefix and suffix.
Run the project "SpringMVCHelloWorld" and you get output in the browser as shown below. Click on
"Click here" and "Hello World!" message is displayed.
No comments:
Post a Comment