我们可以设想一个添加员工的场景,有一个下拉列表,选择员工所在的部门。
我们可以在action中读出所有的部门,然后绑定在s:select中
Action
public class EmployeeAction
{
private Employee emp;
private List<Dep> lstDep;
private DepService depservice;
public String toadd()
{
emp = new emp();
return "toadd";
}
...........
public List<Dep> getLstDep()
{
return depservice.getAllDep();
}
................
}
JSP
<td align="right">
部门:
</td>
<td align="left">
<s:select name="ylcs.dep" list="lstDep" listKey="depId"
listValue="depName" headerKey="headkey" headerValue="请选择状态" />
</td>
这样更新的时候也可以自动绑定到每个字段上,哈哈。
以下是我从网上看的例子,挺好:
1.第一个例子:
<s:select list="{'aa','bb','cc'}" theme="simple" headerKey="00" headerValue="00"></s:select>
2.第二个例子:
<s:select list="#{1:'aa',2:'bb',3:'cc'}" label="abc" listKey="key" listValue="value" headerKey="0" headerValue="aabb">
3.第三个例子:
<%
java.util.HashMap map = new java.util.LinkedHashMap();
map.put(1,"aaa");
map.put(2,"bbb");
map.put(3,"ccc");
request.setAttribute("map",map);
request.setAttribute("aa","2");
%>
<s:select list="#request.map" label="abc" listKey="key" listValue="value"
value="#request.aa" headerKey="0" headerValue="aabb"></
s:select
>
headerKey headerValue 为设置缺省值
4.第四个例子
public class Program implements Serializable {
/** serialVersionUID */
private static final long serialVersionUID = 1L;
private int programid;
private String programName;
public int getProgramid() {
return programid;
}
public void setProgramid(int programid) {
this.programid = programid;
}
public String getProgramName() {
return programName;
}
public void setProgramName(String programName) {
this.programName = programName;
}
}
在 xxx extends extends ActionSupport {
private List<Program> programs ;
public List<Program> getPrograms() {
return programs;
}
public void setPrograms(List<Program> programs) {
this.programs = programs;
}
}
在jsp页面
<s:select list="programs" listValue="programName " listKey="programid " name="program"
id="program" headerKey="0l" headerValue="请选择项目"></s:select>
参考博客:http://www.cnblogs.com/modou/articles/1326683.html
参考博客:http://blog.csdn.net/moliqin/article/details/3753570