2016-12-23 232 views
0

我已經搜索過了,但沒有得到相同的解決方案。我得到的信息,即我們必須使用<mvc:resources location="/resources/" mapping="/resources/**"/>並把所有的靜態成員如CSS/IMG/JS資源文件夾內spring MVC MAVEN:靜態資源用404

src 

|--main 

| | 

| |--java 

| |--resources 

| |--webapp 

然後,我們可以選擇使用JSTL或彈簧標籤的資源,如下圖:

<head> 
    <%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%> 
    <spring:url value="/resources/LoginForm.css" var="loginCss" /> 
    <link rel="stylesheet" href="${loginCss}" type="text/css" /> 
</head> 

我已經開發了一些代碼,記住了所有的要點,但獲取靜態資源的404錯誤。 >網絡 - 調度員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-3.0.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd 
    http://www.springframework.org/schema/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 
    "> 
    <mvc:annotation-driven/> 
    <context:component-scan base-package="com.anand" /> 
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <property name="prefix"> 
      <value>/WEB-INF/views/</value> 
     </property> 
     <property name="suffix"> 
      <value>.jsp</value> 
     </property> 
    </bean> 

    <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"> 
     <property name="basename" value="/WEB-INF/messages" /> 
    </bean> 
    <mvc:resources location="/resources/" mapping="/resources/**"/> 
    <mvc:default-servlet-handler /> 


</beans> 

代碼loginForm.jsp中

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
    pageEncoding="ISO-8859-1"%> 
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%> 
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> 
<title>Home-Login</title> 
<spring:url value="/resources/LoginForm.css" var="loginCss" /> 
<spring:url value="/resources/logo1.PNG" var="logo1" /> 
<spring:url value="/resources/img1.png" var="img1" /> 
<spring:url value="/resources/img2.png" var="img2" /> 
<spring:url value="/resources/img3.png" var="img3" /> 

<link rel="stylesheet" href="${loginCss}" type="text/css" /> 
</head> 
<body class="login"> 
<!-- header starts here --> 
<div id="facebook-Bar"> 
    <div id="facebook-Frame"> 
    <div id="logo"> <a href="#"><img src="${logo1}" /></a></div> 
    <div id="header-main-right"> 
     <div id="header-main-right-nav"> 
     <form:form action="loginform.html" method="post" commandName="loginform"> 
     <table border="0" style="border:none"> 
      <tr> 
       <td ><form:input type="text" tabindex="1" path="userName" placeholder="UserName" class="inputtext radius1" value="" /></td> 
       <td ><form:input type="password" tabindex="2" path="password" placeholder="Password" class="inputtext radius1" /></td> 
       <td ><input type="submit" class="fbbutton" name="login" value="Login" /></td> 
      </tr> 
      <tr> 
       <td><label> 
        <input id="persist_box" type="checkbox" name="persistent" /> 
        <span style="color:#ccc;">Keep me logged in</span></label></td> 
       <td><label><a href="" style="color:#ccc; text-decoration:none">forgot your password?</a></label></td> 
      </tr> 
      </table> 
     </form:form> 
     </div> 
    </div> 
    </div> 
</div> 
<div class="slideshow-container" style="width:100%;height:80%; min-width:1000px;"> 

<div class="mySlides fade" style="width:100%;height:100%"> 
    <div class="numbertext">1/3</div> 
    <img src="${img1}" style="width:100%;height:100%" /> 
    <div class="text">Caption Text</div> 
</div> 

<div class="mySlides fade" style="width:100%;height:100%"> 
    <div class="numbertext">2/3</div> 
    <img src="${img2}" style="width:100%;height:100%" /> 
    <div class="text">Caption Two</div> 
</div> 

<div class="mySlides fade" style="width:100%;height:100%"> 
    <div class="numbertext">3/3</div> 
    <img src="${img3}" style="width:100%;height:100%" /> 
    <div class="text">Caption Three</div> 
</div> 

</div> 
<br/> 

<div style="text-align:center"> 
    <span class="dot"></span> 
    <span class="dot"></span> 
    <span class="dot"></span> 
</div> 

<script> 
var slideIndex = 0; 
showSlides(); 

function showSlides() { 
    var i; 
    var slides = document.getElementsByClassName("mySlides"); 
    var dots = document.getElementsByClassName("dot"); 
    for (i = 0; i < slides.length; i++) { 
     slides[i].style.display = "none"; 
    } 
    slideIndex++; 
    if (slideIndex> slides.length) {slideIndex = 1}  
    for (i = 0; i < dots.length; i++) { 
     dots[i].className = dots[i].className.replace(" active", ""); 
    } 
    slides[slideIndex-1].style.display = "block"; 
    dots[slideIndex-1].className += " active"; 
    setTimeout(showSlides, 2000); // Change image every 2 seconds 
} 
</script> 
</body> 
</html> 

當它重定向我loginForm.jsp中,我做F12

click here for project structure

代碼分析,它爲我顯示了靜態資源,如img1,img2,img3,logo1和.css文件。 Click Here for F12 debug and chrome view of page without css

回答

0

資源WEB-INF內的保護是不能訪問直接從客戶端。

一般來說,如果是靜態資源如html,CSS,那麼JavaScript可以放在WEB-INF之外。因此,放置在外面的靜態內容可以直接從客戶端瀏覽器下載。

請閱讀Referencing a resource placed in WEB-INF folder in JSP file returns HTTP 404 on resource

+0

除了'/ WEB-INF /班/ META-INF/resources' –

+0

@Clement ...感謝,這正是我做了什麼,在不知不覺中,它是爲我工作,我已經寫在我的回答,但後面的邏輯,我讀了你的解釋後得到的...非常感謝.. :) –

0

[[解決]]:終於解決了這個問題,但我不確定我在這裏使用的方法是否正確,但是它是爲我工作。解決方案:我已將資源文件夾複製到webapp文件夾下(即WEB_INF &資源是兄弟)。如果你有同樣的

Click here for updated directory structure image

請你提供的這個更好的解決方案: