【问题标题】:cannot load css and js files when using spring mvc使用spring mvc时无法加载css和js文件
【发布时间】:2015-08-03 06:52:08
【问题描述】:

我是 Spring MVC 的新手。加载资源我使用 < mvc:resources mapping="/design/**" location="/design/" />。虽然这工作得很好,但现在不是。我无法理解这个问题。谁能帮我?

这是我的 spring-dispatcher-servlet.xml。 我的资源(设计)文件夹位于 WebContent 中,它有名为 css、js 和 images 的子文件夹。

< beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
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
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/data/mongo
http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd">
<context:component-scan base-package="controller"/> 

<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix">
        <value>/</value>
    </property>
    <property name="suffix">
        <value>.jsp</value>
    </property>
</bean> 

<bean id="viewResolverhtml" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix">
        <value>/</value>
    </property>
    <property name="suffix">
        <value>.html</value>
    </property>
</bean> 

<bean id="mongo" class="org.springframework.data.mongodb.core.MongoFactoryBean">
    <property name="host" value="localhost"/>
</bean>

<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
    <constructor-arg name="mongo" ref="mongo"/>
    <constructor-arg name="dabaseName" ref="testdb"/>
</bean>

<mongo:repositories base-package="model"/>

<mvc:view-controller path="/" view-name="index"/>
<mvc:resources mapping="/design/**" location="/design/" />

<mvc:annotation-driven/>

这就是我在 index.jsp 中加载资源的方式

<link rel="stylesheet" href="design/css/reset.css" type="text/css" media="screen">
<link rel="stylesheet" href="design/css/style.css" type="text/css" media="screen">
<link rel="stylesheet" href="design/css/grid.css" type="text/css" media="screen">
<script src="design/js/jquery-1.6.3.min.js" type="text/javascript"></script>
<script src="design/js/cufon-yui.js" type="text/javascript"></script>
<script src="design/js/cufon-replace.js" type="text/javascript"></script>
<script src="design/js/Kozuka_Gothic_Pro_OpenType_300.font.js" type="text/javascript"></script>
<script src="design/js/Kozuka_Gothic_Pro_OpenType_500.font.js"  type="text/javascript"></script>
<script src="design/js/FF-cash.js" type="text/javascript"></script>
<script type="text/javascript" src="design/js/jquery.easing.1.3.js"></script>
<script type="text/javascript" src="design/js/tms-0.3.js"></script>
<script type="text/javascript" src="design/js/tms_presets.js"></script>
<script src="design/js/jcarousellite_1.0.1.js" type="text/javascript"></script>

【问题讨论】:

    标签: java spring-mvc


    【解决方案1】:

    尝试以下任何一种方法:

    带有 JSTL 标签 c:url

    <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <link href="<c:url value="/design/css/reset.css" />" rel="stylesheet">
    

    用 spring:url

    <%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
    <spring:url value="/design/css/reset.css" var="reset" />
    

    没有任何标签

    <link href="${pageContext.request.contextPath}/design/css/reset.css" rel="stylesheet"
    

    【讨论】:

      猜你喜欢
      • 2017-03-08
      • 1970-01-01
      • 2017-10-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多