Search code examples
if-statementstruts2

test iterator value in struts 2


I want to select option in drop down on condition.

 <s:set var="defaultState" value="customerBean.state"/>
                                        <select onchange="reflect(this);"  name="customerBean.state" class="form-control"  id="state">
                                            <option>Select</option>
                                            <s:iterator value="stateList" var="statevalue">

                                                <s:if test="%{#defaultState.equalsIgnoreCase(stateValue)}">  
                                                    <option id="stateVal" selected="selected"><s:property value="statevalue"/></option>

                                                </s:if>  
                                                <s:else>  
                                                    <option id="stateVal"><s:property value="statevalue"/></option>
                                                </s:else> 
                                            </s:iterator>
                                        </select>

this does not work. But if I put

<s:if test="%{#defaultState.equalsIgnoreCase('Alabama')}">

this works.

Please help.


Solution

  • Have you checked that it is not a simple typo? In your code you are using stateValue and statevalue.

    Beside that is stateList a simple list or is it a list of beans? For the second option you have to use something like stateValue.value.

    Another option: Because it is an OGNL-Expression you could try also #statevalue. But I'm never sure when to use with # and when without.

    Another option: use the struts tag s:select and to preselect the option use the value attribute.