Visualizzazione dei risultati da 1 a 9 su 9
  1. #1
    Utente di HTML.it L'avatar di Ironmax
    Registrato dal
    Dec 2008
    Messaggi
    1,026

    Problema con Map in spring

    Salve.
    Ho fatto il seguente metodo:
    codice:
    @RequestMapping("/hi/{countryName}/{userName}")
    		public ModelAndView hiWorld(@PathVariable Map<String,String> pathVars) {
    			
    			String name = pathVars.get("userName");
    			String country = pathVars.get("countryName");
    			
    			ModelAndView model = new ModelAndView("HelloPage");
    			model.addObject("msg", "hello " + name + " You are from " + country);
    			return model; }
    Però ho un problema al recupero con il metodo get delle variabili:
    pathVars.get("userName");

    Come può essere risolto?
    Grazie

  2. #2
    Utente di HTML.it L'avatar di andbin
    Registrato dal
    Jan 2006
    residenza
    Italy
    Messaggi
    18,284
    Quale è questo "problema" di cui parli? A occhio, tecnicamente, mi sembra giusto. L'utilizzo di @PathVariable con un Map<String,String> è documentato e lecito.
    Andrea, andbin.devSenior Java developerSCJP 5 (91%) • SCWCD 5 (94%)
    java.util.function Interfaces Cheat SheetJava Versions Cheat Sheet

  3. #3
    Utente di HTML.it L'avatar di Ironmax
    Registrato dal
    Dec 2008
    Messaggi
    1,026
    Praticamente al get con intelsense con eclipse mi chiede un getClass ()
    Solo mettendo get mi chiede di fare un cast.
    Non mi da solo il metodo get come postato prima.
    Devo importare qualche libreria?
    Ultima modifica di Ironmax; 18-08-2016 a 00:00

  4. #4
    Utente di HTML.it L'avatar di andbin
    Registrato dal
    Jan 2006
    residenza
    Italy
    Messaggi
    18,284
    Quote Originariamente inviata da Ironmax Visualizza il messaggio
    Praticamente al get con intelsense con eclipse mi chiede un getClass ()
    Solo mettendo get mi chiede di fare un cast.
    Non mi da solo il metodo get come postato prima.
    Devo importare qualche libreria?
    Che Eclipse ti mostri i metodi applicabili man mano che scrivi il nome del metodo .. ok, è a questo che serve il Content Assist (IntelliSense è il termine usato negli IDE Microsoft ma il concetto è quello).

    Comunque è ovvio che se anche ti mostra getClass(), non è questo che ti serve ma il get(). E visto che la Map è parametrizzata, non serve alcun cast esplicito a String.

    Verifica al massimo che quel Map importato sia davvero java.util.Map e non "un altro" Map di chissà quale API. A parte questo, come ripeto il codice che hai postato mi pare tecnicamente corretto. Compila? Ci sono warning/errori?
    Andrea, andbin.devSenior Java developerSCJP 5 (91%) • SCWCD 5 (94%)
    java.util.function Interfaces Cheat SheetJava Versions Cheat Sheet

  5. #5
    Utente di HTML.it L'avatar di Ironmax
    Registrato dal
    Dec 2008
    Messaggi
    1,026
    Questo è il codice che non mi da più problemi, corretto come mi hai detto:


    codice:
    
    @RequestMapping("/hi/{countryName}/{userName}")
    
    
     public ModelAndView hiWorld(@PathVariable java.util.Map<String, String> pathVars) {
    
    
     String name = pathVars.get("userName");
    
    
     String country = pathVars.get("countryName");
    
    
     ModelAndView model = new ModelAndView("HelloPage");
    
    
     model.addObject("msg", "hello " + name + " You are from " + country);
    
    
     return model;
    
    
    }

    Questo è l'errore dopo l'esecuzione nel browser che ho ottento:
    codice:
    HTTP Status 500 - Request processing failed; nested exception is org.springframework.web.bind.annotation.support.HandlerMethodInvocationException: Failed to invoke handler method [public org.springframework.web.servlet.ModelAndView com.gontuseries.hellocontroller.HelloController.hiWorld(java.util.Map)]; nested exception is java.lang.IllegalStateException: Could not find @PathVariable [pathVars] in @RequestMapping

  6. #6
    Utente di HTML.it L'avatar di andbin
    Registrato dal
    Jan 2006
    residenza
    Italy
    Messaggi
    18,284
    Quote Originariamente inviata da Ironmax Visualizza il messaggio
    codice:
    HTTP Status 500 - Request processing failed; nested exception is org.springframework.web.bind.annotation.support.HandlerMethodInvocationException: Failed to invoke handler method [public org.springframework.web.servlet.ModelAndView com.gontuseries.hellocontroller.HelloController.hiWorld(java.util.Map)]; nested exception is java.lang.IllegalStateException: Could not find @PathVariable [pathVars] in @RequestMapping
    Direi che al 99% è un problema di configurazione di Spring. Come è configurato? XML? Java Config? Come?
    Andrea, andbin.devSenior Java developerSCJP 5 (91%) • SCWCD 5 (94%)
    java.util.function Interfaces Cheat SheetJava Versions Cheat Sheet

  7. #7
    Utente di HTML.it L'avatar di Ironmax
    Registrato dal
    Dec 2008
    Messaggi
    1,026
    Nel mio progetto non ho file di configurazione java, finora non li ho mai utilizzati. Se vuoi puoi darmi una dritta su dove reperirli.
    Mentre le altre configurazioni sono:

    file web.xml
    codice:
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
      <display-name>SpringAnnotationPathVariable</display-name>
      <servlet>
        <servlet-name>spring-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      </servlet>
      <servlet-mapping>
        <servlet-name>spring-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
      </servlet-mapping>
    </web-app>

    file spring-dispatcher-servlet.xml:
    codice:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
     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/context
      http://www.springframework.org/schema/context/spring-context.xsd">
    
     <context:component-scan base-package="com.gontuseries.hellocontroller" />
    
     <bean id="viewResolver"
      class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name="prefix">
       <value>/WEB-INF/</value>
      </property>
      <property name="suffix">
       <value>.jsp</value>
      </property>
     </bean>
    
    
    </beans>
    Ultima modifica di Ironmax; 18-08-2016 a 12:18

  8. #8
    Utente di HTML.it L'avatar di andbin
    Registrato dal
    Jan 2006
    residenza
    Italy
    Messaggi
    18,284
    Aggiungi nel spring-dispatcher-servlet.xml anche

    <mvc:annotation-driven />

    devi anche dichiarare il namespace che è

    xmlns:mvc="http://www.springframework.org/schema/mvc"

    e nel xsi:schemaLocation aggiungi

    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc.xsd



    P.S. spring-dispatcher-servlet.xml deve chiaramente essere sotto WEB-INF. Il default di Spring è cercare un [servlet-name]-servlet.xml nella WEB-INF.
    Andrea, andbin.devSenior Java developerSCJP 5 (91%) • SCWCD 5 (94%)
    java.util.function Interfaces Cheat SheetJava Versions Cheat Sheet

  9. #9
    Utente di HTML.it L'avatar di Ironmax
    Registrato dal
    Dec 2008
    Messaggi
    1,026
    Funziona correttamente ora.
    Però mi da errore in corrispondenza del tag:
    <mvc:annotation-driven />

    Errore:
    codice:
    Multiple annotations found at this line:
    	- cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'mvc:annotation-driven'.
    	- schema_reference.4: Failed to read schema document 'http://www.springframework.org/schema/mvc/spring-mvc.xsd', because 1) could not 
    	 find the document; 2) the document could not be read; 3) the root element of the document is not <xsd:schema>.
    L'errore è un pallino rosso con una x, ma come ti dicevo funziona bene.

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.