Search code examples
htmljspstruts2radio-buttonstruts-tags

Get value of <s:set> variable in radio element struts2


Below loop runs three times, and display 3 radio button, my requirement is to check 1st radio by default.

For this I put one <s:if> condition and checked if iterator's index is 0, setting "true" in one "check" variable.

Now I am accessing this check variable in either like checked="%{check} or ${#check} or #check but desire result is not coming.

Only last radio is coming as checked.

Moreover, if I print "check" variable inside loop but outside radio like this <s:property value="%{check}"/>, it is displaying correct value true,false,false. But when I use this variable inside <s:radio>,it doesn't work properly.

Please help how to access this "check" variable in <s:radio> ?

<s:iterator value="operativeMode" var="mode" status="itStatus">
    <s:set var="check" value="false"/>
    <s:if test="#itStatus.index==0">
        <s:set var="check" value="true"/>
    </s:if> 

    <s:radio class="opmode" theme="simple" name="providerScheduleTO.contactMode"
     listValue="%{#mode.getDescription()}" list="%{#mode.getCode()}" %{check}
    />
    <br>       
</s:iterator>

Solution

  • You are coding like if you'd using standard HTML <input type="radio" /> tags.

    When using Struts2 <s:radio/> tag, a list of HTML <input type="radio" /> tags is generated through the list attribute, so there is no need to iterate the list and create a radiobutton for each element.

    That said,

    1. class is cssClass,
    2. getters are accessed in OGNL without the get and the () parts,
    3. you are missing the listKey attribute, and
    4. the checked element must be specified through the value attribute.

    Your whole code should be replaced by the following:

    <s:radio     list = "%{operativeMode}" 
              listKey = "%{code}"
            listValue = "%{description}" 
                value = "%{operativeMode[0].code}" 
                 name = "providerScheduleTO.contactMode" 
             cssClass = "opmode" 
                theme = "simple"              
    />
    

    Also take a look at