页面在IDEA项目目录不能正确加载css/js等文件的解决方法

   

前提:学校实验室的网站项目,用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>





评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值