【问题标题】:unable to use resources like css/js in spring mvc无法在spring mvc中使用css/js之类的资源
【发布时间】:2016-04-19 13:52:16
【问题描述】:

CSSJS 没有在我的应用程序中呈现-

这是我的Dispatcher.xml-

<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"
    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-4.0.xsd
   http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context.xsd">

    <context:annotation-config />
    <context:component-scan base-package="com.ttnd.springdemo.controller" />
    <mvc:resources location="/resources/" mapping="/resources/**" />
    <mvc:annotation-driven />
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/WEB-INF/views/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>
</beans>

下面是我正在使用的页面css/js-

我尝试过各种方法,其中一些是-

<link rel="stylesheet" type="text/css" href="css/bootstrap.css">
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="css/application.css">
<script src="js/application.js"></script>
<script src="js/bootstrap.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="js/jquery.min.js"></script>
</head>

或-

<link rel="stylesheet" type="text/css" href="<spring:url value='resources/css/application.css' />" />
<script src="<spring:url value='resources/js/application.js' />"></script>
<script src="<spring:url value='resources/js/jquery.min.js' />"></script>

【问题讨论】:

    标签: java spring maven jsp spring-mvc


    【解决方案1】:

    我使用

    包含静态文件

    对于CSS

    <spring:url value="/resources/css/bootstrap.min.css" var="bootstrapCss" />
    <link href="${bootstrapCss}" rel="stylesheet" />
    

    对于js

    <spring:url value="/resources/js/jquery-1.11.1.min.js" var="jqueryJs" />
    <script src="${jqueryJs}"></script>
    

    并在jsp中包含以下标签

    <%@ page isELIgnored="false" %>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
    <%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
    

    希望对你有帮助

    【讨论】:

      【解决方案2】:

      我的 css 也有类似的问题,我修复它的方式是......

      <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/resources/css/main.css">
      

      如果您想测试它是否正在读取您的 css,您可以尝试通过浏览器访问 css 文件,方法是使用以下 url 或类似于http://localhost:8080/spring/resources/css/main.css 的内容。这应该会在您的浏览器中显示您的 css 文件

      【讨论】:

        【解决方案3】:

        我也有类似的问题。通过以下方式修复它:-

        1. 将资源放入webapp/resources 目录。
        2. 评论&lt;mvc:resources location="/resources/" mapping="/resources/**" /&gt;

        希望它也对你有用。

        编辑

        我包含我的 JS/CSS Like

        <link href="resources/css/global.css">
        <script type="text/javascript" src="resources/js/myscript.js"></script>
        

        【讨论】:

        • 这仍然在webapp/resources 目录中。之前在dispatcher.xml 中没有mvc:resources,但这是/不工作。你可以在你的答案中添加这些css/js 的路径编辑它?
        猜你喜欢
        • 2017-01-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-04-07
        • 2020-08-24
        • 2012-09-26
        相关资源
        最近更新 更多