新手:在Jersey框架中,在我的例子中如何获取HTML表单对象数据?
当客户端提交HTML表单时,它会发送一个表单对象到服务器,例如:
params={username: USER_INPUT_USERNAME,
passworkd: USER_INPUT_PASSWORD}
然后,客户端ajax将params对象发送到我的Jersey > 服务器。
在服务器端,我如何获取并解析这个 HTML 表单对象数据 params
:(
@GET
@Produces({MediaType.APPLICATION_JSON})
public FormResult getFormResult(@QueryParam("params") Object params) {
//How can I define the type of the "params" I received?
//Do I need to create a Java Bean which represent the HTML form,
//and use the bean as the type of paprams? or any other way?
}
在上面的代码中,返回类型 FormResult
是一个 POJO bean,它将结果描述为响应客户端)
当我收到params
HTML表单对象数据时,如何定义params
的类型?(上面我定义了它类型为“Object
”,这是错误的)。
我是否必须定义一个 POJO bean 来表示 HTML 表单并使用该 bean 来描述 params 的类型?或者在泽西岛还有其他方式吗?
(如果为HTML表单创建POJO bean,如果HTML表单上有复选框,则params
将是一个动态对象,仅checked 字段将被添加到 params 对象中,这也是一个问题)
有人可以帮忙吗?
When client side submit a HTML form, it will send a form object to server, like:
params={username: USER_INPUT_USERNAME,
passworkd: USER_INPUT_PASSWORD}
Then, client ajax send the params object to my Jersey server.
On the server side, how can I get and parse this HTML form object data params
:
@GET
@Produces({MediaType.APPLICATION_JSON})
public FormResult getFormResult(@QueryParam("params") Object params) {
//How can I define the type of the "params" I received?
//Do I need to create a Java Bean which represent the HTML form,
//and use the bean as the type of paprams? or any other way?
}
(In above code, the return type FormResult
is an POJO bean which describe the result to response to client)
When I receive the params
HTML form object data, how can I define the type of the params
?(above I defined it with type "Object
", which is wrong).
Do I must define a POJO bean to represent the HTML form and use that bean to describe the type of the params
?? or any other way in Jersey?
(If create POJO bean for the HTML form, If there are check boxes on the HTML form, the params
will be an dynamic object, only checked field will be added to the params object, which is also a problem)
Anybody can help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
@QueryParam 映射单个查询字符串参数,因此对于 GET 请求,您需要将所有参数枚举为方法参数:
如果您提到的
params
实际上是一个查询字符串参数 (http://your.rest.service?params=somethingHere
),您可以将其映射到某个类,该类的构造函数采用String
或静态 < code>valueOf(String) 方法,您可以在其中进行实际解析。FormData 类可以如下所示:
编辑:
对于复选框,它并不像看起来那样动态:它们具有相同的名称,因此您将有一个、多个或无与该名称关联的值,具体取决于用户检查的内容。因此,@QueryParam("chk") String[] checkValues 应该足以处理这里的“动态”方面(实际上,“多值”会是一个更好的词)。
The
@QueryParam
maps an individual queryString parameter, so for a GET request you need to enumerate all your parameters as method arguments:If the
params
that you mentioned is actually one query string parameter (http://your.rest.service?params=somethingHere
), you can map that to some class that has a constructor taking aString
, or a staticvalueOf(String)
method where you do the actual parsing.And the FormData class can look like this:
EDIT:
For checkboxes, it's not as dynamic as it looks: they have the same name, so you'll have one-, several- or no- value(s) associated with that name, depending on what the user checked. Therefore, a
@QueryParam("chk") String[] checkedValues
should be enough to handle the "dynamic" aspects here (actually, 'multi-valued' would be a better word).