The default value in the struts2 list

We have a box.This list will show different states in USA. I want the state "LA" to be as preselected. But I do not know the position of "LA" in the list. It can change. To do this, we use the following script.

<select id="state" name="state" title="State" class="js_required prompt_text grid_2" tabindex="5">
                <option>State</option>
                <s:iterator value="@com.homeservices.action.utils.StateCode@values()">
                    <option value="<s:property/>"><s:property/></option>
                </s:iterator>
</select>

var @ com.homeservices.action.utils.StateCode @values ​​() provides a list of values:

AA
AE
AK
CA
CT
IL
LA
MA
MD

...... etc.

Can someone suggest how to make LA as a preselected state.

+3
source share
2 answers

Struts Approach

<s:select id="state"
          name="state"
          title="State"
          headerKey=""
          headerValue="State"
          list="@action.StateCode@values()"
          cssClass="js_required prompt_text grid_2"
          value="@action.StateCode@LA"
          tabindex="5"/>

JSP Approach (JSTL / JSP-EL)

<%@ page import="action.StateCode" %>
<c:set var="states" value="<%=StateCode.values()%>"/>

<select id="state" name="state" title="State"
        class="js_required prompt_text grid_2" tabindex="5">
    <option>State</option>
    <c:forEach items="${states}" var="state">
        <option value="${state}" ${state == 'LA' ? 'selected="selected"' : ''}>${state}</option>
    </c:forEach>
</select>
+5
source

put this script just before </html>and after fillingstate

<script type="text/javascript">
        document.getElementById("state").value = "LA";
</script>
0
source

All Articles