下面分别是struts2标签和其对应的html代码,对于s:select的用法,看过即明。
Ⅰ 使用{}生成的list
jsp:
<s:select name="choice" list="{'one','two','three'}" headerKey="zero"
headerValue="--number--"/>
html:
<select name="choice" id="choice">
<option value="zero">--number--</option>
<option value="one">one</option>
<option value="two">two</option>
<option value="three">three</option>
</select>
发现:
①指定name后,在html中会生成和name相同值的id属性
②headerKey表示select中第一个option的value ,也是select标签的默认值; headerValue表示
第一个option的innerHTML
③其它option的value依次取list中的值
Ⅱ 使用#{}生成的map
jsp:
<s:select name="choice" list="#{1:'one',2:'two',3:'three'}" listKey="key"
listValue="value" headerKey="0" headerValue="--number--"/>
html:
<select name="choice" id="choice">
<option value="0">--number--</option>
<option value="1">one</option>
<option value="2">two</option>
<option value="3">three</option>
</select>
发现:
①listKey是指map中的key,用来设定option的value;listValue是指map中的value,用来
设置option的innerHTML
②其它基本同Ⅰ
Ⅲ从其它地方(request,session,action)中获取list
jsp:
<%
List<String> userList = new ArrayList<String>();
userList.add("Tom");
userList.add("Jerry");
userList.add("Mike");
userList.add("Jane");
pageContext.setAttribute("userList", userList);
%>
<s:select list="#attr.userList" value="#attr.userList[2]"/>
html:
<select name="choice" id="choice">
<option value="Tom">Tom</option>
<option value="Jerry">Jerry</option>
<option value="Mike" selected="selected">Mike</option>
<option value="Jane">Jane</option>
</select>
发现:
①使用value属性可以设定默认选取的option
②其它基本同Ⅰ
结语:看到这里,想必对<s:select>标签了解的差不多了吧 (当然,这里写的是最基本的)。
思考:如果<s:select>标签中既设置了headerKey属性,又设置了value属性,那么哪一个是
select的默认value?