在Spring MVC 3.0中使用下划线绑定请求参数 [英] Binding request parameters with underscores in Spring MVC 3.0

查看:341
本文介绍了在Spring MVC 3.0中使用下划线绑定请求参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下要求:必须使用Spring MVC 3.0将请求参数绑定到对象。请求参数包含下划线(例如 http:// myurl:80 / x?param_one = 1& param_two = 2 )。这些参数应绑定到以下对象:

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;

    ...
}

您将如何处理

重要说明:请注意,可能存在大量这样的参数和对象,并且不认为这样做是一种好习惯

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.

推荐答案

Rajith的答案专门针对控制器方法,不能解决您的问题

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

我目前使用的hacky解决方案是完成您实际要执行的操作,即以这种样式命名我的二传手:

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) {

请注意,RequestParam不能应用于方法,并且应用于此setter的参数时也无效。

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

这篇关于在Spring MVC 3.0中使用下划线绑定请求参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆