首先去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中添加:
<context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </context-param> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <servlet> <servlet-name>spring</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <filter> <filter-name>Encoding</filter-name> <filter-class> org.springframework.web.filter.CharacterEncodingFilter </filter-class> <init-param> <param-name>encoding</param-name> <param-value>utf8</param-value> </init-param> </filter> <filter-mapping> <filter-name>Encoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
另外在WEB-INF下新建applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx" 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-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> <context:annotation-config /> <context:component-scan base-package="org.app.demo.spring" /> <!-- 自动扫描所有注解该路径 --> </beans>
在WEB-INF下新建spring-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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:annotation-config /> <!-- 把标记了@Controller注解的类转换为bean --> <context:component-scan base-package="org.app.demo.spring.controller" /> <!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 --> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" /> <!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/views/" p:suffix=".jsp" /> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" p:defaultEncoding="utf-8" /> </beans>
在源目录下新建三个包
org.app.demo.spring.controller org.app.demo.spring.service org.app.demo.spring.service.impl
在controller包下建HelloWorldController类
package org.app.demo.spring.controller; import javax.servlet.http.HttpServletRequest; import org.app.demo.spring.service.HelloWorldService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; @Controller @RequestMapping("/helloworld.do") public class HelloWorldController{ @Autowired private HelloWorldService helloWorldService; @RequestMapping public String getNewName(@RequestParam("userName") String userName, HttpServletRequest request){ String newUserName = helloWorldService.getNewName(userName); request.setAttribute("newUserName", newUserName); return "helloworld"; } }
在service包下新建HelloWorldService接口
package org.app.demo.spring.service; public interface HelloWorldService { public String getNewName(String userName); } 在impl包下新建HelloWorldService接口的实现类HelloWorldServiceImpl类 package org.app.demo.spring.service.impl; import org.app.demo.spring.service.HelloWorldService; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @Service public class HelloWorldServiceImpl implements HelloWorldService { @Override @Transactional public String getNewName(String userName) { return "Hello Spring!" + userName; } }
新建index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!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> <link rel="stylesheet" type="text/css" href="http://wjjcml1982.blog.163.com/blog/css/db_browser.css"> </head> <body> <form action="helloworld.do" method="post"> 请输入姓名:<input type="text" name="userName" /> <input type="submit" value="提交" /> <br /> </form> </body> </html>
然后再WEB-INF目录下新建views目录,在views目录下新建helloworld.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!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> <link rel="stylesheet" type="text/css" href="http://wjjcml1982.blog.163.com/blog/css/db_browser.css"> </head> <body> <h1><%=request.getAttribute("newUserName")%></h1> </body> </html>
保存完后布置到Tomcat中,启动Tomcat,访问http://localhost:8080/myapp/index.jsp
输入姓名(如张三)后,页面会跳转到http://localhost:8080/myapp/helloworld.do。
显示Hello Spring!张三。一切OK!