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">
 <!-- <display-name>smvc</display-name> -->
 <!-- 在Spring框架中是如何解決從頁面傳來的字符串的編碼問題的呢? 下面我們來看看Spring框架給我們提供過濾器CharacterEncodingFilter 
 這個過濾器就是針對於每次瀏覽器請求進行過濾的,然后再其之上添加了父類沒有的功能即處理字符編碼。 其中encoding用來設置編碼格式,forceEncoding用來設置是否理會 
 request.getCharacterEncoding()方法,設置為true則強制覆蓋之前的編碼格式。 -->
 <filter>
 <filter-name>characterEncodingFilter</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>
 <init-param>
 <param-name>forceEncoding</param-name>
 <param-value>true</param-value>
 </init-param>
 </filter>
 <!-- 過濾器映射 -->
 <filter-mapping>
 <filter-name>characterEncodingFilter</filter-name>
 <url-pattern>/*</url-pattern>
 </filter-mapping>
 <!-- 項目中使用Spring 時,applicationContext.xml配置文件中並沒有BeanFactory,要想在業務層中的class 
 文件中直接引用Spring容器管理的bean可通過以下方式 -->
 <!--1、在web.xml配置監聽器ContextLoaderListener -->
 <!--ContextLoaderListener的作用就是啟動Web容器時,自動裝配ApplicationContext的配置信息。因為它實現了ServletContextListener這個接口,在web.xml配置這個監聽器,啟動容器時,就會默認執行它實現的方法。 
 在ContextLoaderListener中關聯了ContextLoader這個類,所以整個加載配置過程由ContextLoader來完成。 它的API說明 
 第一段說明ContextLoader可以由 ContextLoaderListener和ContextLoaderServlet生成。 如果查看ContextLoaderServlet的API,可以看到它也關聯了ContextLoader這個類而且它實現了HttpServlet這個接口 
 第二段,ContextLoader創建的是 XmlWebApplicationContext這樣一個類,它實現的接口是WebApplicationContext->ConfigurableWebApplicationContext->ApplicationContext-> 
 BeanFactory這樣一來spring中的所有bean都由這個類來創建 IUploaddatafileManager uploadmanager 
 = (IUploaddatafileManager) ContextLoaderListener.getCurrentWebApplicationContext().getBean("uploadManager"); -->
 <listener>
 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>
 <!--2、部署applicationContext的xml文件 -->
 <!--如果在web.xml中不寫任何參數配置信息,默認的路徑是"/WEB-INF/applicationContext.xml, 在WEB-INF目錄下創建的xml文件的名稱必須是applicationContext.xml。 
 如果是要自定義文件名可以在web.xml里加入contextConfigLocation這個context參數: 在<param-value> </param-value>里指定相應的xml文件名,如果有多個xml文件,可以寫在一起並以“,”號分隔。 
 也可以這樣applicationContext-*.xml采用通配符,比如這那個目錄下有applicationContext-ibatis-base.xml, 
 applicationContext-action.xml,applicationContext-ibatis-dao.xml等文件,都會一同被載入。 
 在ContextLoaderListener中關聯了ContextLoader這個類,所以整個加載配置過程由ContextLoader來完成。 -->
 <context-param>
 <param-name>contextConfigLocation</param-name>
 <param-value>classpath:spring/applicationContext.xml</param-value>
 </context-param>
 <!--如果你的DispatcherServlet攔截"/",為了實現REST風格,攔截了所有的請求,那么同時對*.js,*.jpg等靜態文件的訪問也就被攔截了。 -->
 <!--方案一:激活Tomcat的defaultServlet來處理靜態文件 -->
 <!--要寫在DispatcherServlet的前面, 讓 defaultServlet先攔截請求,這樣請求就不會進入Spring了,我想性能是最好的吧。 -->
 <!-- <servlet-mapping>
 <servlet-name>FileServlet</servlet-name>
 <url-pattern>*.css</url-pattern>
 </servlet-mapping>
 <servlet-mapping>
 <servlet-name>FileServlet</servlet-name>
 <url-pattern>*.swf</url-pattern>
 </servlet-mapping>
 <servlet-mapping>
 <servlet-name>FileServlet</servlet-name>
 <url-pattern>*.gif</url-pattern>
 </servlet-mapping>
 <servlet-mapping>
 <servlet-name>FileServlet</servlet-name>
 <url-pattern>*.jpg</url-pattern>
 </servlet-mapping>
 <servlet-mapping>
 <servlet-name>FileServlet</servlet-name>
 <url-pattern>*.png</url-pattern>
 </servlet-mapping>
 <servlet-mapping>
 <servlet-name>FileServlet</servlet-name>
 <url-pattern>*.js</url-pattern>
 </servlet-mapping>
 <servlet-mapping>
 <servlet-name>FileServlet</servlet-name>
 <url-pattern>*.html</url-pattern>
 </servlet-mapping>
 <servlet-mapping>
 <servlet-name>FileServlet</servlet-name>
 <url-pattern>*.xml</url-pattern>
 </servlet-mapping>
 <servlet-mapping>
 <servlet-name>FileServlet</servlet-name>
 <url-pattern>*.json</url-pattern>
 </servlet-mapping>
 <servlet-mapping>
 <servlet-name>FileServlet</servlet-name>
 <url-pattern>*.map</url-pattern>
 </servlet-mapping> -->
 <!--使用Spring MVC,配置DispatcherServlet是第一步。DispatcherServlet是一個Servlet,,所以可以配置多個DispatcherServlet -->
 <!--DispatcherServlet是前置控制器,配置在web.xml文件中的。攔截匹配的請求,Servlet攔截匹配規則要自已定義,把攔截下來的請求,依據某某規則分發到目標Controller(我們寫的Action)來處理。 -->
 <servlet>
 <servlet-name>DispatcherServlet</servlet-name><!--在DispatcherServlet的初始化過程中,框架會在web應用的 
 WEB-INF文件夾下尋找名為[servlet-name]-servlet.xml 的配置文件,生成文件中定義的bean。 -->
 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
 <!--指明了配置文件的文件名,不使用默認配置文件名,而使用dispatcher-servlet.xml配置文件。 -->
 <init-param>
 <param-name>contextConfigLocation</param-name>
 <!--其中<param-value>**.xml</param-value> 這里可以使用多種寫法 -->
 <!--1、不寫,使用默認值:/WEB-INF/<servlet-name>-servlet.xml -->
 <!--2、<param-value>/WEB-INF/classes/dispatcher-servlet.xml</param-value> -->
 <!--3、<param-value>classpath*:dispatcher-servlet.xml</param-value> -->
 <!--4、多個值用逗號分隔 -->
 <param-value>classpath:spring/spring-mvc.xml</param-value>
 </init-param>
 <!--是啟動順序,讓這個Servlet隨Servletp容器一起啟動。 -->
 <load-on-startup>1</load-on-startup>
 </servlet>
 <servlet-mapping>
 <!--這個Servlet的名字是dispatcher,可以有多個DispatcherServlet,是通過名字來區分的。每一個DispatcherServlet有自己的WebApplicationContext上下文對象。同時保存的ServletContext中和Request對象中. -->
 <!--ApplicationContext是Spring的核心,Context我們通常解釋為上下文環境,我想用“容器”來表述它更容易理解一些,ApplicationContext則是“應用的容器”了:P,Spring把Bean放在這個容器中,在需要的時候,用getBean方法取出 -->
 <servlet-name>DispatcherServlet</servlet-name>
 <!--Servlet攔截匹配規則可以自已定義,當映射為@RequestMapping("/user/add")時,為例,攔截哪種URL合適? -->
 <!--1、攔截*.do、*.htm, 例如:/user/add.do,這是最傳統的方式,最簡單也最實用。不會導致靜態文件(jpg,js,css)被攔截。 -->
 <!--2、攔截/,例如:/user/add,可以實現現在很流行的REST風格。很多互聯網類型的應用很喜歡這種風格的URL。弊端:會導致靜態文件(jpg,js,css)被攔截后不能正常顯示。 -->
 <url-pattern>/</url-pattern> <!--會攔截URL中帶“/”的請求。 -->
 </servlet-mapping>
 
 <error-page> <!--當系統出現404錯誤,跳轉到頁面nopage.html -->
 <error-code>404</error-code>
 <location>/view/nopage.jsp</location>
 </error-page>
 <error-page> <!--當系統出現java.lang.NullPointerException,跳轉到頁面error.html -->
 <exception-type>java.lang.NullPointerxception</exception-type>
 <location>/error.html</location>
 </error-page>
 <session-config><!--會話超時配置,單位分鍾 -->
 <session-timeout>360</session-timeout>
 </session-config>
 <!-- <filter>
 <filter-name>checkFilter</filter-name>
 <filter-class>com.cn.filter.SessionFliter</filter-class>
 </filter>
 <filter-mapping>
 <filter-name>checkFilter</filter-name>
 <url-pattern>/*</url-pattern>
 </filter-mapping> -->
</web-app>
2.applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
 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.xsd
 http://www.springframework.org/schema/context
 http://www.springframework.org/schema/context/spring-context.xsd">
 <!-- 注意上面的也可以寫成spring-context-4.3.xsd,如果不寫則默認是用當前的版本 -->
 
 <!--啟用spring的一些annotation -->
 <context:annotation-config />
 <!-- 注意這里的base-package的值就是HelloWorldController.java所在的包名 -->
 <context:component-scan base-package="com.cn.*" >
 <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> 
 </context:component-scan>
</beans>
spring-mvc.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:tx="http://www.springframework.org/schema/tx" 
 xmlns:context="http://www.springframework.org/schema/context" 
 xmlns:mvc="http://www.springframework.org/schema/mvc" 
 xsi:schemaLocation="http://www.springframework.org/schema/beans 
 http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
 http://www.springframework.org/schema/tx 
 http://www.springframework.org/schema/tx/spring-tx-3.2.xsd 
 http://www.springframework.org/schema/context 
 http://www.springframework.org/schema/context/spring-context-3.2.xsd 
 http://www.springframework.org/schema/mvc 
 http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"> 
 
 <!-- 自動掃描的包名 --> 
 <context:component-scan base-package="com.cn.*" >
 <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> 
 </context:component-scan>
 
 <!-- 默認的注解映射的支持,自動注冊DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter --> 
 <mvc:annotation-driven /> 
 
 <!-- 視圖解釋類 --> 
 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
 <property name="prefix" value="/view/"/> 
 <property name="suffix" value=".jsp"/> 
 </bean> 
 
 <!-- 對靜態資源文件的訪問--> 
 <mvc:resources mapping="/images/**" location="/WEB-INF/images/" cache-period="31556926"/> 
 <mvc:resources mapping="/js/**" location="/WEB-INF/js/" cache-period="31556926"/> 
 <mvc:resources mapping="/css/**" location="/WEB-INF/css/" cache-period="31556926"/> 
 
</beans>
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>spring</groupId>
 <artifactId>spring-mvc</artifactId>
 <packaging>war</packaging>
 <version>0.0.1-SNAPSHOT</version>
 <name>spring-mvc Maven Webapp</name>
 <url>http://maven.apache.org</url>
 <!-- 初始化參數 -->
 <properties>
 <spring.version>4.1.1.RELEASE</spring.version>
 </properties>
 <dependencies>
 <dependency>
 <groupId>junit</groupId>
 <artifactId>junit</artifactId>
 <version>3.8.1</version>
 <scope>test</scope>
 </dependency>
 <dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-core</artifactId>
 <version>${spring.version}</version>
 </dependency>
 <dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-web</artifactId>
 <version>${spring.version}</version>
 </dependency>
 <dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-webmvc</artifactId>
 <version>${spring.version}</version>
 </dependency>
 <dependency>
 <groupId>javax.servlet</groupId>
 <artifactId>javax.servlet-api</artifactId>
 <version>3.1.0</version>
 </dependency>
 <dependency> 
 <groupId>javax.servlet</groupId> 
 <artifactId>jstl</artifactId> 
 <version>1.2</version> 
 </dependency>
 </dependencies>
 <build>
 <finalName>發布的項目名稱</finalName>
 <resources>
 <!--表示把java目錄下的有關xml文件,properties文件編譯/打包的時候放在resource目錄下 -->
 <resource>
 <directory>${basedir}/src/main/java</directory>
 <includes>
 <include>**/*.properties</include>
 <include>**/*.xml</include>
 </includes>
 </resource>
 <resource>
 <directory>${basedir}/src/main/resources</directory>
 </resource>
 </resources>
 </build>
</project>
