struts2 s:select 标签

时间:2024-11-11 22:33:02
1 静态生成

<s:select name="" list="#{'0':'男','1':'女'}" label="性别" headerKey="" headerValue="请选择性别"></s:select>


注解:这里从页面来静态(也就是在页面中写死啦)构造下拉选项,关键点是list="#{'0':'男','1':'女'}" 通过它实现的select的两个option 。headerKey指定缺省的选项"key",headerValue指定缺省的选项"value"。


2.设置下拉框数据源

	<%
			 map = new ();
			(1, "aaa");
			(2, "bbb");
			(3, "ccc");
			("map", map);
			("aa", "2");
	%>

<s:select list="#"  label="abc" listKey="key" listValue="value" value="#"  headerKey="0" headerValue="aabb">
</s:select>




value的值设置为了2,所以初始化下拉框时,会自动选择key值为2的选项




3.action传递list过来

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) {
         = programid;
    }
        public String getProgramName() {
        return programName;
    }
    public void setProgramName(String programName) {
         = programName;
    }
}


action

xxxAction extends  extends ActionSupport {
    private  List<Program> programs ;//list集合
       public List<Program> getPrograms() {
        return programs;
    }
    public void setPrograms(List<Program> programs) {
             = programs;
    }
}

jsp页面

  <s:select list="programs "  listValue="programName " listKey="programid "  name="program" 
             headerKey="0l" headerValue="" >
  </s:select>  

listKey说明下拉框的key值对应 programid字段,listValue说明下拉框的显示部分对应programName字段