2012-01-31 110 views
22

我试图将网址映射/locations/{locationId}/edit.html - 这似乎与此代码的工作:PathVariable春季控制器

@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"; 
    } 
} 

呼叫中的异常所提到的URL结果:

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

我是否以错误的方式使用了@PathVariable注解?

如何正确使用它?

回答

32

应该是@PathVariable("locationId") int locationId

+8

这在这里详细描述,而当你的代码没有调试信息(HTTP发生编译:// docs.spring.io/spring/docs/3.2.x/spring-framework-reference/html/mvc.html):**如果URI模板变量名称与方法参数名称匹配,则可以省略该详细信息。只要你的代码没有在没有调试信息的情况下编译,Spring MVC会将方法参数名称与URI模板变量名称** – 2013-11-20 19:16:56

+0

注意,使用“Debug As”进行编译不一定包含项目中的调试信息。检查你的设置,[详细在这里](http://stackoverflow.com/a/1318483/1412656),并基本检查所有调试测深复选框! – 2013-11-20 19:27:29

16

您应该value参数添加到您的@PathVariable,例如,

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