问题
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¶m_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