5

I am new in Struts2. I have user form, role column have a drop-down list. When user form is in edit mode, the stored values are placed into corresponding controls. But I can't set drop-down list by default selected value. How can I do it?

6 Answers 6

9

In addition to Nate's answer, I've found that I need to put apostrophes around the data in the value attribute if the key type is a String in order for it to recognize that my input value is a String.

1
  • Saved my day. Much thanks to you ! So value="'helloworld" works and not value="helloworld really weird syntax
    – Sangimed
    Aug 22, 2019 at 15:15
6

If the value in your select tag matches a key from the list in the select tag, Struts will do the correct thing and make that value the default. Note that the types must match.

https://struts.apache.org/tag-developers/select-tag.html

1
3

To illustrate this in an example :

<s:select name="employee.course.courseId" value="3"  label="%{getText('label.courseName')}" list="courses" listKey="courseId" listValue="courseName" />
  • Here the Employee object contains an object called "course" and it has a property "courseId"
  • listKey is selected as courseId and the listValue is selected as courseName
  • Hence the output will be like :

        <option value="1">Computer Science</option>
        <option value="2">Electronics</option>
        <option value="3">Mechanical</option>
    
  • The value attribute is set to "3" and it matches the 3rd attribute in the list, which is Mechanical

  • Therefore this will be the default selected value in the dropdown, hence the output html will be like :

        <option value="1">Computer Science</option>
        <option value="2">Electronics</option>
        <option value="3" selected="selected">Mechanical</option>
    

Hope this helps.

0
0

Even though you follow everything correctly and it doesn't preselect then you need to make sure that return type of the key matches. e.g. for following list

<select onchange="showHideDefDiv('typeListId')" style="selectCss50" class="selectCss50" id="typeListId" name="definitionDiv_I">
  <option value="blank"> </option>
  <option selected="selected" value="definitionDiv_I">I</option>
</select>

<s:select list="%{editRulePojo.groupPojoList}" listKey="%{groupType}"
          listValue="%{groupTypeValue}" value='definitionDiv_I' />

doesn't work, while

<s:select list="%{editRulePojo.groupPojoList}" listKey="%{groupType}"
          listValue="%{groupTypeValue}" value='%{editRulePojo.groupType}' />

works.

From Struts2 documentation:

Note: For any of the tags that use lists (select probably being the most ubiquitous), which uses the OGNL list notation (see the "months" example above), it should be noted that the map key created (in the months example, the '01', '02', etc.) is typed. '1' is a char, '01' is a String, "1" is a String. This is important since if the value returned by your "value" attribute is NOT the same type as the key in the "list" attribute, they WILL NOT MATCH, even though their String values may be equivalent. If they don't match, nothing in your list will be auto-selected.

0
0

Right way to do show the selected value in drop down is to create get method in your action class :

Action Class:

public String getDefaultValue() {
    return "defaultVal";
} 

JSP:

<s:select theme="simple" list="yourList" name="name" id="id" listKey="id" listValue="description" value="defaultValue"/>
-1
<!--name attribute inside select tag must be a variable in action class with getter/setter -->
<!-- test variable sets the value of selected item in action class -->
<select name="test">
    <!-- name attribute could be anything you want but value attribute must be a model class variable-->
    <s:set name="lead_string_LS_ID" value="MasterDataModel.string_LS_ID" />
        <!-- value attribute must be a list to iterate, status (an instanceof IteratorStatus will be pushed into stack upon each iteration)or result  -->
        <!-- var Name used to reference the value pushed into the Value Stack (my list contain leadSource_String_Id)-->
        <s:iterator value="leadSource_list" status="result" var="leadSource_String_Id">
                <!--#lead_string_LS_ID is value taken from <set> tag above. Note # must be in-front of the name
                    leadSource_String_Id is element specified in var of <iterator> tag  
                -->
                <s:if test='(#lead_string_LS_ID.equals(leadSource_String_Id))'>

                    <option value="<s:property value='leadSource_String_Id'/>" selected="selected">
                        <s:property value="leadSource_String_Name" />
                    </option>
                </s:if>
                <s:else>
                    <option
                        value="<s:property value='leadSource_String_Id'/>">
                        <s:property value="leadSource_String_Name" />
                    </option>
                </s:else>
        </s:iterator>
</select>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.