参考<Spring in Action 3rd Edtion>中实例,稍加修改,这个DEMO中没有使用数据库,单纯从MVC的角度,解释Spring MVC的搭建过程,前后台值的传递。
1.首先是web.xml文件
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" 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_2_5.xsd"> <!-- Spring MVC Servlet Configuration --> <servlet> <servlet-name>spitter</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>spitter</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- Spring Application Context Loader --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> </web-app>
使用的servlet是org.springframework.web.servlet.DispatcherServlet ,这个是Spring MVC的核心,是请求的入口,用来处理请求转发,这里要注意的是<servlet-name>这个属性,例子中的值是spitter,框架默认处理会去找WEB-INF路径下名为spitter-servlet.xml的Spring Context. 下边还配置了一个Listener,这个稍后再做解释。下边先来看看spitter-servlet.xml.
2.spitter-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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" 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"> <context:component-scan base-package="com.huangjing.spitter.mvc"/> <!-- Mapping For Static Resource Request --> <mvc:resources mapping="/resources/**" location="/resources/"/> <!-- AnnotationDriven Controller Mapping --> <mvc:annotation-driven/> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/"/> <property name="suffix" value=".jsp"/> </bean> </beans>
<context:component-scan>与普通的上下文配置文件没有什么区别,将基于Annotation的bean注册至容器,需要注意的是<mvc>命名空间下的两个属性,resources,是请求静态资源的路径映射。这里还有注册了一个bean,对应class是org.springframework.web.servlet.view.InternalResourceViewResolver,指定了两个属性,前缀和后缀,前缀指定视图层文件的存放目录,后缀指定文件类型,不单可以使用JSP,Velocity,FreeMarker都是可以的。下边看看工程的结构吧。
3.目录文件层次
4. Controller类
package com.huangjing.spitter.mvc;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.huangjing.spitter.service.SpitterService;
@Controller
public class HomeController {
public final int SPITTER_PER_PAGE = 25;
private SpitterService spitterService;
@Autowired
public HomeController(SpitterService spitterService){
this.spitterService = spitterService;
}
@RequestMapping({"/home"})
public String showHomePage(Map<String,Object> model){
model.put("spittles", spitterService.getRecentSpittles(SPITTER_PER_PAGE));
return "home";
}
}
这个相当于Struts中Action,@Controller @Autowired是SpringFramework的内容,不作解释了,主要还是看看@RequestMapping这个注解,这个注解起到两个作用,(1)将所标注的注解声明为请求处理方法;(2)指定所接受的请求,类似于Struts中的<action>的name属性,参数是一个Map,把处理的值放进去,前台便可以通过EL表达式取得了。返回一个字符串,对应Struts的result.
5.然后当然还有域模型和业务处理类,域模型是可以重复利用的POJO.业务处理类也是Spring API无关的,只是这两个类我也是使用Spring来注入的,但是之前看到spitter-servlet.xml文件并不会注册这些bean,他们的配置文件又在哪呢,这个就是org.springframework.web.context.ContextLoaderListener所做的事了,默认他会加载另外的Context,不给他参数的话,默认是会加载WEB-INF下的applicationContext.xml作为额外的Spring上下文。
6.最后看看JSP文件
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ taglib prefix="s" uri="http://www.springframework.org/tags"%> <!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>Demo Page</title> <link rel="stylesheet" type="text/css" href="resources/ext/resources/css/ext-all.css"> <script type="text/javascript" src="resources/ext/bootstrap.js"></script> <script type="text/javascript" src="resources/js/common.js"></script> <script type="text/javascript" src="resources/js/Test.js"></script> </head> <body> Demo Home Page ${spittles[0].text} ${spittles[1].text} </body> </html>
里面我引用了Spring的标签库,但是并没有使用,页面中使用EL取值。到这里只是一个简单的HelloWorld示例,以后再做深入学习。