1、准备包
下载的是spring framework 3.2.0,从中抽取以下jar到工程的WEB-INF/lib下:
spring-beans-3.2.0.RELEASE.jar
spring-context-3.2.0.RELEASE.jar
spring-core-3.2.0.RELEASE.jar
spring-expression-3.2.0.RELEASE.jar
spring-web-3.2.0.RELEASE.jar
spring-webmvc-3.2.0.RELEASE.jar
另外还需要几个第三方jar包,记录日志和处理json:
commons-logging-1.1.1.jar
jackson-core-als-1.9.11.jar
jackson-mapper-asl-1.9.11.jar
2、WEB-INF/web.xml
< xml version="1.0" encoding="UTF-8" >
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">
3、WEB-INF/spring-servlet.xml
< xml version="1.0" encoding="UTF-8" >
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
4、WEB-INF/applicationContext.xml
spring的配置文件,由于我们不使用它的其它功能,暂时放个空的就好了。
< xml version="1.0" encoding="UTF-8" >
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.0.xsd">
5、写Controller
package com.test.mvc.web;
import org.springframework.stereotype.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.*;
/**
* 控制器,用Controller注解
*/
@Controller
public class HomeController {
/**
* 映射到/welcome
*/
@RequestMapping(value = "/welcome")
public ModelAndView welcome(){
ModelAndView mv = new ModelAndView("welcome"); //使用welcome.jsp,如果不写,根据url默认也是welcome.jsp
mv.addObje