前提:学校实验室的网站项目,用Spring Mvc搭建的。加入前端页面后,页面读取不到css js images等任何文件
最后终于在网上找到一个解决方向----(SpringMVC 中 需要配置 对静态资源文件的访问)
解决方法:
方法一,在 spring-servlet.xml 配置文件中加入
<mvc:default-servlet-handler/> 方法二,在 spring-servlet.xml 配置文件中加入
<mvc:resources mapping="/images/**" location="/images/" cache-period="31556926"/> <mvc:resources mapping="/js/**" location="/js/" cache-period="31556926"/> <mvc:resources mapping="/css/**" location="/css/" cache-period="31556926"/> 备注:加<mvc:resources...前,要加上<mvc:annotation-driver/>,否则servlet控件器会解析失败
用了看上去简单又高大上的第一种方法,加在 spring-servlet.xml 最下面一行,试验成功,撒花
--------------------------------------------------------------------我是分割线--------------------------------------------------------------------------------------
备注:初学Spring MVC, Spring-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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- 启用spring mvc 注解 --> <context:annotation-config/> <!-- 配置拦截器 --> <bean id="mainInterceptor" class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"> <property name="interceptors"> <bean class="cn.com.interceptor.LwInterceptor"></bean> </property> </bean> <!-- 配置jackson,可以以json格式做入参出参 --> <mvc:annotation-driven> <mvc:message-converters> <bean class="org.springframework.http.converter.StringHttpMessageConverter"/> <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/> </mvc:message-converters> </mvc:annotation-driven> <!-- 设置使用注解的类所在的jar包 --> <context:component-scan base-package="cn.com"/> <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources/ directory --> <mvc:resources mapping="/resources/**" location="/public/"/> <!-- 完成请求和注解POJO的映射 --> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/> <!-- 对转向页面的路径解析。prefix:前缀, suffix:后缀 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/" p:suffix=".jsp"/> <!-- 加载静态页面--> <mvc:default-servlet-handler/> </beans>