Binding request parameters with underscores in Spring MVC 3.0

跟風遠走 提交于 2020-06-27 08:46:41

问题


Consider the following requirement: request parameters have to be bound to objects using Spring MVC 3.0. The request parameters contain underscores (e.g. http://myurl:80/x?param_one=1&param_two=2). These parameters should be bound to the following object:

class MyObject {
    private Integer paramOne;
    private Integer paramTwo;

    ...
}

How would you go about doing this?

Important note: consider that there may be a substantial amount of parameters and objects like this and that it's not considered good practice to define setter methods on the objects that include underscores.


回答1:


Rajith's answer is for controller methods specifically, and doesn't address your question originally asking how to bind underscore parameters to an object.

The hacky solution I currently have in place is to accomplish what you are actually asking is to name my setters in this style:

public void setProject_ids(List<Long> project_ids) {

Note that RequestParam cannot be applied to methods, and it does not have an effect when applied to the argument of this setter.




回答2:


You can do this using @RequestMapping attribute

@RequestParam(value = "param_One") String paramOne,@RequestParam(value = "param_two") String paramTwo

Add this into method signature

But for best practice its better to pass same variable name



来源:https://stackoverflow.com/questions/13253371/binding-request-parameters-with-underscores-in-spring-mvc-3-0

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