Visualizzazione dei risultati da 1 a 9 su 9
  1. #1

    [Spring] Problemi con resources

    ciao!

    ho un problema con il mapping delle resources in spring.
    a livello di META-INF e WEB-INF ho creato una directory resources, e dentro ci ho messo i vari css e js in varie sotto directory.
    poi nel dispatcher-servlet:
    codice:
    <?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"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    
        <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>
    
        <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
            <property name="mappings">
                <props>
                    <prop key="index.html">indexController</prop>
                </props>
            </property>
        </bean>
    
        <bean id="viewResolver"
              class="org.springframework.web.servlet.view.InternalResourceViewResolver"
              p:prefix="/WEB-INF/jsp/"
              p:suffix=".jsp" />
    
        <bean name="indexController"
              class="org.springframework.web.servlet.mvc.ParameterizableViewController"
              p:viewName="index" />
        
        <mvc:resources mapping="/resources/**" location="/resources/persona/Template/Light_Version/" />
        <mvc:annotation-driven />
        
    </beans>
    a questo punto nella pagina ho fatto questo:
    codice:
    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    <%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
    <!DOCTYPE html>
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <title>Home</title>
            <spring:url value="/resources/css/framework.css" var="framework_css" />
            <link href="${framework_css}" rel="stylesheet">
        </head>
        <body>
    
    </body>
    </html>
    ma non trova il css.

    dove sto sbagliando??

  2. #2
    Utente di HTML.it L'avatar di andbin
    Registrato dal
    Jan 2006
    residenza
    Italy
    Messaggi
    18,284
    Quote Originariamente inviata da fermat Visualizza il messaggio
    codice:
        <mvc:resources mapping="/resources/**" location="/resources/persona/Template/Light_Version/" />
    codice:
            <spring:url value="/resources/css/framework.css" var="framework_css" />
            <link href="${framework_css}" rel="stylesheet">
    ma non trova il css.
    Personalmente non ho mai usato <spring:url> (credo sia di base simile al <c:url> di JSTL che ho già usa sovente) ma a prima vista mi pare sensato.

    Il file framework.css dovrebbe quindi trovarsi fisicamente in /resources/persona/Template/Light_Version/css/framework.css dove / è la "context root".
    Andrea, andbin.devSenior Java developerSCJP 5 (91%) • SCWCD 5 (94%)
    java.util.function Interfaces Cheat SheetJava Versions Cheat Sheet

  3. #3
    stavo usando acche io <c:url>, ho provato <spring:url> giusto per capire se stavo sbagliando io.

    cmq così funziona:
    codice:
    <link href="<c:url value="/resources/persona/Template/Light_Version/css/framework.css" />" rel="stylesheet">
    però allora non riesco a capire perchè dovrei mettere mvc:resources così:
    codice:
    <mvc:resources mapping="/resources/**" location="/resources/persona/Template/Light_Version/" />
    cioè, l'attributo location non serve proprio per evitare di mettere tutto il path ogni volta?

  4. #4
    Utente di HTML.it L'avatar di Alex'87
    Registrato dal
    Aug 2001
    residenza
    Verona
    Messaggi
    5,802
    Premetto che Spring Web lo conosco gran poco...

    Hai provato a togliere la barra che ho messo in grassetto?

    <spring:url value="/resources/css/framework.css" var="framework_css" />
    SpringSource Certified Spring Professional | Pivotal Certified Enterprise Integration Specialist
    Di questo libro e degli altri (blog personale di recensioni libri) | ​NO M.P. TECNICI

  5. #5
    ciao!

    si ho provato.
    in entrambi in modi:
    codice:
    <link rel="stylesheet" type="text/css" href="<c:url value="resources/css/framework.css" />">
    <spring:url value="resources/css/framework.css" var="framework_css" />
    <link href="${framework_css}" rel="stylesheet">
    niente da fare!

  6. #6
    la cosa che poi continuo a non capire è questa.
    se dentro metto il file css dentro /resources/css e cambio il mappgin così:
    codice:
    <mvc:resources mapping="/resources/**" location="/resources/" />
    poi posso fare così:
    codice:
    <link rel="stylesheet" type="text/css" href="<c:url value="resources/css/framework.css" />">
    a questo punto mi viene il dubbio che il problema siano le sotto directory.

  7. #7
    ok ho capito.

    ho modificato così:
    codice:
    <mvc:resources mapping="/Light_Version/**" location="/resources/persona/Template/Light_Version/" />
    non avevo capito il funzionamento del mapping.

    EDIT: no era rimasto memoria il vecchio CSS, continuo a cercare...
    Ultima modifica di fermat; 12-04-2016 a 09:25

  8. #8
    allora, giusto per fare ordine, alla fine della fiera io sto messo così.
    i file che mi servono (css, js, immagini) stanno dentro a: /resources/persona/Template/Light_Version/

    la directory resources sta al livello di root:
    - META-INF
    - WEB-INF
    - resources
    - redirect.jsp

    nel dispatcher-servlet ho messo così:
    codice:
    <mvc:resources mapping="/res/**" location="/resources/persona/Template/Light_Version/" />
    nella pagina ho fatto i tentativi in vari modi, tipo:

    codice:
    <link rel="stylesheet" type="text/css" href="<c:url value="/res/css/framework.css" />">
    oppure
    codice:
    <link rel="stylesheet" type="text/css" href="<c:url value="res/css/framework.css" />">
    non funziona nessuno dei due casi.
    se metto il path completo ovviamente funziona:
    codice:
    <link rel="stylesheet" type="text/css" href="<c:url value="/resources/persona/Template/Light_Version/css/framework.css" />">
    il che significherebbe che il path è giusto in teoria...

    ho provato facendo vari Build&Clean, con l'undeploy/deploy manuale (senza l'automatismo dell'ide per intenderci), ma niente da fare.

    sennò pace, userò i path completi.

  9. #9
    come mi hai indirittamente suggerito da un'altra parte, penso che il problem risiede nel dispatcher-servlet principalmente.
    modificandolo in questo modo (e modificando il controller, il web.xml per far funzionare il resto), adesso funziona:
    codice:
    <?xml version='1.0' encoding='UTF-8' ?>
    <!-- was: <?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.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx.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.package" />
        <mvc:annotation-driven />
        
        <bean id="viewResolver"
              class="org.springframework.web.servlet.view.InternalResourceViewResolver"
              p:prefix="/WEB-INF/jsp/"
              p:suffix=".jsp" />
        
        <mvc:resources mapping="/resources/**" location="/resources/persona/Template/Dark_Version/" />
    </beans>
    tra le varie cose, non ho più il SimpleUrlHandlerMapping.

    ciao!!

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.