Spring MVC配置詳解(1)

來源:互聯網
上載者:User

標籤:conf   命名空間   init   property   file   except   support   ext   定義   

web.xml的配置
<!-- 配置前端控制器             前端控制器(DispatcherServlet)-->  <servlet>      <servlet-name>spring</servlet-name>      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>      <init-param>          <!-- ContextconfigLocation配置springmvc載入的設定檔          適配器、處理映射器等           -->          <param-name>contextConfigLocation</param-name>          <param-value>WEB-INF/classes/spring/springmvc.xml</param-value>  </init-param>  </servlet>  <servlet-mapping>      <servlet-name>spring</servlet-name>      <!-- 1、.action訪問以.action結尾的  由DispatcherServlet進行解析           2、/,所有訪問都由DispatcherServlet進行解析       -->      <url-pattern>/</url-pattern>  </servlet-mapping>

  在<servlet-mapping>中url如果是.action,前端控制器就只會攔截以.action結尾的請求,並不會理會靜態檔案。對靜態頁面的控制就要通過其他的手段。以/作為url的話就會攔截所有的請求,包括靜態頁面的請求。這樣的話就可以攔截任何想要處理的請求,但是有一個問題。如果攔截了所有的請求,如果不在攔截器中做出相應的處理那麼所有靜態js、css以及頁面中用到的圖片就會訪問不到造成頁面無法正常顯示。但這可以通過靜態資源的配置來解決這個問題。

配置spring容器:<context-param>        <param-name>contextConfigLocation</param-name>        <param-value>WEB-INF/classes/spring/applicationContext-*.xml</param-value></context-param>其中applicationContext-*.xml包含3個設定檔,是springIoC容器的具體配置。後面會提到。配置一個監聽器:<listener>        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener>web.xml的完整配置是這樣的:<?xml version="1.0" encoding="UTF-8"?><web-app 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">  <display-name></display-name>      <!-- 404錯誤攔截 -->  <error-page>    <error-code>404</error-code>    <location>/error404.jsp</location>  </error-page>  <!-- 500錯誤攔截 -->  <error-page>    <error-code>500</error-code>    <location>/error500.jsp</location>  </error-page>  <!-- 載入spring容器 -->    <context-param>        <param-name>contextConfigLocation</param-name>        <param-value>WEB-INF/classes/spring/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>      <init-param>          <!-- ContextconfigLocation配置springmvc載入的設定檔          適配器、處理映射器等           -->          <param-name>contextConfigLocation</param-name>          <param-value>WEB-INF/classes/spring/springmvc.xml</param-value>  </init-param>  </servlet>  <servlet-mapping>      <servlet-name>spring</servlet-name>      <!-- 1、.action訪問以.action結尾的  由DispatcherServlet進行解析           2、/,所有訪問都由DispatcherServlet進行解析       -->      <url-pattern>/</url-pattern>  </servlet-mapping>  <!-- 解決post亂碼問題的過濾器 -->  <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>  </filter>  <filter-mapping>      <filter-name>CharacterEncodingFilter</filter-name>      <url-pattern>/*</url-pattern>  </filter-mapping>  <welcome-file-list>    <welcome-file>welcome.jsp</welcome-file>  </welcome-file-list></web-app>看到設定檔中多了兩塊內容,一個是error page是用來友好的處理錯誤的,可以使用錯誤碼來區別並跳轉到相應的處理頁面。這段配置代碼最好放到最前面,在前端控制器攔截之前處理。還有一塊內容是一個解決post亂碼問題的過濾器,攔截post請求並編碼為utf8。springmvc.xml的配置視圖解析器的配置:<!-- 配置視圖解析器 -->     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">         <!-- 使用首碼和尾碼 -->         <property name="prefix" value="/"></property>         <property name="suffix" value=".jsp"></property></bean>在Controller中設定視圖名的時候會自動加上首碼和尾碼。Controller的配置自動掃描方式,掃描包下面所有的Controller,可以使用註解來指定訪問路徑。<!-- 使用組件掃描的方式可以一次掃描多個Controller --><context:component-scan base-package="com.wxisme.ssm.controller">也可以使用單個的配置方式,需要指定Controller的全限定名。<bean name="/queryUser.action" class="com.wxisme.ssm.controller.Controller1"/>配置註解的處理器適配器和處理器映射器:<!-- 註解的處理器適配器 --><bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/><!-- 註解的處理器映射器 --><bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>也可以使用下面的簡化配置:<!-- 配置註解的處理器映射器和處理器適配器 --><mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven>配置攔截器,可以直接定義攔截所有請求,也可以自訂攔截路徑。<mvc:interceptors>    <!-- 直接定義攔截所有請求 -->    <bean class="com.wxisme.ssm.interceptor.IdentityInterceptor"></bean>        <!-- <mvc:interceptor>            攔截所有路徑的請求   包括子路徑            <mvc:mapping path="/**"/>            <bean class="com.wxisme.ssm.interceptor.IdentityInterceptor"></bean>        </mvc:interceptor> -->    </mvc:interceptors>配置全域異常處理器<!-- 定義全域異常處理器 -->    <!-- 只有一個全域異常處理器起作用 -->    <bean id="exceptionResolver" class="com.wxisme.ssm.exception.OverallExceptionResolver"></bean>設定檔上傳資料解析器,在上傳檔案時需要配置。<!--配置上傳檔案資料解析器  -->    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">        <property name="maxUploadSize">            <value>9242880</value>        </property>    </bean>還可以配置一些自訂的參數類型,以日期類型綁定為例。<!-- 自訂參數類型綁定 -->    <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">     <property name="converters">         <list>             <!-- 日期類型綁定 -->             <bean class="com.wxisme.ssm.controller.converter.DateConverter"/>         </list>     </property>    </bean>上面提到過如果在配置前端控制器時攔截了所有的請求,不做特殊處理就會導致部分靜態資源無法使用。如果是這種情況就可以使用下面的配置來訪問靜態資源檔案。<mvc:resources mapping="/images/**" location="/images/" /><mvc:resources mapping="/css/**" location="/css/" />  <mvc:resources mapping="/js/**" location="/js/" /><mvc:resources mapping="/imgdata/**" location="/imgdata/" />也可以使用預設,但是需要在web.xml中配置。<!-- 訪問靜態資源檔案 -->    <!-- <mvc:default-servlet-handler/> 需要在web.xml中配置-->完全可以不攔截所有路徑,大可避免這個問題的發生。完整的配置大概是這樣的,需要注意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:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:jdbc="http://www.springframework.org/schema/jdbc"xmlns:jee="http://www.springframework.org/schema/jee"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd">     <!-- 配置視圖解析器 -->     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">         <!-- 使用首碼和尾碼 -->         <property name="prefix" value="/"></property>         <property name="suffix" value=".jsp"></property>     </bean>     <!-- 使用組件掃描的方式可以一次掃描多個Controller -->     <context:component-scan base-package="com.wxisme.ssm.controller">     </context:component-scan>     <!-- 配置註解的處理器映射器和處理器適配器 -->    <mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven>    <!-- 自訂參數類型綁定 -->    <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">     <property name="converters">         <list>             <!-- 日期類型綁定 -->             <bean class="com.wxisme.ssm.controller.converter.DateConverter"/>         </list>     </property>    </bean>    <!-- 訪問靜態資源檔案 -->    <!-- <mvc:default-servlet-handler/> 需要在web.xml中配置-->    <mvc:resources mapping="/images/**" location="/images/" />    <mvc:resources mapping="/css/**" location="/css/" />      <mvc:resources mapping="/js/**" location="/js/" />    <mvc:resources mapping="/imgdata/**" location="/imgdata/" />    <!-- 定義攔截器 -->    <mvc:interceptors>    <!-- 直接定義攔截所有請求 -->    <bean class="com.wxisme.ssm.interceptor.IdentityInterceptor"></bean>        <!-- <mvc:interceptor>            攔截所有路徑的請求   包括子路徑            <mvc:mapping path="/**"/>            <bean class="com.wxisme.ssm.interceptor.IdentityInterceptor"></bean>        </mvc:interceptor> -->    </mvc:interceptors>    <!--配置上傳檔案資料解析器  -->    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">        <property name="maxUploadSize">            <value>9242880</value>        </property>    </bean>    <!-- 定義全域異常處理器 -->    <!-- 只有一個全域異常處理器起作用 -->    <bean id="exceptionResolver" class="com.wxisme.ssm.exception.OverallExceptionResolver"></bean> </beans>

  

Spring MVC配置詳解(1)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.