feinifi 2014-10-29
现在主流的Web MVC框架除了Struts这个主力 外,其次就是Spring MVC了,因此这也是作为一名程序员需要掌握的主流框架,框架选择多了,应对多变的需求和业务时,可实行的方案自然就多了。不过要想灵活运用Spring MVC来应对大多数的Web开发,就必须要掌握它的配置及原理。
一、下载spring源包
spring地址:http://www.springsource.org/download
我下的是spring-framework-3.1.0.RELEASE-with-docs.zip
下载依赖包:spring-framework-3.0.5.RELEASE-dependencies.zip
注意官网上3.0.3版本以后同版本依赖包不提供下载
二、导入所需jar包
引入dist目录下除了下面三个其余所有包
org.springframework.web.struts-3.1.0.RELEASE.jar
org.springframework.spring-library-3.1.0.RELEASE.libd
org.springframework.web.portlet-3.1.0.RELEASE.jar
引入依赖包下com.springsource.org.apache.commons.logging-1.1.1.jar及com.springsource.org.aopalliance-1.0.0.jar
三、spring框架配置
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"> <!--配置Sring MVC的核心控制器DispatcherServlet --> <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:/spring-*.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <!-- 为DispatcherServlet建立映射 --> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>*.html</url-pattern> </servlet-mapping> <context-param> <param-name>log4jConfigLocation</param-name> <param-value>classpath:/log4j.properties</param-value> </context-param> <!-- 配置log4j.xml监听器 --> <listener> <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> </listener> <!-- 解决工程编码过滤器 --> <filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <display-name></display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
2、应用上下文配置
spring-servlet.xml即配置用于开启基于注解的springMVC功能,照web.xml中设定,路径为WEB-INF下