JERSEY RESTful - How to work with multiselect chec

2019-04-16 11:42发布

问题:

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.