struts中设置和获得一个列表框

来源:互联网 发布:房源发布软件 编辑:程序博客网 时间:2024/05/29 15:44

当select的value和label属性都是可变的,想在页面中设置并传到后台,或从后台读取某属性在页面上显示。怎么做呢?现在从下面代码就可以满足:

使用<html:options />. label 和value 的Collection 可以在action中初始化

代码(一):

javascript里面嵌入JAVA代码
<% Vector shopList=(Vector)shopBean.findShopList); %>

<SCRIPT LANGUAGE="JavaScript1.2">
var allShopArr = new Array();
     <%
   int ss = 0;
   if( null != shopList){
   for (Iterator s = shopList.iterator(); s.hasNext();) {
            ShopModel shopModel = (ShopModel)s.next();  
               
        %>           
                  allShopArr[<%= ss %>] = new Object();
                  allShopArr[<%= ss %>].Id = "<%= shopModel.getShopID() %>";
                  allShopArr[<%= ss %>].Name = "<%= shopModel.getShopShortName() %>";
        <%
             ss ++;
           }
           }
        %>



function goto(this) {
    // 这里写上你的ACTION
    //这是当前访问的OPTION: this.selectedIndex;
    //这是当前访问的OPTION的VALUE: allShopArr [this.selectedIndex].ID;
    //这是当前访问的OPTION的LABLE: allShopArr [this.selectedIndex].Name;

}
</script>

<html:select name="allShopId" multiple id="allShopId" size=6 style="width:168px" onChange="javascript:goto(this)">
                      <%  for (Iterator i = shopList.iterator(); i.hasNext();) {
                  ShopModel shopModel = (ShopModel)i.next();
                  String shopName=shopModel.getShopShortName();
                  long shopId=shopModel.getShopID();
             
                  %>
                      <html:option value="<%=String.valueOf(shopId)%>"><%=shopName%></html:option>
                      <%}%>
</html:select>

(代码二)

在jsp页面有个下拉列表框
<html:select property="status">
        <html:options collection="status" property="value" labelProperty="label"/>
</html:select>
在dao里有此方法,简单起见,没有从数据库中取。
public Collection getStatus()
{
     ArrayList rslist = new ArrayList();
     HashMap rscol1 = new HashMap();
     HashMap rscol2 = new HashMap();
     HashMap rscol3 = new HashMap();
     rscol1.put("value","1");
     rscol1.put("label","状态1");
     rslist.add(rscol1);
     rscol2.put("value","2");
     rscol2.put("label","状态2");
     rslist.add(rscol2);
     rscol3.put("value","3");
     rscol3.put("label","状态3");
     rslist.add(rscol3);
     return rslist;
}
在jsp表单相应的form里声明
private String status;
在action的execute方法中调用dao中的方法给下拉列表赋值
httpServletRequest.setAttribute("status",dao.getStatus());
这样就可以实现了下拉列表的选择操作。

原创粉丝点击