springmvc无法访问JS,CSS等文件

配置好web.xml中的dispatchservlet后,js,css,都不能正常显示

web.xml配置文件

<!-- 核心控制器 -->  
<servlet>  
      <servlet-name>springmvc</servlet-name>  
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
      <init-param>  
          <param-name>contextConfigLocation</param-name>  
          <param-value>/WEB-INF/applicationContext.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>  

 

url-pattern有5种配置模式:  

(1)/xxx:完全匹配/xxx的路径

(2)/xxx/*:匹配以/xxx开头的路径,请求中必须包含xxx。

(3)/*:匹配/下的所有路径,请求可以进入到action或controller,但是转发jsp时再次被拦截,不能访问jsp界面。

(4).xx:匹配以xx结尾的路径,所有请求必须以.xx结尾,但不会影响访问静态文件。

(5)/:默认模式,未被匹配的

springmvc配置文件

<mvc:annotation-driven>
<mvc:resources location="/css/" mapping="/css/**"/>
<mvc:resources location="/" mapping="/*.html"/>

<resources/>元素指示SpringMVC哪些静态资源需要单独处理(不通过dispatcherServlet),第一确保在/css目录下的所有文件可见,第二个允许显示所有的.html文件。

注意: 如果没有<mvc:annotation-driven>, <resources/>元素会阻止任意控制器被调用, 若不需要使用resources,则不需要<mvc:annotation-driven>元素

猜你喜欢

转载自www.cnblogs.com/guo-rong/p/9197131.html