1. 程式人生 > >關於springMVC中controller與靜態資源不能同時訪問的問題

關於springMVC中controller與靜態資源不能同時訪問的問題

發現問題
在引用layui的時候發現layui樣式不能夠成功應用,以為是路徑的問題,最終發現其實是配置中攔截了所有的斜槓 / 的請求,

<!-- spring mvc servlet -->
    <servlet>
        <description>spring mvc servlet</description>
        <servlet-name>springMvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class
>
<init-param> <description>spring mvc 配置檔案</description> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mvc.xml</param-value> </init-param> <load-on-startup>
1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springMvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>

包括靜態資源同樣被攔截。
於是百度解決了這個問題,就是在springMVC的配置檔案中加上一條:
解決問題

<!-- 解除servlet對靜態資原始檔訪問的限制 -->
<mvc:default-servlet-handler />

發現問題
這樣一來layui是成功引用了,但是又不能訪問controller 裡的@RequestMapping了,刪了上面這行程式碼又是可以成功訪問RequestMapping的,於是陷入了二者不可兼得的境地。
解決問題

最終搜尋各種資源,得以解決:
在springMVC的配置檔案中再加上一條:

<!-- 預設的註解對映 -->
    <mvc:annotation-driven />

最終兩者都可以成功訪問了。