2016-12-10 160 views
0

我發現從資源目錄連接CSS和JS文件時出現問題。Netbeans Spring找不到資源文件

目錄結構:

enter image description here

JSP文件:

<%@taglib prefix="spring" uri="http://www.springframework.org/tags" %> 

<%@page contentType="text/html" pageEncoding="UTF-8"%> 
<!DOCTYPE html> 
<html> 
    <head> 
    <spring:url value="/resources/css/style.css" var="mainCss" /> 

    <meta charset="utf-8"> 
    <meta http-equiv="X-UA-Compatible" content="IE=edge"> 
    <title>Project Name</title>   
    <link href="${mainCss}" rel="stylesheet" /> 

    </head> 
    <body> 
    </body> 
</html> 

applicationContext.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:aop="http://www.springframework.org/schema/aop" 
      xmlns:tx="http://www.springframework.org/schema/tx" 
      xmlns:mvc="http://www.springframework.org/schema/mvc" 
      xmlns:context="http://www.springframework.org/schema/context" 
      xsi:schemaLocation="http://www.springframework.org/schema/beans  
      http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 
      http://www.springframework.org/schema/aop  http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 
      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.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-4.0.xsd"> 


    <context:annotation-config /> 
    <context:component-scan base-package="com.controller"/> 
    <mvc:default-servlet-handler/> 
    <mvc:annotation-driven/>  

    <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> 
     <property name="basename" value="messages" /> 
    </bean> 
</beans> 

當我檢查什麼是在樣式表文件中的控制檯,我發現:

Resource interpreted as Stylesheet but transferred with MIME type text/html: "http://localhost:8080/ProjectName/resources/css/style.css".

路徑重定向到應用程序索引。

你有什麼想法如何解決它?是什麼導致了問題?

+0

/資源不應該在路徑當你寫的頁面標記。您需要將應用程序打包到WAR中,並將CSS和JavaScript放在上下文根目錄下。 – duffymo

回答

0

你可以嘗試改變

<spring:url value="/resources/css/style.css" var="mainCss" /> 

<spring:url value="./resources/css/style.css" var="mainCss" />