【问题标题】:Unable to load static resources:Spring MVC无法加载静态资源:Spring MVC
【发布时间】:2017-02-25 04:09:27
【问题描述】:

我正在使用 Spring MVC 创建一个示例应用程序。我的问题是我的静态资源(如 css 和 javascript)没有加载。谁能帮助我。这是我的第一个 spring 应用程序。任何帮助都将非常明显...... .

下面是我的代码...

login.jsp

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

<html>

<head>

<spring:url value="/resources/common/css/login.css" var="loginCSS" />
<spring:url value="/resources/common/js/login.js" var="loginJS" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>

<link href="${loginCSS}" rel="stylesheet" />
<script src="${loginJS}"></script>

</head>

<body>

<h2>Sample spring mvc</h2>
<div class="test">
  <div class="form">

    <form class="test">
      <input type="text" placeholder="username"/>
      <input type="password" placeholder="password"/>
      <button>login</button>
      <p class="message">Not registered? <a href="#">Create an account</a></p>
    </form>
  </div>
</div>

</body>
</html>

示例-servlet

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    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">

    <context:component-scan base-package="com.sample.controller" />

    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/views/" />
        <property name="suffix" value=".jsp" />

    </bean>
    <mvc:resources mapping="/resources/**" location="/resources/" />
    <!-- <mvc:default-servlet-handler/> -->
    <mvc:annotation-driven />

</beans>

login.css

.test {
    background-color: red;
}

我的文件夹结构

【问题讨论】:

    标签: javascript java jquery css spring-mvc


    【解决方案1】:

    &lt;mvc:resources mapping="/resources/**" location="/resources/" /&gt; 替换为

    <mvc:resources mapping="/resources/css/**" location="/resources/common/css/" />        
    <mvc:resources mapping="/resources/js/**" location="/resources/common/js/" />
    

    在你的jsp中这样使用:

    <spring:url value="/resources/css/login.css" var="loginCSS" />
    <spring:url value="/resources/js/login.js" var="loginJS" />
    

    【讨论】:

    • 我试过了,但它仍然没有加载资源
    • 分享你的目录结构
    • 我将文件夹结构添加到我的问题中
    • 将资源文件夹移到 WEB-INF 之外,在 WebContent 内部
    猜你喜欢
    • 2013-07-18
    • 2019-01-31
    • 1970-01-01
    • 2022-09-30
    • 1970-01-01
    • 2014-10-15
    • 1970-01-01
    • 2020-10-14
    • 1970-01-01
    相关资源
    最近更新 更多