一般而言配置文件包含:
web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<description>
Spring MVC DEOM
</description>
<display-name>springMVC</display-name>
<!--
CharacterEncodingFilter類具有encoding和forceEncoding兩個屬性耳舅,其中encoding是表示設置request的編碼卷哩,forceEncoding表示是否同時設置response的編碼。
<filter>下的<filter-name>內的值和<filter-mapping>下的<filter-name>內的值要完全一致
-->
<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>
<!-- /下的所有請求都為UTF-8編碼 -->
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--
Spring MVC相關設置
<servlet>下的<servlet-name>內的值和<servlet-mapping>下的<servlet-name>內的值要完全一致
-->
<servlet>
<servlet-name>SpringMVC-DEOM</servlet-name>
<!-- DispatcherServlet主要負責流程的控制 -->
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- Spring MVC配置文件路徑 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/springMVC-servlet.xml</param-value>
</init-param>
<!-- 表示啟動容器時初始化該Servlet -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVC-DEOM</servlet-name>
<!-- 表示哪些請求交給Spring Web MVC處理皱埠, “/” 是用來定義默認servlet映射的日矫。也可以如“*.html”表示攔截所有以html為擴展名的請求。 -->
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
Spring MVC配置文件路徑 上面信息中可以得出 /WEB-INF/springMVC-servlet.xml 是springMVC的配置文件用踩,我們來看看springmvc配置文件中有哪些信息深寥;
1)最簡單的婉称,如下只包含一個bean節(jié)點
<?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"
xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd">
<bean id = "helloworld" class="com.game.controller.Helloworld">
</bean>
</beans>
其中說明 com.game.controller.Helloworld 是我們注冊的一個類块仆,這個類可訪問;
2)但在一個項目中王暗,我們其實并不希望每個類都這么聲明一次后再用悔据,所以往往項目中是以以下方式進行
<?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"
xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd">
<context:component-scan base-package="com.game.controller"/>
</beans>
從Spring提供的參考手冊中我們得知該配置的功能是掃描配置的base-package包下的所有使用了@Component注解的類,并且將它們自動注冊到容器中瘫筐,同時也掃描@Controller蜜暑,@Service,@Respository這三個注解策肝,因為他們是繼承自@Component肛捍。
3)在項目中我們經常見到還有如下這個配置,其實有了上面的配置之众,這個是可以省略掉的拙毫,因為上面的配置會默認打開以下配置。以下配置會默認聲明了@Required棺禾、@Autowired缀蹄、 @PostConstruct、@PersistenceContext膘婶、@Resource缺前、@PreDestroy等注解。
<context:annotation-config/>
4) 另外悬襟,還有一個和SpringMVC相關如下配置衅码,經過驗證,這個是SpringMVC必須要配置的脊岳,因為它聲明了@RequestMapping逝段、@RequestBody、@ResponseBody等割捅。并且奶躯,該配置默認加載很多的參數綁定方法,比如json轉換解析器等亿驾。
<mvc:annotation-driven />
springmvc通過上邊這個注解的方式可以代替處理器映射器和處理器適配器