Spring MVC referencing params variable from RequestMapping

心不动则不痛 提交于 2019-11-29 00:09:16

问题


I have the method below:

@RequestMapping(value = "/path/to/{iconId}", params="size={iconSize}", method = RequestMethod.GET)
public void webletIconData(@PathVariable String iconId, @PathVariable String iconSize, HttpServletResponse response) throws IOException {
    // Implementation here
}

I know how to pass the variable "webletId" from the RequestMapping using the @PathVariable, but how do I reference the variable "iconSize" from params?

Thanks a lot.


回答1:


Use @RequestParam:

@RequestMapping(value = "/path/to/{iconId}", method = RequestMethod.GET) 
public void webletIconData(@PathVariable String iconId, 
    @RequestParam("size") String iconSize, 
    HttpServletResponse response) throws IOException { ... }

See also:

  • 15.3.2.3 Supported handler method arguments and return types



回答2:


axtavt is right

I only want to explain what your mistake is:

The @RequestMapping params parameter is a filter to make sure that the annotated handler method is only invoked if there is a parameter with the requested value.

So a handler method annotated with @RequestMapping(params="action=doSomething") will be only invoked if there is an request parameter actionwith the content doSomething.



来源:https://stackoverflow.com/questions/5000876/spring-mvc-referencing-params-variable-from-requestmapping

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!