struts问题关于<html:select标签 关注,用STRUTS如何做这样的处理,不知道有没有现成的标签!STRUTS有现在的逻辑的标签在这里应该可以用到. 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我记忆中,struts select标签是可以处理这个问题的,你试试用name和lableName属性,字符串集合可能可以做到。<html:options name="optionValues" labelName="optionLabels"/>不过由于这个标签每次要我构造collection很麻烦,所以我现在自己写了一个select标签直接从数据库抽取信息,也绕开了bean这一层。 你试一试request.setAttribute("trainHeadForm",要显示的当前值);注意是 .value 在trainHeadAdd.jsp中,<html:select name ="trainHeadForm" property="deptName" style="width:160" value="你传过去的值"> <html:options collection ="deptName" property ="id" labelProperty ="description" /> </html:select> 你把showDeptName()返回值改成Vector,然后在execute里修改Vector DeptName = showDeptName();后面跟上 DeptName.insertElementAt("你原来页面上显示的",0); request.setAttribute ("deptName", DeptName);然后再试试ok??? <html:select name ="trainHeadForm" property="deptName" style="width:160" value="<%= 你传过去的值 %>"> <html:options collection ="deptName" property ="id" labelProperty ="description" /> </html:select> 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> To: lmh7607(海风) 你好! 我将我的步骤都写出来,麻烦你帮我看看具体如何做,谢谢例如:为了显示一个部门名称:第一步:我写了一个javabean,DeptNameBean.java 其代码如下:这是一个struts的例子介绍的,我按着这样的方式做:public class DeptNameBean { private int id; //机务段名称ID private String description; //机务段描述即机务段名称 public DeptNameBean() { } public DeptNameBean( int id, String description ) { this.id = id; this.description = description; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; }}第二步:在Action中,我写了一个函数 showDeptName() public Collection showDeptName() throws Exception{ //连接数据库 getConnection(); PreparedStatement ps = null; ResultSet rs = null; String sql = null; ArrayList arrayList = new ArrayList(); try{ sql = "select distinct * from Btbl_OrganizationInfo order by DeptName"; ps = conn.prepareStatement(sql); rs = ps.executeQuery(); while(rs.next()){ //调用DeptNameBean arrayList.add(new DeptNameBean(rs.getInt("OrganizationID"),rs.getString("DeptName"))); } }catch(Exception ex){ System.out.println("error call showDeptName():" + ex.getMessage()); }finally{ //释放数据库连接 if(conn != null) conn.close(); if(ps != null) ps.close(); if(rs != null) rs.close(); } //返回结果 return arrayList; }第三步:我在execute方法中是这样做的: if(trainHeadForm.getOperate().equals("findtoupdateTrainHead")){ //显示所属机务段名称 Collection DeptName = showDeptName(); HttpSession session = httpServletRequest.getSession(); session.setAttribute("deptName",DeptName); //显示车头类型名称 Collection HeaderTypeName = showHeaderTypeName(); session.setAttribute("headerTypeName",HeaderTypeName); return (actionMapping.findForward("find_update_trainhead")); }第四步:我在页面上的标签是这样写的: <html:select name ="trainHeadForm" property="deptName" style="width:160" > <html:options collection ="deptName" property ="id" labelProperty ="description" /> </html:select>这个id是DeptNameBean.java中的id,description是DeptNameBean.java中的description。通过这样做后,例如显示的页面是:updateTrainHead.jsp,通过这样运行后在页面上就正确的显示出数据了。 第5步:我现在有一个连接例如从trainHeadMain.jsp页面连接到updateTrainHead.jsp页面,实现修改功能,就相在trainHeadMain.jsp中显示出所有的数据,然后点击某个连接到updateTrainHead.jsp页面,实现修改功能。我在trainHeadMain.jsp中,将所有的值传过去,<a href="../trainhead/trainHead.do?operate=findtoupdateTrainHead&trainHeaderID=<bean:write name="findalltrainhead" property="trainHeaderID"/>&trainHeaderCode=<bean:write name="findalltrainhead" property="trainHeaderCode"/>&deptID=<bean:write name="findalltrainhead" property="deptID"/>&deptName=<bean:write name="findalltrainhead" property="deptName"/>&headTypeID=<bean:write name="findalltrainhead" property="headTypeID"/>&headerTypeName=<bean:write name="findalltrainhead" property="headerTypeName"/>&telNum=<bean:write name="findalltrainhead" property="telNum"/>"><bean:write name="findalltrainhead" property="trainHeaderCode"/> </a>,在updateTrainHead.jsp中我查看了运行后的文件,里面也显示出了值,就表示值是传过去的,当标签为html:text的时候,上面就显示了传过去的值,但是,当为html:select的时候,就不显示传过去的值,始终默认显示第一个值。正确的是应该显示当前传过去的值。 看不出什麼大的錯誤,可以檢查一下我下面所說的: <html:select name ="trainHeadForm" property="deptName" style="width:160" > <html:options collection ="deptName" property ="id" labelProperty ="description" /> </html:select>如果select處在form裡面,就沒有必要加上name屬性了,如果寫上了name屬性,就要對應了struts-config.xml設置的form-bean name屬性,否則的話就出錯了. 首先要谢谢楼主的厚爱,给我发信息来看这个问题,我看了一下觉的还你的JSP里的问题。你把你的“<html:select name ="trainHeadForm" property="deptName" style="width:160" > <html:options collection ="deptName" property ="id" labelProperty ="description" /> </html:select>”改成下面的结构试一下<html:select property="value(rs_你要邦定的字段名)" size="1" style="width:100pt"><html:options collection="要邦定的字段的表名" property="value" labelProperty ="description"/>还有就是你的Struts_comfig.xml文件里的配置,要配置form-bean如上楼所说的那样子。</html:select> 建议检查<html:select>zhong <option>zhong .value and .text 的区别,因为要显示的值,和设置的值,通常不是一个。 struts1中js验证表单的问题! 登录问题 如何详细配置tomcat5.5日志? 配置Quartz问题:Unconvertible UTF-8 character begin... fdsfsda 那位高手能简单说一下j2ee项目的开发流程吗? 100分求jdom问题.解决马上送分. 实体类除了set/get是否可以加入增删改查的功能?? 求助关于jtable更新的问题 有没有做STRUTS开发的?? [讨论]这样的系统采用J2EE是否恰当。 关于web服务器和应用服务器的区别?
你试试用name和lableName属性,字符串集合可能可以做到。
<html:options name="optionValues" labelName="optionLabels"/>
不过由于这个标签每次要我构造collection很麻烦,
所以我现在自己写了一个select标签直接从数据库抽取信息,也绕开了bean这一层。
注意是 .value
<html:select name ="trainHeadForm" property="deptName" style="width:160" value="你传过去的值">
<html:options collection ="deptName" property ="id" labelProperty ="description" />
</html:select>
然后在execute里修改Vector DeptName = showDeptName();
后面跟上
DeptName.insertElementAt("你原来页面上显示的",0);
request.setAttribute ("deptName", DeptName);
然后再试试ok???
<html:options collection ="deptName" property ="id" labelProperty ="description" />
</html:select>
<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>
你好!
我将我的步骤都写出来,麻烦你帮我看看具体如何做,谢谢
例如:为了显示一个部门名称:第一步:我写了一个javabean,DeptNameBean.java
其代码如下:这是一个struts的例子介绍的,我按着这样的方式做:
public class DeptNameBean { private int id; //机务段名称ID
private String description; //机务段描述即机务段名称 public DeptNameBean() {
}
public DeptNameBean( int id, String description ) {
this.id = id;
this.description = description;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
第二步:在Action中,我写了一个函数 showDeptName()
public Collection showDeptName() throws Exception{
//连接数据库
getConnection();
PreparedStatement ps = null;
ResultSet rs = null;
String sql = null;
ArrayList arrayList = new ArrayList();
try{
sql = "select distinct * from Btbl_OrganizationInfo order by DeptName";
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
while(rs.next()){
//调用DeptNameBean
arrayList.add(new DeptNameBean(rs.getInt("OrganizationID"),rs.getString("DeptName"))); }
}catch(Exception ex){
System.out.println("error call showDeptName():" + ex.getMessage());
}finally{
//释放数据库连接
if(conn != null) conn.close();
if(ps != null) ps.close();
if(rs != null) rs.close();
}
//返回结果
return arrayList;
}第三步:我在execute方法中是这样做的:
if(trainHeadForm.getOperate().equals("findtoupdateTrainHead")){
//显示所属机务段名称
Collection DeptName = showDeptName();
HttpSession session = httpServletRequest.getSession();
session.setAttribute("deptName",DeptName);
//显示车头类型名称
Collection HeaderTypeName = showHeaderTypeName();
session.setAttribute("headerTypeName",HeaderTypeName);
return (actionMapping.findForward("find_update_trainhead"));
}
第四步:我在页面上的标签是这样写的:
<html:select name ="trainHeadForm" property="deptName" style="width:160" >
<html:options collection ="deptName" property ="id" labelProperty ="description" />
</html:select>
这个id是DeptNameBean.java中的id,description是DeptNameBean.java中的description。
通过这样做后,例如显示的页面是:updateTrainHead.jsp,通过这样运行后在页面上就正确的显示出数据了。
<a href="../trainhead/trainHead.do?operate=findtoupdateTrainHead&trainHeaderID=<bean:write name="findalltrainhead" property="trainHeaderID"/>&trainHeaderCode=<bean:write name="findalltrainhead" property="trainHeaderCode"/>&deptID=<bean:write name="findalltrainhead" property="deptID"/>&deptName=<bean:write name="findalltrainhead" property="deptName"/>&headTypeID=<bean:write name="findalltrainhead" property="headTypeID"/>&headerTypeName=<bean:write name="findalltrainhead" property="headerTypeName"/>&telNum=<bean:write name="findalltrainhead" property="telNum"/>"><bean:write name="findalltrainhead" property="trainHeaderCode"/>
</a>,
在updateTrainHead.jsp中我查看了运行后的文件,里面也显示出了值,就表示值是传过去的,当标签为html:text的时候,上面就显示了传过去的值,但是,当为html:select的时候,就不显示传过去的值,始终默认显示第一个值。正确的是应该显示当前传过去的值。
<html:options collection ="deptName" property ="id" labelProperty ="description" />
</html:select>如果select處在form裡面,就沒有必要加上name屬性了,如果寫上了name屬性,就要對應了struts-config.xml設置的form-bean name屬性,否則的話就出錯了.
“<html:select name ="trainHeadForm" property="deptName" style="width:160" >
<html:options collection ="deptName" property ="id" labelProperty ="description" />
</html:select>
”改成下面的结构试一下
<html:select property="value(rs_你要邦定的字段名)" size="1" style="width:100pt">
<html:options collection="要邦定的字段的表名" property="value" labelProperty ="description"/>
还有就是你的Struts_comfig.xml文件里的配置,要配置form-bean如上楼所说的那样子。
</html:select>
.text 的区别,因为要显示的值,和设置的值,通常不是一个。