Reputation: 1857
I have some radio buttons on a Struts 1 form:
<input type="radio" name="productId" value="31415" />
<input type="radio" name="productId" value="31416" />
Then in the Action
class I can get the values from the form just fine. For example if I select the first radio button then myForm.getProductId()
returns 31415
.
The question: is there a way in Struts 1 to tell which input field was selected by index, while keeping the current functionality?
(In worst case I know that I can create a new hidden field which contains the selected radio button and update that field from JavaScript, but I would like to avoid that way as well.)
Explanation: The radio buttons are created based on a collection, but for some reason the collection's elements sometimes don't have a product ID, so if there are multiple elements like that, then I can't tell which radio button was selected. But I'd like to know even in those cases, to improve the fault tolerance of the system. (I have no power over the elements of the collection, they come from a web service.)
Upvotes: 0
Views: 1203
Reputation: 1
You can use indexed properties for the indexed name of the input fields.
For indexed properties,
BeanUtils.populate()
uses the name of the request parameter to determine the proper setter method to call on theActionForm
Use indexed names
<input type="radio" name="productId[0]" value="31415" />
<input type="radio" name="productId[1]" value="31416" />
Define indexed properties
private String[] productId;
public String getProductId(int index) {
return productId[index];
}
public void setProductId(int index, String productId) {
productId[index] = productId;
}
public String[] getProductId( ) {
return productId;
}
Upvotes: 1