解决ssm拦截静态资源的问题

来源:互联网 发布:域名具有特性 编辑:程序博客网 时间:2024/05/22 00:06

SpringMVC框架中, jsp页面引用的js,css,html等静态文件在运行时出现404错误,也就是说找不到文件路径;

    我在引用的时候写的是相对路径,并且路径是完全正确的,那为什么会出现这种错误呢?

    一.现象:

      1.如果你的配置描述符文件(web.xml)中,DispatcherServlet拦截为"*.do"这样的有后缀的URL,就不存在访问不

       到静态资源的现象。

      2.如果你的配置描述符文件(web.xml)中,DispatcherServlet拦截为"/",就会出现访问不到的的现象;

    二.解决办法:

      1.方案一:激活Tomcat的defaultServlet来处理静态文件,当然要写在DispatcherServlet的前面, 让defaultServlet先拦截请求,

        Tomcat, Jetty, jboss, and GlassFish 自带的默认Servlet的名字 -- "default"

<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"      xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"      id="SpringMVCDemo" version="2.5">      <display-name>SpringMVCDemo</display-name>      <welcome-file-list>          <welcome-file>index.html</welcome-file>          <welcome-file>index.htm</welcome-file>          <welcome-file>index.jsp</welcome-file>          <welcome-file>default.html</welcome-file>          <welcome-file>default.htm</welcome-file>          <welcome-file>default.jsp</welcome-file>      </welcome-file-list>      <!-- 初始化spring容器 -->      <context-param>          <param-name>contextConfigLocation</param-name>          <param-value>classpath:spring/applicationContext-*.xml</param-value>      </context-param>      <listener>          <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>      </listener>          <context-param>          <param-name>spring.profiles.active</param-name>          <param-value>dev</param-value>      </context-param>      <context-param>          <param-name>spring.profiles.default</param-name>          <param-value>dev</param-value>      </context-param>      <context-param>          <param-name>spring.liveBeansView.mbeanDomain</param-name>          <param-value>dev</param-value>      </context-param>        <!-- 解决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>          <servlet-mapping>    <servlet-name>default</servlet-name>    <url-pattern>*.js</url-pattern>  </servlet-mapping>    <!-- springmvc的前端控制器 -->      <servlet>          <servlet-name>SpringMVCDemo</servlet-name>          <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>          <!-- contextConfigLocation不是必须的, 如果不配置contextConfigLocation, springmvc的配置文件默认在:WEB-INF/servlet的name+"-servlet.xml" -->          <init-param>              <param-name>contextConfigLocation</param-name>              <param-value>classpath:spring/springmvc.xml</param-value>          </init-param>          <load-on-startup>1</load-on-startup>      </servlet>      <servlet-mapping>          <servlet-name>SpringMVCDemo</servlet-name>          <url-pattern>/</url-pattern>      </servlet-mapping>  </web-app>



2.方案二:把拦截的"/",换成带后缀的拦截,如果换成带后缀的,所有的请求也要带这个后缀,否则请求就会被过滤掉;

    三.究其原因:

    1.如果DispatcherServlet拦截为"*.do",那么系统只会对.do为后缀的请求进行拦截;如果DispatcherServlet拦截为"/",将对所有的文

     件(包括js css html)进行拦截;

    2.spring为了实现rest风格,当静态文件被DispatcherServlet拦截之后,是不能通过,所以就访问不到;

    rest具体的讲解可以参考如下: http://blog.sina.com.cn/s/blog_616e189f0100snfg.html

点击打开链http://blog.csdn.net/wangyi201212/article/details/50404101接

3.方案三:你还可以在springmvc.xml文件中配置静态资源访问的问题


<!--静态资源拦截问题  -->    <mvc:resources location="/js/" mapping="/js/**"/>    <mvc:resources location="/lang/" mapping="/lang/**"/>


0 0
原创粉丝点击