Spring MVC Development - Quick Tutorial

[复制链接]
查看11 | 回复7 | 2007-1-24 12:56:49 | 显示全部楼层 |阅读模式
This is a short tutorial on Developing web applications with Spring from Manoj at “The Khangaonkar Report”, one of our JCG partners.
(NOTE: The original post has been slightly edited to improve readability)
Spring MVC enables easy web application development with a framework based on the Model View Controller architecture (MVC) pattern. The MVC architectural pattern requires the separation of the user interface (View), the data being processed (Model) and the Controller which manages the interactions between the view and the model.
At the core of Spring MVC is a servlet, the DispatcherServlet, that handles every request. The DispatcherServlet routes the HTTP request to a Controller class authored by the application developer. The controller class handles the request and decides which view should be displayed to the user as part of the response.
Let us develop a simple web application that takes a request and sends some data back to the user. Before you proceed any further, I recommend you download the source code from here.
For this tutorial you will also need:
1. A servlet container like Tomcat
2. Spring 3.0
3. Eclipse is optional. I use eclipse as my IDE. Eclipse lets you export the war that can be deployed to Tomcat. But you can use other IDEs or command line tools as well.
4. Some familiarity with JSPs and Servlets is required.
回复

使用道具 举报

千问 | 2007-1-24 12:56:49 | 显示全部楼层
Step 1: If you were to develop a web application in J2EE, typically you do it by developing servlets and/or JSPs, that are packaged in a .war file. Also necessary is a deployment descriptor web.xml that contains configuration metadata. The war is deployed to an application server like Tomcat.
With Spring, the first thing to do is to wire Spring to this J2EE web infrastructure by defining org.springframework.web.servlet.DispatcherServlet as the servlet class for this application. You also need to define org.springframework.web.context.ContextLoaderListener as a listener. ContextLoaderListener is responsible for loading the spring specific application context which has Spring metadata.
The web.xml setup ensures that every request to the application is routed by the servlet engine to DipatcherServlet. The updated to web.xml is shown below:
view source
print?
01

02

03
org.springframework.web.context.ContextLoaderListener
04

05

06

07
springmvc
08

09
org.springframework.web.servlet.DispatcherServlet
10

11
1
12

13

14
springmvc
15
*.htm
16

回复

使用道具 举报

千问 | 2007-1-24 12:56:49 | 显示全部楼层
Step 2: The heavy lifting in this web application is done by a controller class. This is an ordinary java class or bean that extends org.springframework.web.servlet.mvc.AbstractController. We override the handleRequestInternal method. In this method, you would do the things necessary to handle the request which may include for example reading from a database.
The method returns a org.springframework.web.servlet.ModelAndView object which encapsulates the name of the view and any data (model) that needs to be displayed by the view. ModelAndView holds data as name value pairs.This data is later made available to the view. If the view is a jsp, then you can access the data using either jstl techniques or by directly querying the Request object. The code for our controller is shown below:
view source
print?
1
public class SpringMVCController extends AbstractController {
2
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) {
3
ModelAndView mview = new ModelAndView("springmvc") ;
4
mview.addObject("greeting", "Greetings from SpringMVC") ;
5
mview.addObject("member1", new Member("Jonh","Doe",
6
"1234 Main St","Pleasanton","94588","[email protected]","1234")) ;
7
return mview ;
8
}
9
}
The name of the view springmvc is passed in to the constructor of ModelAndView. The addObject method invocations add 2 model objects, “greeting” and “member1”. Later you will see how the view can retrieve the objects and display them.
回复

使用道具 举报

千问 | 2007-1-24 12:56:49 | 显示全部楼层
Step 3: Every Spring application needs metadata that defines the beans and their dependencies. For this application, we create a springmvc-servlet.xml. We help spring find it by specifying its location in web.xml.
view source
print?
1

2
contextConfigLocation
3
/WEB-INF/springmvc-servlet.xml
4

In springmvc-servlet.xml, the controller bean is defined as:
view source
print?
1

回复

使用道具 举报

千问 | 2007-1-24 12:56:49 | 显示全部楼层
Step 4: How does DispatcherServlet know which Controller should handle the request?
Spring uses handler mappings to associate controllers with requests. 2 commonly used handler mappings are BeanNameUrlHandlerMapping and SimpleUrlHandlerMapping.
In BeanNameUrlHandlerMapping, when the request url matches the name of the bean, the class in the bean definition is the controller that will handle the request.
In our example, we use BeanNameUrlHandlerMapping as shown below. Every request url ending in .htm is handled by SpringMVCController.
view source
print?
1

In SimpleUrlHandlerMapping, the mapping is more explicit. You can specify a number of URLs and each URL can be explicitly associated with a controller.
回复

使用道具 举报

千问 | 2007-1-24 12:56:49 | 显示全部楼层
Step 5: How does the DispatcherServlet know what to return as the response ?
As mentioned earlier, the handleRequestInternal method of the controller returns a ModelAndView Object.
In the controller code shown above, the name of the view "springmvc" is passed in the constructor of ModelAndView. At this point we have just given the name of the view. We have not said what file or classes or artifacts help produce the html, nor have we said whether the view technology used is JSP or velocity templates or XSLT. For this you need a ViewResolver, which provides that mapping between view name and a concrete view. Spring lets you produce a concrete view using many different technologies, but for this example we shall use JSP.
Spring provides a class InternalResourceViewResolver that supports JSPs and the declaration below in springmvc-servlet.xml tells spring that we use this resolver. The prefix and suffix get added to view name to produce the path to the jsp file that renders the view.
view source
print?
1

2

3

4

回复

使用道具 举报

千问 | 2007-1-24 12:56:49 | 显示全部楼层
Step 6: In this example, the view resolves to springmvc.jsp, which uses JSTL to get the data and display it. Spring makes the model objects “greeting” and “member1” available to the JSP as request scope objects. For educational purposes, the code below also gets the objects directly from the request.
view source
print?
01
// Using JSTL to get the model data
02
${greeting}
03
${member1.lastname
04
05
// Using java to get the model directly from the request
06
Map props = request.getParameterMap() ;
07
System.out.println("PARAMS =" + props) ;
08
Enumeration em = request.getAttributeNames() ;
09
while (em.hasMoreElements()) {
10
String name = (String) em.nextElement() ;
11
System.out.println("name = "+name) ;
12
}
13
System.out.println("Attrs are "+request.getAttributeNames()) ;
14
System.out.println("greeting is "+ request.getAttribute("greeting")) ;
15
Member m = (Member)request.getAttribute("member1") ;
16
System.out.println("member is "+m.toString()) ;
回复

使用道具 举报

千问 | 2007-1-24 12:56:49 | 显示全部楼层
Step 7: All files we have developed so far should be packaged into a war file as you would in any web application. The war may be deployed to tomcat by copying to tomcat_install\webapps. I built a war that you can download here.
Step 8: Point your web browser http://localhost:8080/springmvc/test.htm to run the application. The browser should display the data.

To summarize, Spring simplifies web application development by providing building blocks that you can assemble easily. We built a web application using Spring MVC. Spring provides an easy way to wire together our model, the controller SpringMVCController and the view springmvc.jsp. We did not have to explicitly code any request/response handling logic. By changing the metadata in springmvc-servlet.xml, you can switch to a different controller or a different view technology.
Thats it guys, a simple guide to Developing web applications with Spring by our JCG partner Manoj Khangaonkar. You can find the source code created for this tutorial here. Don't forget to share!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行