2012-01-31 14 views
22

Próbuję map url /locations/{locationId}/edit.html - który wydaje się działać z tym kodem:PathVariable w Spring Controller

@Controller 
@RequestMapping("/locations") 
public class LocationController 
{ 
    @RequestMapping(value = "/{locationId}/edit.html", method = RequestMethod.GET) 
    public String showEditForm(Map<String, Object> map, @PathVariable int locationId) 
    { 
    map.put("locationId", locationId); 
    return "locationform"; 
    } 
} 

połączeń wspomniane URL powoduje wyjątek:

java.lang.IllegalArgumentException: Name for argument type [int] not available, and parameter name information not found in class file either. 

Czy używam adnotacji @PathVariable w niewłaściwy sposób?

Jak używać go poprawnie?

Odpowiedz

32

powinno być @PathVariable("locationId") int locationId

+8

ten jest szczegółowo tutaj, a dzieje się, gdy kod jest kompilowany bez informacje debugowania (http: // docs.spring.io/spring/docs/3.2.x/spring-framework-reference/html/mvc.html): ** jeśli nazwa zmiennej szablonu URI jest zgodna z nazwą argumentu metody, możesz pominąć ten szczegół. Dopóki twój kod nie zostanie skompilowany bez informacji o debugowaniu, Spring MVC będzie dopasowywać nazwę argumentu metody do nazwy zmiennej szablonu URI ** –

+0

pamiętać, że samo kompilowanie przy użyciu "Debug As" niekoniecznie będzie zawierać informacje debugowania w projekcie. Sprawdź ustawienia, [jak opisano tutaj] (http://stackoverflow.com/a/1318483/1412656), i po prostu sprawdź wszystkie pola wyboru debugowania! –

16

Należy dodać value argument Twój @PathVariable np

public String showEditForm(
     @PathVariable("locationId") int locationId, 
     Map<String, Object> map) { 
    // ... 
} 
Powiązane problemy