首先去Spring官网上去下载最新的Spring版本。http://www.springsource.com/download/community

目前最新的版本是3.1 M2。下载完后解压,将dist目录下的所有jar文件复制到你的项目的lib目录下(我在MyEclipse中新建了一个myapp的Web项目),另外再添加如下的JAR包,
commons-fileupload-1.2.1.jar

commons-logging-1.1.1.jar

在web. xml中添加:

[html] view plain copy print ?
  1. <context-param>

  2. <param-name>contextConfigLocationparam-name>

  3. <param-value>/WEB-INF/applicationContext.xmlparam-value>

  4. context-param>

  5. <listener>

  6. <listener-class>

  7. org.springframework.web.context.ContextLoaderListener

  8. listener-class>

  9. listener>

  10. <servlet>

  11. <servlet-name>springservlet-name>

  12. <servlet-class>

  13. org.springframework.web.servlet.DispatcherServlet

  14. servlet-class>

  15. <load-on-startup>1load-on-startup>

  16. servlet>

  17. <servlet-mapping>

  18. <servlet-name>springservlet-name>

  19. <url-pattern>*.dourl-pattern>

  20. servlet-mapping>

  21. <filter>

  22. <filter-name>Encodingfilter-name>

  23. <filter-class>

  24. org.springframework.web.filter.CharacterEncodingFilter

  25. filter-class>

  26. <init-param>

  27. <param-name>encodingparam-name>

  28. <param-value>utf8param-value>

  29. init-param>

  30. filter>

  31. <filter-mapping>

  32. <filter-name>Encodingfilter-name>

  33. <url-pattern>/*url-pattern>

  34. filter-mapping>


另外在WEB-INF下新建applicationContext.xml

[html] view plain copy print ?
  1. xmlversion="1.0"encoding="UTF-8"?>

  2. <beansxmlns="http://www.springframework.org/schema/beans"

  3. xmlns:aop="http://www.springframework.org/schema/aop"xmlns:context="http://www.springframework.org/schema/context"

  4. xmlns:p="http://www.springframework.org/schema/p"xmlns:tx="http://www.springframework.org/schema/tx"

  5. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  6. xsi:schemaLocation="

  7. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

  8. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd

  9. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd

  10. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

  11. <context:annotation-config/>

  12. <context:component-scanbase-package="org.app.demo.spring"/>

  13. beans>


在WEB-INF下新建spring-servlet.xml

[html] view plain copy print ?
  1. xmlversion="1.0"encoding="UTF-8"?>

  2. <beansxmlns="http://www.springframework.org/schema/beans"

  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"

  4. xmlns:context="http://www.springframework.org/schema/context"

  5. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

  6. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd

  7. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd

  8. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

  9. <context:annotation-config/>

  10. <context:component-scanbase-package="org.app.demo.spring.controller"/>

  11. <beanclass="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>

  12. <beanclass="org.springframework.web.servlet.view.InternalResourceViewResolver"

  13. p:prefix="/WEB-INF/views/"p:suffix=".jsp"/>

  14. <beanid="multipartResolver"

  15. class="org.springframework.web.multipart.commons.CommonsMultipartResolver"

  16. p:defaultEncoding="utf-8"/>

  17. beans>


在源目录下新建三个包

[java] view plain copy print ?
  1. org.app.demo.spring.controller

  2. org.app.demo.spring.service

  3. org.app.demo.spring.service.impl


在controller包下建HelloWorldController类

[java] view plain copy print ?
  1. package org.app.demo.spring.controller;

  2. import javax.servlet.http.HttpServletRequest;

  3. import org.app.demo.spring.service.HelloWorldService;

  4. import org.springframework.beans.factory.annotation.Autowired;

  5. import org.springframework.stereotype.Controller;

  6. import org.springframework.web.bind.annotation.RequestMapping;

  7. import org.springframework.web.bind.annotation.RequestParam;

  8. @Controller

  9. @RequestMapping("/helloworld.do")

  10. publicclass HelloWorldController{

  11. @Autowired

  12. private HelloWorldService helloWorldService;

  13. @RequestMapping

  14. public String getNewName(@RequestParam("userName") String userName, HttpServletRequest request){

  15. String newUserName = helloWorldService.getNewName(userName);

  16. request.setAttribute("newUserName", newUserName);

  17. return"helloworld";

  18. }

  19. }


在service包下新建HelloWorldService接口

[java] view plain copy print ?
  1. package org.app.demo.spring.service;

  2. publicinterface HelloWorldService {

  3. public String getNewName(String userName);

  4. }

  5. 在impl包下新建HelloWorldService接口的实现类HelloWorldServiceImpl类

  6. package org.app.demo.spring.service.impl;

  7. import org.app.demo.spring.service.HelloWorldService;

  8. import org.springframework.stereotype.Service;

  9. import org.springframework.transaction.annotation.Transactional;

  10. @Service

  11. publicclass HelloWorldServiceImpl implements HelloWorldService {

  12. @Override

  13. @Transactional

  14. public String getNewName(String userName) {

  15. return"Hello Spring!" + userName;

  16. }

  17. }


新建index.jsp

[plain] view plain copy print ?
  1. <%@ page language="java" contentType="text/html; charset=UTF-8"

  2. pageEncoding="UTF-8"%>

  3. Insert title here

  4. 请输入姓名:



然后再WEB-INF目录下新建views目录,在views目录下新建helloworld.jsp

[html] view plain copy print ?
  1. <%@ page language="java"contentType="text/html; charset=UTF-8"

  2. pageEncoding="UTF-8"%>

  3. >

  4. <html>

  5. <head>

  6. <metahttp-equiv="Content-Type"content="text/html; charset=UTF-8">

  7. <title>Insert title heretitle>

  8. <linkrel="stylesheet"type="text/css"href="http://wjjcml1982.blog.163.com/blog/css/db_browser.css">

  9. head>

  10. <body>

  11. <h1><%=request.getAttribute("newUserName")%>h1>

  12. body>

  13. html>


保存完后布置到Tomcat中,启动Tomcat,访问http://localhost:8080/myapp/index.jsp

输入姓名(如张三)后,页面会跳转到http://localhost:8080/myapp/helloworld.do。
显示Hello Spring!张三。一切OK!