`
lijun87
  • 浏览: 263748 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论

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>

 

 

[转自: http://www.cnblogs.com/stephencat/archive/2006/04/22/381898.html  ]

分享到:
评论

相关推荐

    孙维琴struts1中掌握html标签小程序

    孙维琴struts1中掌握html标签小程序。有几个比较典型的标签:&lt;html:select&gt; &lt;html:options&gt; &lt;html:link&gt; &lt;html:rewrite&gt; &lt;jsp:useBean&gt; &lt;jsp:setProperty&gt;等的用法

    Java Oracle分页处理

    package com.list.struts.util; import java.util.... document.forms[0].pagetype.options[${page.currentPage}-1].selected = true; 页 &lt;/form&gt; &lt;/html&gt;

    Spring-Reference_zh_CN(Spring中文参考手册)

    6.8.1. 在Spring中使用AspectJ来为domain object进行依赖注入 6.8.1.1. @Configurable object的单元测试 6.8.1.2. 多application context情况下的处理 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来...

    Spring中文帮助文档

    6.8.1. 在Spring中使用AspectJ进行domain object的依赖注入 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ加载时织入(LTW) 6.9. 更多资源 7...

    前端-后端java的Util类的工具类

    │ struts.xml │ ├─28个java常用的工具类 │ │ Base64.java │ │ Base64DecodingException.java │ │ CConst.java │ │ CharTools.java │ │ ConfigHelper.java │ │ Counter.java │ │ CTool.java │ │...

    freemarker总结

    options:该参数可以省略,指定包含时的选项,包含encoding和parse两个选项,其中encoding指定包含页面时所用的解码集,而parse指定被包含文件是否作为FTL文件来解析,如果省略了parse选项值,则该选项默认是true. ...

    snmp-formula

    例如,您可以在公共Struts文件中设置通用SNMP配置,其中将包括: snmp : conf : settings : logconnects : false sysServices : 72 而要在同一群集上使用SNMP监视GPFS的团队会将此Struts文件添加到他们的软件包中:...

    Spring API

    6.8.1. 在Spring中使用AspectJ进行domain object的依赖注入 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ加载时织入(LTW) 6.9. 更多资源 7...

    Spring 2.0 开发参考手册

    6.8.1. 在Spring中使用AspectJ来为domain object进行依赖注入 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ Load-time weaving(LTW) 6.9. ...

    spring chm文档

    6.8.1. 在Spring中使用AspectJ来为domain object进行依赖注入 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ Load-time weaving(LTW) 6.9. ...

    Advanced Flex 3 2008

    the help of two fully functional case studies that include Apache Struts and Ruby on Rails applications, respectively. Chapter 12: Sculpting Interactive Business Intelligence Interfaces Business ...

Global site tag (gtag.js) - Google Analytics