How to use @FormParam when form elements are dynamically created
Since html form elements are dynamically generated in this application, the number of elements is unknown. How can I get information about an element using @FormParam annotation? For example, the code below gets information for two form elements:
@POST
@Path("/newpage")
@Produces("text/html")
public String func(@FormParam("element1") String firstElement,
@FormParam("element2") String secondElement) throws IOException
{
// your code goes here
}
This is not possible because we do not know the number of elements.
+3
source to share
1 answer
I can't think of a way to do this with @FormParam
, but you can use @Context
to access HttpServletRequest
(which links to a map of all form parameters):
// you can make this a member of the Resource class and access within the Resource methods
@Context
private HttpServletRequest request;
@POST
@Path("/newpage")
@Produces("text/html")
public String func() throws IOException
{
// retrieve the map of all form parameters (regardless of how many there are)
final Map<String, String[]> params = request.getParameterMap();
// now you can iterate over the key set and process each field as necessary
for(String fieldName : params.keySet())
{
String[] fieldValues = params.get(fieldName);
// your code goes here
}
}
+4
source to share