当前位置: 首页 > 图文教程 > Java技术 > Web框架 > 浅谈Struts中html:options的使用

Web框架
Web框架:Struts标记库定制JSP标记
Web框架:简述Ajax技术的那些框架
Web框架:利用列表数据提高开发效率
Web框架:Struts2中声明式异常处理
Web框架:小议Spring的异常处理那点事
Web框架:详谈AOP概念
Web框架:小编浅谈Struts2的Ajax支持
Web框架:Ajax提供的div标签
Web框架:程序员之家七月份Web框架总结
Web框架:浅谈Spring Bean封装机制
Web框架:小编浅谈Struts配置文件
Web框架:小编谈Spring中的AOP 应用
Web框架:Spring的闪亮之处:事务管理
Web框架:浅谈iBATIS的动态映射
Web框架:Hibernate中五个接口那些事
Web框架:Hibernate的ORM与Hibernate的优点
Web框架:Struts2的OGNL
Web框架:Struts中的ActionForm 作为防火墙
Web框架:小编浅谈Struts2中使用JSON插件实现Ajax
Web框架:Aegis绑定那些事

Web框架 中的 浅谈Struts中html:options的使用


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-12-26   浏览: 228 ::
收藏到网摘: n/a

html:optionsStruts中比较复杂的一个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的最大不方便就是一旦出问题,定位很难,不知道什么地方使用错误,或忘记设置什么了。

  

  因为前面需求中optionvalue值和label值不一样,那么在beanCollection中保存的就是一个valuelabel组成的对象,名为LabelvalueBean,在LabelvalueBean中有两个属性valuelabel

  

  在程序某个地方要为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算有值了。

  第二小步,需要设置Selectedselected有两种,单选和多选:

  在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>

  

  其中testbeanActionForm的名称。

  

  以上是html:optionsCollection解决方案,如果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