2016-04-05 8 views
0

Ich bin ziemlich neu in Spring MVC und ich habe das folgende Problem in einem Spring MVC-Projekt, das FreeMarker für meine Ansichten verwenden.Warum kann ich nicht auf die CSS-Ressourcen zugreifen, die im Ordner webapp resources dieser Spring MVC-Anwendung definiert sind?

Ich finde einige Probleme beim Importieren einiger Ressourcen (CSS-Dateien und Bilder definiert in der myProject\src\main\webapp\resources\ Verzeichnis meines Projekts).

So habe ich diese header.ftl-Datei, die den Header alle meine Seiten darstellen:

<!DOCTYPE html> 
<!--[if IE 8]><html class="no-js is-ie8"><![endif]--> 
<!--[if gt IE 8]><!--><html class="no-js"><!--<![endif]--> 
    <head> 
     <meta charset="utf-8"> 
     <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> 
     <meta name="viewport" content="width=device-width, initial-scale=1"> 
     <title>MY WEBSITE</title> 
     <meta name="robots" content="noindex,nofollow"> 

     <link rel="stylesheet" href="resources/css/webfont.css"> 
     <link rel="stylesheet" href="resources/bootstrap/css/bootstrap.min.css"> 
     <link rel="stylesheet" href="resources/bootstrap/css/bootstrap-theme.min.css"> 
     <link rel="stylesheet" href="resources/plugins/bs-select/bootstrap-select.min.css"> 
     <link rel="stylesheet" href="resources/plugins/bs-dialog/bootstrap-dialog.min.css"> 
     <link rel="stylesheet" href="resources/css/style.css" /> 

</head> 

<body id="main"> 
    <div class="container page-header"> 
     <h1>My WebSite</h1> 
    </div> 

So, wie Sie sehen können, habe ich einige CSS-Ressourcen am importieren, die in myProject\src\main\webapp\resources\ Verzeichnis in meinem Projekt definiert sind.

Das Problem ist, dass diese Ressourcen nicht in meinem Projekt importiert werden (so habe ich keine CSS-Einstellungen).

Dies wird die gerenderte Ausgabe für die bisherigen Ressourcen CSS den erzeugten HTML-Code in FIRFOX suchen:

<link rel="stylesheet" href="resources/css/webfont.css"> 
    <link rel="stylesheet" href="resources/bootstrap/css/bootstrap.min.css"> 
    <link rel="stylesheet" href="resources/bootstrap/css/bootstrap-theme.min.css"> 
    <link rel="stylesheet" href="resources/plugins/bs-select/bootstrap-select.min.css"> 
    <link rel="stylesheet" href="resources/plugins/bs-dialog/bootstrap-dialog.min.css"> 
    <link rel="stylesheet" href="resources/css/style.css" /> 

Die einzige Datei Konfiguration Frühling xml in diesem Projekt Web-context.xml genannt wird (es ist wo Freemarker konfiguriert ist, und wo ist die Komponente Scan für mein Controller) speciied, dieses:

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns="http://www.springframework.org/schema/beans" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:sec="http://www.springframework.org/schema/security" 

    xsi:schemaLocation=" 
    http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context.xsd 
    http://www.springframework.org/schema/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc.xsd 
    http://www.springframework.org/schema/security 
    http://www.springframework.org/schema/security/spring-security.xsd 
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop.xsd"> 

    <context:annotation-config /> 

    <!-- Attivazione dei Proxy tramite aspetto --> 
    <aop:aspectj-autoproxy /> 

    <context:property-placeholder location="classpath*:*.properties" 
     ignore-resource-not-found="true" /> 

    <!-- Controller --> 
    <context:component-scan base-package="it.istruzione.iam.ssum.registrazione"> 
    </context:component-scan> 

    <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory --> 
    <mvc:resources mapping="/resources/**" location="/resources/" /> 

    <!-- Configurazione del resolver FreeMarker --> 
    <!-- freemarker config --> 
    <bean id="freemarkerConfig" 
     class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer"> 
     <property name="templateLoaderPath" value="/WEB-INF/freemarker/" /> 
    </bean> 

    <bean id="viewResolver" 
     class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver"> 
     <property name="cache" value="true" /> 
     <property name="prefix" value="" /> 
     <property name="suffix" value=".ftl" /> 
    </bean> 

    <!-- Configurazione MVC --> 
    <mvc:annotation-driven /> 

</beans> 

Wie Sie sehen, ich hinzufügen, haben thi s Linie zu versuchen, die Anfrage zu dem Inhalt des /resources/ Verzeichnisses zu handhaben:

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

Aber es immer noch nicht funktioniert und ich kann nicht auf meine CSS Ressourcen platziert in /resources/ Verzeichnisse zuzugreifen.

Wenn ich die vorherige Seite in den Browser zeigen, in die Eclipse-Konsole ich folgende Fehlermeldungen zu diesen Ressourcen im Zusammenhang erhalten:

16:55:40,249 ERROR [org.jboss.as] (Controller Boot Thread) JBAS015875: JBoss EAP 6.2.0.GA (AS 7.3.0.Final-redhat-14) started (with errors) in 13261ms - Started 176 of 240 services (3 services failed or missing dependencies, 59 services are passive or on-demand) 
16:55:42,623 WARN [org.springframework.web.servlet.PageNotFound] (http-localhost/127.0.0.1:8080-1) No mapping found for HTTP request with URI [/iam-ssum-registrazione-web/] in DispatcherServlet with name 'dispatcher' 
16:57:14,624 WARN [org.springframework.web.servlet.PageNotFound] (http-localhost/127.0.0.1:8080-1) No mapping found for HTTP request with URI [/iam-ssum-registrazione-web/iscrizione/resources/css/webfont.css] in DispatcherServlet with name 'dispatcher' 
16:57:14,640 WARN [org.springframework.web.servlet.PageNotFound] (http-localhost/127.0.0.1:8080-1) No mapping found for HTTP request with URI [/iam-ssum-registrazione-web/iscrizione/resources/css/style.css] in DispatcherServlet with name 'dispatcher' 
16:57:14,658 WARN [org.springframework.web.servlet.PageNotFound] (http-localhost/127.0.0.1:8080-6) No mapping found for HTTP request with URI [/iam-ssum-registrazione-web/iscrizione/assets/js/modernizr-custom.js] in DispatcherServlet with name 'dispatcher' 
16:57:14,659 WARN [org.springframework.web.servlet.PageNotFound] (http-localhost/127.0.0.1:8080-3) No mapping found for HTTP request with URI [/iam-ssum-registrazione-web/iscrizione/resources/bootstrap/css/bootstrap-theme.min.css] in DispatcherServlet with name 'dispatcher' 
16:57:14,661 WARN [org.springframework.web.servlet.PageNotFound] (http-localhost/127.0.0.1:8080-4) No mapping found for HTTP request with URI [/iam-ssum-registrazione-web/iscrizione/resources/plugins/bs-select/bootstrap-select.min.css] in DispatcherServlet with name 'dispatcher' 
16:57:14,664 WARN [org.springframework.web.servlet.PageNotFound] (http-localhost/127.0.0.1:8080-5) No mapping found for HTTP request with URI [/iam-ssum-registrazione-web/iscrizione/resources/plugins/bs-dialog/bootstrap-dialog.min.css] in DispatcherServlet with name 'dispatcher' 
16:57:14,664 WARN [org.springframework.web.servlet.PageNotFound] (http-localhost/127.0.0.1:8080-2) No mapping found for HTTP request with URI [/iam-ssum-registrazione-web/iscrizione/resources/bootstrap/css/bootstrap.min.css] in DispatcherServlet with name 'dispatcher' 

Warum? Was ist falsch? Was vermisse ich? Wie kann ich dieses Problem beheben?

Antwort

0

Sie verwenden können JSTL `, um sicherzustellen, dass Ihre URLs korrekt sind:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 
<link href="<c:url value="/resources/css/webfont.css" />" rel="stylesheet"> 

bearbeiten sorry ich es nicht bemerken, ist eine Freemarker Seite:

<#import "/spring.ftl" as spring /> 

<link href="<@spring.url '/resources/css/webfont.css'/>" rel="stylesheet"> 
+0

Kann ich wirklich JSTL verwenden in einer FreeMarker-Seite? Ist das nicht etwas mit der JSP-Seite zu tun? Es scheint mir verdrahtet – AndreaNobili

+0

Ok, aber ich denke, dass es klarer ist, konfigurieren Sie die Ressourcen-Mapping in die Spring XML-Konfiguration, aber es kann nicht funktionieren – AndreaNobili

+0

@AndreaNobili, wenn Sie direkt aufrufen: http: // localhost: xxxx/yourProject/Ressourcen/css/webfont.css' was bekommst du? –