2017-02-25 226 views
0

我正在用Spring MVC創建一個示例應用程序。我的問題是我的靜態資源如css和javascript沒有加載。請任何人都請幫助我。這是我的第一個spring application.Any幫助將是非常可觀的....無法加載靜態資源:Spring MVC

下面是我的代碼...

的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; 
} 

MyFolder文件結構 enter image description here

回答

1

更換<mvc:resources mapping="/resources/**" location="/resources/" />

<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" /> 
+0

我想這一點,但它sti將不會加載資源 – Miller

+0

分享您的目錄結構 – mhshimul

+0

我將我的文件夾結構添加到我的問題 – Miller