How to get radio button list values ββin jsp
I am starting JSP. I am passing a list of images from a JAVA controller to a JSP. In JSP, I created 2 radio buttons for each image in the list, iterating over the list. Now how do I get the switch values ββfor each image in the JAVA controller.
My JAVA controller:
String[] imagesList = {"images/team.JPG", "images/team.JPG", "images/team.JPG", "images/team.JPG"};
model.addAttribute("imagesList",imagesList);
My Jsp:
<c:forEach var="image" items="${imagesList}" varStatus="i">
<input type="radio" name="${image}" value="${image}"/>Yes
<input type="radio" name="${image}" value="${image}"/>No
</c:forEach>
source to share
This may not work with the HTML you created. Suppose you have a radio button like this in your HTML page:
<input type="radio" name="foo" value="bar"/>
If the radio is not unchecked and the form is submitted, then no parameter will be submitted at all.
If the radio is validated and the form is submitted, then a parameter will be sent foo=bar
.
So, in your case, you have
<input type="radio" name="${image}" value="${image}"/>Yes
<input type="radio" name="${image}" value="${image}"/>No
This means that (assuming the image is "foo")
- If none of the radios are checked, no parameter will be sent for the image
- If "Yes" is checked, the parameter will be sent
foo=foo
- If marked "No", the parameter will be sent
foo=foo
So, you have no way to tell the difference between yes and no, since both checkboxes have the same meaning.
First, I would not use the image path as the name of this checkbox. Use an identifier that uniquely identifies the image and does not contain characters to be escaped like /
.
Then use a different value for Yes and No:
<input type="radio" name="${image.id}" value="true"/>Yes
<input type="radio" name="${image.id}" value="false"/>No
Then you can iterate over all the parameters of the query and get all those whose value is true: their names are the IDs of the images for which Yes was checked.
Note that it would be much more logical to use a unique checkbox for each image, rather than 2 radio buttons:
<input type="checkbox" name="checkedImages" value="${image.id}"/>
So all you have to do is get the parameter values ββfor a parameter named "checkedImages" and you will get an array directly containing all the IDs of the checked images.
source to share