您的位置:首页 > 其它

配置了<mvc:default-servlet-handler />还是不能访问静态资源

2017-09-21 13:17 561 查看
解决方法:

今天遇到这个问题,最终发现是路径中有跟视图名相同的部分。

比如说:我有个视图名叫index,然后我想访问webapp/index.html

就会出现访问不到静态资源的情况。

我建了一个webapp/static/index.html的文件夹和文件,

访问:webapp/static/index.html就可以了

猜想原因:

静态资源不能存放在webapp这个路径下,否则访问webapp/index.html会转换到webapp/WEB-INF/views/index.jsp

视图名和静态资源存放的路径不应该有一致的部分,否则如何确定先映射动态还是静态资源,比如

视图映射:/index/*

静态路径:webapp/index/index.html

这个时候访问/index/index.html会转访问webapp/WEB-INF/views/index.jsp

所以/*要慎用

视图映射为:/index,访问上述路径并不会出现问题

web.xml配置如下

<?xml version="1.0" encoding="UTF-8"?>
<web-app
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=" http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">

<display-name>springmvcdemo</display-name>

<!-- 解决中文乱码:统一编码UTF-8 -->
<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>

<!-- SpringMVC前端控制器  -->
<servlet>
<servlet-name>springmvcdemo</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<!-- 注意这里不能为 /* -->
<servlet-name>springmvcdemo</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>


springmvcdemo-servlet.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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

<!--扫描web层的@Controller-->
<context:component-scan base-package="springmvcdemo"/>

<!-- 解除对静态资源访问的限制,但是记住静态资源的路径不能有视图名  -->
<mvc:default-servlet-handler/>

<!-- 主页映射 -->
<mvc:view-controller path="/" view-name="home"/>

<mvc:annotation-driven enable-matrix-variables="true">
<!-- 全局设定响应内容为UTF-8编码 -->
<mvc:message-converters register-defaults="true">
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<constructor-arg value="UTF-8"></constructor-arg>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>

<!-- 页面匹配 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐