How to set selected default value in Struts2 <s: select> tag?

I am new to Struts2. I have a user form, the roles column has a drop-down list. When the user form is in edit mode, the saved values ​​are placed in the corresponding controls. But I can not set the dropdown list by default. How can i do this?

+5
source share
5 answers

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

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

+6
source

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

+6
source

:

<s:select name="employee.course.courseId" value="3"  label="%{getText('label.courseName')}" list="courses" listKey="courseId" listValue="courseName" />
  • Employee , "", "courseId"
  • listKey courseId, listValue courseName
  • , :

        <option value="1">Computer Science</option>
        <option value="2">Electronics</option>
        <option value="3">Mechanical</option>
    
  • value "3" 3- ,

  • , , html :

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

, .

+3
<!--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>
0

, , , .

<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' />

,

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

.

Struts2:

. , (, , ), OGNL (. "" ), , ( , "01", "02" ..). '1' char, '01' , "1" . , , "", , "list", , String . , .

0

All Articles