1. 程式人生 > >Spring MVC 配置樣例

Spring MVC 配置樣例

spring mvc 配置檔案記錄

<?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:aop="http://www.springframework.org/schema/aop"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">
        <!-- 配置檢視解析器 -->
        <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/"></property>
        <property name="suffix" value=".jsp"></property>
        </bean>
        <!-- 掃描控制器包  -->
        <context:component-scan base-package="com.geng.controller"></context:component-scan>
        
        <!-- 開啟mvc 註解,註冊HandlerMappering,HandlerAdapter -->
        <mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven>
         <!--  配置一個string 轉換date 的型別轉換器-->
         <bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
          <property name="converters">
            <set>
               <bean class="com.geng.util.String2DateConverter"></bean>
            </set>
          </property>
        </bean>
        
        <!-- 宣告式異常處理 -->
        <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
         <property name="defaultErrorView" value="error"></property>
         <property name="exceptionMappings">
            <props>
              <prop key="java.lang.Excepiton">error</prop>
            </props>
         </property>
        </bean>
        
        <!--配置攔截器  -->
       <mvc:interceptors>
           <mvc:interceptor>
               <mvc:mapping path="/*"/>
               <bean class="com.geng.controller.FirstInterceptor"></bean>
           </mvc:interceptor>
       </mvc:interceptors>
       
       <!-- 配置靜態資源 -->
     <!--   <mvc:resources mapping="/js/**" location="/js/"></mvc:resources>
	 <mvc:resources mapping="/css/**" location="/css/"></mvc:resources>
	 <mvc:resources mapping="/html/**" location="/html/"></mvc:resources>
	 <mvc:resources mapping="/img/**" location="/img/"></mvc:resources> -->
	   <!-- 控制器找不到的請求,找靜態資源  -->
	 <mvc:default-servlet-handler/>
        </beans>

此檔名稱為SpringMvc.xml 需要在專案的web.xml 檔案中配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="3.0" 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_3_0.xsd">
<!-- 註冊一個springMvc的前端控制器 -->

<servlet>
   <servlet-name>springMVC</servlet-name>
   <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
   <!-- 前端控制器需要一個初始化引數contextConfigLocation 
        配置一個資源路徑,就是springmvc的配置檔案  -->
   <init-param>
     <param-name>contextConfigLocation</param-name>
     <param-value>classpath:springMvc.xml</param-value>
   </init-param>
</servlet>
<!-- 配置前端控制器處理的請求  -->
<servlet-mapping>
  <servlet-name>springMVC</servlet-name>
  <url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 配置一個設定字符集的過濾器 -->
<filter>
  <filter-name>CharacterEncodingFilter</filter-name>
  <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  <!-- 這個CharacterEncodingFilter有一個屬性encoding 用初始化引數的方式設定 -->
  <init-param>
   <param-name>encoding</param-name>
   <param-value>UTF-8</param-value>
  </init-param>
  <!--  -->
</filter>
<filter-mapping>
  <filter-name>CharacterEncodingFilter</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>

<!--  此過濾器用於實現吧post 請求轉換成指定的請求,比如put ,delete 
 需要在form傳遞引數時傳遞一個_method 引數   vlaue 值為PUT/DELETE
   -->
<filter>
  <filter-name>HiddenHttpMethodFilter</filter-name>
  <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
  <filter-name>HiddenHttpMethodFilter</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>


<!--  -->
 <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <!--  配置需要載入的配置檔案,比如dao,service等的xml 檔案-->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:application-*.xml</param-value>
  </context-param>
  <error-page>
    <error-code>404</error-code>
    <location>/img/404.jpg</location>
  </error-page>
 
</web-app>