JERSEY RESTful - How to work with multiselect checkboxes?

穿精又带淫゛_ 提交于 2019-12-22 00:40:53

问题


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.

I've test:

@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).

Some idea?


回答1:


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 form:

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

Submitting the form with B and C checked prints out:

[b, c]

I also verified in the debugger that the list is populated with 2 strings, b and c.



来源:https://stackoverflow.com/questions/11158276/jersey-restful-how-to-work-with-multiselect-checkboxes

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