服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

struts 中 html:options 的使用


  html:options是struts中比较复杂的一个tage lib,用法灵活,但是sturts提供的源码exercise taglib中没有提出常用jsp+actionform这样形式的最直接的总结,现从中总结如下,分两种情况:数组和collection。
  
  需求,要达到:
  <select name="beancollectionselect" multiple="multiple" size="10">
  <option value="value 0">label 0</option>
  <option value="value 1" selected="selected">label 1</option>
  <option value="value 2">label 2</option>
  <option value="value 3" selected="selected">label 3</option>
  <option value="value 4">label 4</option>
  <option value="value 5" selected="selected">label 5</option>
  <option value="value 6">label 6</option>
  <option value="value 7">label 7</option>
  <option value="value 8">label 8</option>
  <option value="value 9">label 9</option></select>
  
  要实现上述效果,需要两步:
  第一:设置actionform,
  也分两小步:第一小步必须在actionform中,有一句
  private collection beancollection;
  public collection getbeancollection();
  
  collection beancollection要确保是一个实现,如arraylist,如果不是则会报no collection found的错误,struts的最大不方便就是一旦出问题,定位很难,不知道什么地方使用错误,或忘记设置什么了。
  
  因为前面需求中option的value值和label值不一样,那么在beancollection中保存的就是一个value和label组成的对象,名为labelvaluebean,在labelvaluebean中有两个属性value和label,
  
  在程序某个地方要为beancollection赋值,如:
  
  vector entries = new vector(10); 
  entries.add(new labelvaluebean("label 0", "value 0"));     
  entries.add(new labelvaluebean("label 1", "value 1"));     
  entries.add(new labelvaluebean("label 2", "value 2"));     
  entries.add(new labelvaluebean("label 3", "value 3"));     
  entries.add(new labelvaluebean("label 4", "value 4"));      
  entries.add(new labelvaluebean("label 5", "value 5"));     
   entries.add(new labelvaluebean("label 6", "value 6"));      
  entries.add(new labelvaluebean("label 7", "value 7"));      
  entries.add(new labelvaluebean("label 8", "value 8"));      
  entries.add(new labelvaluebean("label 9", "value 9"));
  
  然后执行setbeancollection(entries);
  这样actionform中的beancollection算有值了。
  第二小步,需要设置selected,selected有两种,单选和多选:
  在actionform中必须有:
  
  private string singleselect = "single 5"; 
  public string getsingleselect()
   {
     return (this.singleselect);
    } 
  public void setsingleselect(string singleselect)
   {
     this.singleselect = singleselect;
    }
  
  或多选,多选必须是数组:
  
  private string[] beancollectionselect = { "value 1", "value 3",
                         "value 5" }; 
  public string[] getbeancollectionselect() {
    return (this.beancollectionselect);  }
    public void setbeancollectionselect(string beancollectionselect[])
   {
      this.beancollectionselect = beancollectionselect;
    }
  
  第二:在jsp中写入tang lib语句如下:
  
  <html:select property="beancollectionselect" size="10" multiple="true">
      <html:optionscollection name="testbean" property="beancollection"/>  
   </html:select>
  
  其中testbean是actionform的名称。
  
  以上是html:options的collection解决方案,如果option值很少,简单地可以实现为数组,两步:
  第一:在actionform中,
  
  private string values[] =
     { "magazine", "journal", "news paper","other" }; 
  private string labels[] =
     { "l-magazine", "l-journal", "l-news paper","l-other"};
    private string selected = "magazine";  
  public string getselected()
  {
     return selected;
    }  
  public void setselected(string selected)
  {
     this.selected = selected;
    } 
  public string[] getvalues()
  {
     return values;
    }  
  public void setvalues(string[] values)
  {   this.values = values;
    } 
  public string[] getlabels()
  {
     return values;
    }  
  public void setlabels(string[] labels)
  {
     this.labels = labels;
    }
  
  第二步在jsp中:
  
  <html:select property="selected" >     
  <html:options name="testbean" property="values" labelproperty="label"/>   </html:select>
  
  struts标签库的使用还是需要小心,不必完全使用struts的标签库,个人感觉struts这种替代html语句的标签库有一种牵强附会,给使用者掌握带来难度,使用者除熟悉html外,还必须理解struts的对应标签库用法,而且这种调试出错,问题也无法准确定位,总是抽象地告诉你,no bean 或no form

扫描关注微信公众号