Spring+SpringMvc+Mybatis整合注意事項

我是壞男孩發表於2016-08-31

1.web.xml程式碼如下

<?xml version="1.0" encoding="UTF-8"?>  
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xmlns="http://java.sun.com/xml/ns/javaee"  
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"  
    version="3.0">  
    <display-name>Archetype Created Web Application</display-name>  
    <!-- Spring和mybatis的配置檔案 -->  
    <context-param>  
        <param-name>contextConfigLocation</param-name>  
        <param-value>classpath:spring-mybatis.xml</param-value>  
    </context-param>  
    
   
    <!-- 編碼過濾器 -->  
    <filter>  
        <filter-name>encodingFilter</filter-name>  
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
        <async-supported>true</async-supported>  
        <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>  
    <!-- Spring監聽器 -->  
    <listener>  
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
    </listener>  
    <!-- 防止Spring記憶體溢位監聽器 -->  
    <listener>  
        <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>  
    </listener>  
  
    <!-- Spring MVC servlet -->  
    <servlet>  
        <servlet-name>SpringMVC</servlet-name>  
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
        <init-param>  
            <param-name>contextConfigLocation</param-name>  
            <param-value>classpath:spring-mvc.xml</param-value>  
        </init-param>  
        <load-on-startup>1</load-on-startup>  
        <async-supported>true</async-supported>  
    </servlet>  
    <servlet-mapping>  
        <servlet-name>SpringMVC</servlet-name>  
        <!-- 此處可以可以配置成*.do,對應struts的字尾習慣 這裡代表攔截如下兩種字尾的請求,如果只寫 /,那麼代表攔截所有資源 -->  
        <url-pattern>*.json</url-pattern>  
        <url-pattern>*.jhtml</url-pattern> 
    </servlet-mapping>  
    <welcome-file-list>  
        <welcome-file>/index.jsp</welcome-file>  
    </welcome-file-list>  
  
</web-app>  

 <!-- 讀取spring和shiro配置檔案 -->
<!--     <context-param> -->
<!--         <param-name>contextConfigLocation</param-name> -->
<!--         <param-value>classpath:spring-shiro.xml</param-value> -->
<!--     </context-param> -->
    <!-- 設計路徑變數值 -->
<!--     <context-param> -->
<!--         <param-name>webAppRootKey</param-name> -->
<!--         <param-value>springmvc.root</param-value> -->
<!--     </context-param> -->
    
    <!-- shiro過濾器 -->
<!--     <filter> -->
<!--         <filter-name>shiroFilter</filter-name> -->
<!--         <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> -->
<!--         <init-param> -->
<!--             <param-name>targetFilterLifecycle</param-name> -->
<!--             <param-value>true</param-value> -->
<!--         </init-param> -->
<!--     </filter> -->
<!--     <filter-mapping> -->
<!--         <filter-name>shiroFilter</filter-name> -->
<!--         <url-pattern>*.jhtml</url-pattern> -->
<!--         <url-pattern>*.json</url-pattern> -->
<!--     </filter-mapping> -->

 2.當有兩個以上的.xml檔案載入時,錯誤範例

<!-- Spring和mybatis的配置檔案 --> 
<context-param> 
<param-name>contextConfigLocation</param-name> 
<param-value>classpath:spring-mybatis.xml</param-value> 
</context-param>

<!-- Spring-shiro的配置檔案 --> 

<param-name>contextConfigLocation</param-name> 
<param-value>classpath:spring-shiro.xml</param-value> 
</context-param>

正確寫法

 <!-- Spring和mybatis的配置檔案與 Spring-shiro的配置檔案 -->  
    <context-param>  
        <param-name>contextConfigLocation</param-name>  
        <param-value>classpath:spring-mybatis.xml,classpath:spring-shiro.xml</param-value>  
    </context-param> 
    

 

相關文章