Search code examples
javajspstruts2ognl

How to retrieve the data of <s:radio> within <s:iterator> tag into Struts2's action


I need to do something in webpage like this : a bunch of rows that display questions and choices , the choices are made up of radio buttions. I don't konw how to get these radio button's data into my java code. This is my jsp code:

<s:iterator value="questionsList" status="status" var="questionsList">
    <tr>
        <td><s:property value="title"/></td>
    </tr>

    <tr>    
         <td>
             <s:radio list="{'A','B','C','D'}" name="[%{#status.index}]answer" theme="simple"/>
         </td>
     </tr>
</s:iterator>

I've tried

private List answer = new ArrayList();
answer = (List)request.getAttribute("[%{#status.index}]answer");

but it doesn't work. If you have other means to do this please tell me, thank you!


Solution

  • You can provide indexed property names for the lists you want to get in Java

    <s:iterator value="questionsList" status="status">
        <tr>
            <td><s:property value="title"/></td>
        </tr>
    
        <tr>    
             <td>
                 <s:radio list="{'A','B','C','D'}" name="questionsList[%{#status.index}].answer" theme="simple"/>
             </td>
         </tr>
    </s:iterator>
    

    Voila! Your question list will be populated with indexes provided by status.index variable.