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?
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.
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
To illustrate this in an example :
<s:select name="employee.course.courseId" value="3" label="%{getText('label.courseName')}" list="courses" listKey="courseId" listValue="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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With