经过两天不懈的奋战,我的第一个Spring Web程序终于看到结果了。本文记录了开发的详细过程,借此纪念自己的辛苦付出。
第一步:开发环境的准备。我的开发环境用的是Eclipse Indigo JavaEE 3.7.2 + JBoss Tools 3.3 + SpringSource Tool Suite for Eclipse Indigo (3.7) 2.9.1。
第二步:下载Spring Framework,当前的最新版本是3.1.1.RELEASE,下载地址是http://s3.amazonaws.com/dist.springframework.org/release/SPR/spring-framework-3.1.1.RELEASE-with-docs.zip。将下载完成后的文件解压缩到D:\develop\spring-framework-3.1.1,下图是其解开后的目录结构:
第三步:打开Eclipse,新建一个Dynamic Web Project项目guestbook-spring,将D:\develop\spring-framework-3.1.1\dist目录下所有的jar文件复制到项目的WEB-INF/lib目录。其实dist目录下的很多文件本文中都不会用到,但为了以后使用方便,就一次性的将所有jar文件复制了过来。
第四步:新增控制器HelloController.java,完整内容如下:
1 package org.reindeer.tutorials.guestbook.spring; 2 3 import org.springframework.stereotype.Controller; 4 import org.springframework.web.bind.annotation.RequestMapping; 5 import org.springframework.web.bind.annotation.RequestMethod; 6 import org.springframework.web.servlet.ModelAndView; 7 8 @Controller 9 public class HelloController { 10 @RequestMapping(value = "/hello", method = RequestMethod.GET) 11 public ModelAndView index() { 12 return new ModelAndView("welcome"); 13 } 14 }
说明:
- 第8行的@Controller,表明了HelloController类是一个Spring MVC的控制器。
- 第10行@RequestMapping用来指明方法的请求映射,当前方法映射到/hello的GET请求.
- 第12行的welcome是视图的名称。
第五步:新增文件WEB-INF/views/welcome.jsp,完整内容如下:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="java.util.*" %> <!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=UTF-8"> <title>Insert title here</title> </head> <body>Welcome!<br/>Current time is <%=new Date()%></body> </html>
第六步:新增文件WEB-INF/config/application-context.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:context="http://www.springframework.org/schema/context" default-lazy-init="true" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 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"> <mvc:annotation-driven /> <context:component-scan base-package="org.reindeer.tutorials.guestbook.spring" /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/views/" p:suffix=".jsp" /> </beans>
第七步:新增文件WEB-INF/config/servlet-context.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:context="http://www.springframework.org/schema/context" default-lazy-init="true" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 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"> </beans>
第八步:修改WEB-INF/web.xml,完整内容下:
<?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp_ID" version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <display-name>guestbook-spring</display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/config/application-context.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>spring-servlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/config/servlet-context.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>spring-servlet</servlet-name> <url-pattern>/spring/*</url-pattern> </servlet-mapping> </web-app>
最后一步:将项目发布到JBoss AS,打开浏览器,输入网址http://localhost:8080/guestbook-spring/spring/hello,就可以成功的看到我们的欢迎页面。
让我们详细的解释一下整个的实现过程:
- 在Web应用程序中集成Spring MVC框架,必须要在web.xml文件中注册org.springframework.web.context.ContextLoaderListener监听程序和org.springframework.web.servlet.DispatcherServlet服务程序。
- ContextLoaderListener监听程序使用<context-param>contextConfigLocation</context-param>指定的配置文件(本例中是/WEB-INF/config/application-context.xml)初始化应用程序上下文环境。
- DispatcherServlet服务程序使用<init-param>contextConfigLocation</init-param>指定的配置文件(本例中是/WEB-INF/config/servlet-context.xml)初始化应用程序上下文环境。
- ContextLoaderListener上下文环境和DispatcherServlet上下文环境是有上下级隶属关系的。在一个Web应用程序,只能定义一个ContextLoaderListener监听程序,但是可以定义多个DispatcherServlet服务程序。每个DispatcherServlet服务程序都可以有专属的本地上下文环境,但是他们都共享一个相同的ContextLoaderListener上下文环境。
- application-context.xml使用了Spring MVC的注解功能。<mvc:annotation-driven>指示开启Spring的注解功能,<context:component-scan>指示Spring搜索org.reindeer.tutorials.guestbook.spring包中的所有类。
- org.springframework.web.servlet.view.InternalResourceViewResolver指示使用Spring内置的视图解析方法,每一个视图都应该能够对应到文件/WEB-INF/views/视图名.jsp。例如,HelloController.index()方法返回一个名为welcome的视图,转换为内部的文件就应该是/WEB-INF/views/welcome.jsp。
- 在web.xml文件中我们指定了spring-servlet的请求格式为/spring/*,相应的HelloController.index()方法的请求URI就应该是/spring/hello。
guestbook-spring项目文件下载地址(不包括Spring库文件):guestbook-spring-0.01.rar