JERSEY RESTful-如何使用多选复选框? [英] JERSEY RESTful - How to work with multiselect checkboxes?
问题描述
我正在尝试创建一种从多选复选框HTML组件接收参数列表的方法.但是,它只是行不通.
I´m trying to create a method that receive a list of parameters from a multiselection checkbox html component. But, it just doesn´t work.
我已经测试:
@POST..
.. myMethod(@FormParam String [] myCheckboxAttribute)
.. myMethod(@FormParam List<String> myCheckboxAttribute)
这些方法都无法正常工作(最后一个(列表)仅选中了第一个复选框,而其他复选框则没有).
None of those works well (the last one (list) come with just the first checkbox checked but the others no).
有什么主意吗?
推荐答案
您必须在@FormParam批注中指定form参数的名称.
You have to specify the name of the form parameter in the @FormParam annotation.
以下是对我有用的示例:
Here is an example that works for me:
@POST
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Produces(MediaType.TEXT_PLAIN)
public String postForm(@FormParam("param") List<String> param) {
return param.toString();
}
HTML表单:
<html><body>
<form action="http://localhost:9998/myresource" method="POST">
<input type="checkbox" name="param" value="a">A</input>
<input type="checkbox" name="param" value="b">B</input>
<input type="checkbox" name="param" value="c">C</input>
<input type="submit">OK</input>
</form>
</body></html>
提交带有B和C选中的表格的打印件:
Submitting the form with B and C checked prints out:
[b, c]
我还在调试器中验证了列表中是否包含2个字符串b和c.
I also verified in the debugger that the list is populated with 2 strings, b and c.
这篇关于JERSEY RESTful-如何使用多选复选框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!