posts - 32,  comments - 149,  trackbacks - 0
1. html:select

该标签生成一个select元素。multiple属性决定是否为多选。如果指定了multiple="true"则为多选,此时对应的属性应该是一个数组。否则,此时对应的属性应该是标量。

注意:为了正确的处理未作选择的情况,在ActionForm中的reset()方法中必须将标量属性设置为默认值而将数组的长度置为0。 另外的一个重要问题就是struts如何生成option元素了,这个任务struts交给了html:option、html:options和html:optionsCollection三个标签。

    1)html:option

     该标签生成一个HTML的option元素。该标签必须嵌在html:select标签中。它的显示文本来自其标签体,也可以来自于资源文件。

eg. <html:option value="red">红色</html:option>    

     <html:option value="blue">蓝色</html:option>

   2)html:options

     该标签生成多个HTML的option元素。该标签必须嵌在html:select标签中。

     指定collection属性的方式举例如下:

  <html:select name="userForm" property="uid" size="1">  

<html:options collection="userCollection" property="uid" labelProperty="uname"/>

</html:select>

    未指定collection属性方式的举例如下:用数组的形式 String uids[] ,String unames[]

  <html:select name="userForm" property="uid" size="1">  

<html:options property="uids" labelProperty="unames"/>

</html:select>

    3)html:optionsCollection标签

     该标签生成多个HTML的option元素。其功能和html:options标签的相同。
      userForm 里有个List userlist 它通过这样得到的 
     userlist.add(new LabelValueBean("label1","value1"));

  <html:select name="userForm" property="uid" size="1">    
  <html:optionsCollection name="userForm" property="userlist"> 
  </html:select>

   4)实现举例

   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;  //Collection 可以用List或者Vector
  public Collection getBeanCollection();
  Collection beanCollection要确保是一个实现,如ArrayList,
   如果不是则会报No collection found的错误,Struts的最大不方便就是一旦出问题,

   定位很难,不知道什么地方使用错误,或忘记设置什么了,不过可以查看tomcat Log的。
  
  因为前面需求中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中:
  //下面这个我使用时,出错,总是提示找布道labels,不知什么原因?
  <html:select property="selected" >     
  <html:options name="testbean" property="values" labelProperty="labels"/> 
  </html:select>
总结: 经过这段时间的开发和学习,自我感觉struts还是不错的,虽然一开始的时候有点麻烦,

不过其实Java的配置也是有点麻烦的,不过用了这么长时间的Java,现在觉得Java挺好的!

posted on 2007-01-18 09:05 chunkyo 阅读(4510) 评论(3)  编辑  收藏 所属分类: openSource(struts&hibernate&spring等等)

FeedBack:
# re: Struts 中 html:options 的使用
2008-05-07 19:03 | redleaf
您好,这个有源码吗?
没怎么看懂  回复  更多评论
  
# re: Struts 中 html:options 的使用 [未登录]
2008-07-24 16:04 | haha
labelProperty属性去掉就可以了,  回复  更多评论
  
# re: Struts 中 html:options 的使用
2009-04-01 17:56 | 打假
去死吧,全他是抄来,且是错的。  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航:
 
<2007年1月>
31123456
78910111213
14151617181920
21222324252627
28293031123
45678910

这个博客主要是关于java技术和开源技术,大家一起来进步了!

常用链接

留言簿(12)

随笔分类

随笔档案

文章分类

收藏夹

DotNet

Java技术网站

Linux VS Unix

其他常去网站

常光顾的BLOG

文学类网站

游戏类网站

最新随笔

搜索

  •  

积分与排名

  • 积分 - 195809
  • 排名 - 290

最新评论

阅读排行榜

评论排行榜