optionsCollection标签用法:
与options标签一样,optionsCollection标签可以从集合或者是包含集合的对象里获得选项的标签/值对。在这两种情况里,集合或包含集合的对象必须是一个作用域对象,否则定制标签将无法访问它。

1.与包含集合的对象配合使用
举例:userForm动作表单有一个如下所示的ArrayList类型的userList属性,相应的set,get方法,通过在action中设置好userForm后,request.setAttribute("userForm",userForm);
在jsp页面:
<html:select property="school">
<html:optionsCollection name="userForm"
       property="userList"/>
</html:select>
2.与集合配合使用
action中
ArrayList userList=new ArrayList();
userList.add(new LabelValueBean("1","haha"));
userList.add(new LabelValueBean("2","dada"));
userList.add(new LabelValueBean("3","xiaoxiao"));
request.setAttribute("userList",userList);jsp页面
<html:select property="school">
<html:optionsCollection name="userList"
       label="label" value="value"/>
<!--这样就可以将集合中的数据都显示出来,相当于多个option标签-->
</html:select>
注:label与bean中你要显示的属性名字对应

    name指定bean的名称

    userList指定集合名称

例子解释:
<html:optionsCollection   name="stationList"   value="value"   label="label"   />  
这里name代表一个存储在某个作用域中的bean,即page,request,session,或者application存放的的变量名  
  如是在进这页面前有一个request.setAttribe("stationList",list);  
  list的可以是任何收集类!  
  list可以是对象  
  如user对象,有一个userId,userName,有相应的getter方法  
  则可以这样写  
  <html:optionsCollection   name="stationList"   value="userId"   label="userName"   />  
  这里,就把userId作为值,userName作为显示text在select里

例子解释:
<html:select   property="category">  
  <html:optionsCollection   name"cat"   property="options"/>  
  </html:select>  
  同时使用NAME和PROPERTY,属性指定对象的名称和字段,将调用该对象的值获取方法以返回一个用来生成选项的集合

你可能感兴趣的:(html,bean,jsp,struts)