struts的html:select问题,谁解决了100分

qjhaaaaa 2003-10-15 04:37:59
为了从数据库中读取值显示到下拉列表框中
我是这样实现的
我写了一个bean
package com.zzcq.stationsrv.system.trainhead;

/**
*
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company:zzcq </p>
* @author qinjianhua
* @version 1.0
*/

public class DeptNameBean {

private int id; //机务段名称ID
private String description; //机务段描述即机务段名称

public DeptNameBean() {
}
/**
* 显示所属机务段名称
* @param id 机务段名称id
* @param description 机务段名称描述
*/
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中这样写的
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方法中
//显示所属机务段名称
Collection DeptName = showDeptName();
HttpSession session = httpServletRequest.getSession();
session.setAttribute("deptName",DeptName);

//显示车头类型名称
Collection HeaderTypeName = showHeaderTypeName();
session.setAttribute("headerTypeName",HeaderTypeName);

在jsp页面上的标签如下
<html:select name ="trainHeadForm" property="deptName" style="width:160" >
<html:options collection ="deptName" property ="id" labelProperty ="description" />
</html:select>

就实现了显示下拉列表框的问题
现在我有一个问题
在一个主页面例如trainHeadMain.jsp中
有一个列表显示出数据库中表中的所有数据
我点击一个按钮,将列表中的值传到修改页面
updateTrainHead.jsp,所有的值是传过去的
text中能显示传过去的值,但是html:select中值不是显示传过去的值,而是显示下拉列表框中的第一个值。这个问题怎么解决呢
...全文
263 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
IT凡人 2003-10-21
  • 打赏
  • 举报
回复
对不起,最近比较忙,你的问题解决了吗?
qjhaaaaa 2003-10-18
  • 打赏
  • 举报
回复
解决一下吗我都搞几天了
qjhaaaaa 2003-10-17
  • 打赏
  • 举报
回复
对的,问题还是没有解决我快疯了
Rayy 2003-10-17
  • 打赏
  • 举报
回复
现在问题还是没有解决?
aku0708 2003-10-17
  • 打赏
  • 举报
回复
up
qjhaaaaa 2003-10-17
  • 打赏
  • 举报
回复
启动时候抛出异常
StandardManager[/struts]: IOException while loading persisted sessions: java.io.EOFException
java.io.EOFException


tandardManager[/struts]: Exception loading sessions from persistent storage

java.io.EOFException


点击某个按钮的时候
出现异常
org.apache.jasper.JasperException: Cannot find bean under name deptName

at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:254)




我现在有疑问:


if(trainHeadForm.getOperate().equals("findtoupdateTrainHead")){
ArrayList DeptName = showDeptName();
DeptNameBean deptNameForm = new DeptNameBean();
deptNameForm.setDeptNameID((ArrayList)DeptName.get(0));
deptNameForm.setDetpNameDesc((ArrayList)DeptName.get(1));
ArrayList HeaderTypeName = showHeaderTypeName();
HeaderTypeNameBean headerTypeNameForm = new HeaderTypeNameBean();
headerTypeNameForm.setHeaderTypeNameID((ArrayList)HeaderTypeName.get(0));
headerTypeNameForm.setHeaderTypeNameDesc((ArrayList)HeaderTypeName.get(1));
return (actionMapping.findForward("find_update_trainhead"));
}

在这里面我定义两个form没有关系吧
另外你看看我的标签也没有什么错误呀


现在的问题还是没有解决
qjhaaaaa 2003-10-17
  • 打赏
  • 举报
回复
第三步我在execute方法中这样写的
if(trainHeadForm.getOperate().equals("findtoupdateTrainHead")){
ArrayList DeptName = showDeptName();
DeptNameBean deptNameForm = new DeptNameBean();
deptNameForm.setDeptNameID((ArrayList)DeptName.get(0));
deptNameForm.setDetpNameDesc((ArrayList)DeptName.get(1));
ArrayList HeaderTypeName = showHeaderTypeName();
HeaderTypeNameBean headerTypeNameForm = new HeaderTypeNameBean();
headerTypeNameForm.setHeaderTypeNameID((ArrayList)HeaderTypeName.get(0));
headerTypeNameForm.setHeaderTypeNameDesc((ArrayList)HeaderTypeName.get(1));
return (actionMapping.findForward("find_update_trainhead"));
}


第四步:我的标签是这样写的

<html:select name ="trainHeadForm" property="deptName" style="width:160" >
<html:option value="">请选择</html:option>
<html:options property ="deptNameID" labelProperty ="detpNameDesc"/>
</html:select>
<html:select name ="trainHeadForm" property="headerTypeName" style="width:160" >
<html:option value="">请选择</html:option>
<html:options property ="headerTypeNameID" labelProperty ="headerTypeNameDesc"/>
</html:select>
qjhaaaaa 2003-10-17
  • 打赏
  • 举报
回复
laipu(寻梦天涯) 你好,我已经按照你的那样该了,但是出错,并且不显示,抛出异常
我要在一个页面上显示两个下拉框
第一步:我的bean是这样写的,已经按照你那样修改:

/*****************************************************
DeptNameBean .java
******************************************************/
package com.zzcq.stationsrv.system.trainhead;

import java.util.ArrayList;
public class DeptNameBean {

private ArrayList deptNameID = new ArrayList(); //机务段名称ID
private ArrayList detpNameDesc = new ArrayList(); //机务段描述即机务段名称

public DeptNameBean() {
}
/*
* 显示所属机务段名称
* @param id 机务段名称id
* @param description 机务段名称描述
*/


public DeptNameBean( ArrayList deptNameID, ArrayList detpNameDesc ) {
this.deptNameID = deptNameID;
this.detpNameDesc = detpNameDesc;
}
public ArrayList getDeptNameID() {
return deptNameID;
}
public ArrayList getDetpNameDesc() {
return detpNameDesc;
}
public void setDeptNameID(ArrayList deptNameID) {
this.deptNameID = deptNameID;
}
public void setDetpNameDesc(ArrayList detpNameDesc) {
this.detpNameDesc = detpNameDesc;
}
}
/***********************************************
HeaderTypeNameBean .java
***********************************************/

package com.zzcq.stationsrv.system.trainhead;

import java.util.ArrayList;
public class HeaderTypeNameBean {
private ArrayList headerTypeNameID = new ArrayList(); //车头类型ID
private ArrayList headerTypeNameDesc = new ArrayList(); //车头类型描述即车头类型名称
public HeaderTypeNameBean() {
}

/**
* 显示车头类型名称
* @param id 车头类型id
* @param description 车头类型名称描述
*/

public HeaderTypeNameBean(ArrayList headerTypeNameID,ArrayList headerTypeNameDesc){
this.headerTypeNameID =headerTypeNameID;
this.headerTypeNameDesc = headerTypeNameDesc;
}
public ArrayList getHeaderTypeNameDesc() {
return headerTypeNameDesc;
}
public ArrayList getHeaderTypeNameID() {
return headerTypeNameID;
}
public void setHeaderTypeNameDesc(ArrayList headerTypeNameDesc) {
this.headerTypeNameDesc = headerTypeNameDesc;
}
public void setHeaderTypeNameID(ArrayList headerTypeNameID) {
this.headerTypeNameID = headerTypeNameID;
}

}
第二步:我在TrainHead.java中这样写的
public ArrayList showDeptName() throws Exception{
//连接数据库
getConnection();
PreparedStatement ps = null;
ResultSet rs = null;
String sql = null;
ArrayList arrayList = new ArrayList();
ArrayList deptNameID = new ArrayList();
ArrayList detpNameDesc = new ArrayList();
try{
sql = "select distinct * from Btbl_OrganizationInfo order by DeptName";
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
while(rs.next()){
deptNameID.add(rs.getString("OrganizationID"));
detpNameDesc.add(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;
}

public ArrayList showHeaderTypeName() throws Exception{
//连接数据库
getConnection();
PreparedStatement ps = null;
ResultSet rs = null;
String sql = null;
ArrayList arrayList = new ArrayList();
ArrayList HeaderTypeNameID = new ArrayList();
ArrayList HeaderTypeNameDesc = new ArrayList();
try{
sql = "select distinct * from Btbl_TrainHeaderType order by HeaderTypeName";
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
while(rs.next()){
HeaderTypeNameID.add(rs.getString("HeaderTypeID"));
HeaderTypeNameDesc.add(rs.getString("HeaderTypeName"));
}
}catch(Exception ex){
System.out.println("error call showHeaderTypeName():" + ex.getMessage());
}finally{
//释放数据连接
if(conn != null) conn.close();
if(ps != null) ps.close();
if(rs != null) rs.close();
}
//返回结果
return arrayList;
}
squallzeng 2003-10-17
  • 打赏
  • 举报
回复
补充一下collection ="deptName",在要邦定的表名和字段名是一样的吗?你这样写的就是表名和字段名一样是deptName,collection ="邦定的表名".还有就是你的“property="deptName"”要改成“property="value(rs_deptName)"”看看吧
squallzeng 2003-10-17
  • 打赏
  • 举报
回复
首先,我觉的是你的表可能建的有问题,不知道你的表有没有一个唯一的标志呢?你现在在你的下面代码中得到的是表。也就是说“property="deptName"”这个东东写的有问题,应该是你要邦定表中要显视的字段名才对的。把你的property="deptName"改成property="要显视的字段名"这样子试一下看看如何?
<html:select name ="trainHeadForm" property="deptName" style="width:160" >
<html:options collection ="deptName" property ="id" labelProperty ="description" />
</html:select>
IT凡人 2003-10-16
  • 打赏
  • 举报
回复
注:最后一行:httpServletRequest.setAttribute(),无效,删除
IT凡人 2003-10-16
  • 打赏
  • 举报
回复
怎么回事,居然提交了不能编辑吗?重来!

<html:select name ="formBean" property="id" style="width:160" >
<html:option value="0" >请先选择设备类型</html:option>
<html:options property="idList" labelProperty="descList" />
</html:select>
其中name ="formBean" ,可以省略,如果你只有一个form
property 是value值,labelProperty是text值,两者必须是各自的ArrayList以及扩展类型!
所以你的程序要改一下,改动如下:
package com.zzcq.stationsrv.system.trainhead;

/**
*
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company:zzcq </p>
* @author qinjianhua
* @version 1.0
*/

import java.util.ArrayList;

public class DeptNameBean {

private int id; //机务段名称ID
private String description; //机务段描述即机务段名称
// modified by laipu
private ArrayList idList = new ArrayList(); //机务段名称ID List
private ArrayList descList = new ArrayList(); //机务段描述即机务段名称 List
// end modified by laipu

public DeptNameBean() {
}
/**
* 显示所属机务段名称
* @param id 机务段名称id
* @param description 机务段名称描述
*/
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;
}
// modified by laipu ,相关注释略
public ArrayList getIdList() {
return idList;
}
public void setIdList(ArrayList idList) {
this.idList = idList;
}
public ArrayList getDescList() {
return descList;
}
public void setDescList(ArrayList descList) {
this.descList = descList;
}
// end modified by laipu
//
}

然后我在action中这样写的
public Collection showDeptName() throws Exception{
//连接数据库
getConnection();
PreparedStatement ps = null;
ResultSet rs = null;
String sql = null;
ArrayList arrayList = new ArrayList();
// modified by laipu ,相关注释略
ArrayList idList = new ArrayList();
ArrayList nameList = new ArrayList();
// end modified by laipu

try{
sql = "select distinct * from Btbl_OrganizationInfo order by DeptName";
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
while(rs.next()){
//调用DeptNameBean
// modified by laipu ,相关注释略
//arrayList.add(new DeptNameBean(rs.getInt("OrganizationID"),rs.getString("DeptName")));
idList.add(rs.getString("OrganizationID"));
nameList.add(rs.getString("DeptName"));
}
arrayList.add(idList);
arrayList.add(nameList);
// end modified by laipu
}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方法中
//显示所属机务段名称
Collection DeptName = showDeptName();
DeptNameBean form = new DeptNameBean();
form.setIdList((ArrayList)DeptName.get(0));
form.setDescList((ArrayList)DeptName.get(1));
//不需要用任何session,request 的Attribute,只用缺省的form 就可以,不需要设置任何变量
//HttpSession session = httpServletRequest.getSession();
//session.setAttribute("deptName",DeptName);

//显示车头类型名称
//Collection HeaderTypeName = showHeaderTypeName();
//session.setAttribute("headerTypeName",HeaderTypeName);
httpServletRequest.setAttribute()
IT凡人 2003-10-16
  • 打赏
  • 举报
回复
<html:select name ="trainHeadForm" property="xxxxx" style="width:160" >
<html:option value="0" >请先选择设备类型</html:option>
<html:options property="dtdId" labelProperty="dtdList" />
</html:select>

property 是value值,labelProperty是text值,两者必须是各自的ArrayList以及扩展类型!
所以你的程序要改一下,改动如下:
package com.zzcq.stationsrv.system.trainhead;

/**
*
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company:zzcq </p>
* @author qinjianhua
* @version 1.0
*/

import java.util.ArrayList;

public class DeptNameBean {

private int id; //机务段名称ID
private String description; //机务段描述即机务段名称
// modified by laipu
private ArrayList idList; //机务段名称ID List
private ArrayList descList; //机务段描述即机务段名称 List
// end modified by laipu

public DeptNameBean() {
}
/**
* 显示所属机务段名称
* @param id 机务段名称id
* @param description 机务段名称描述
*/
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;
}
// modified by laipu ,相关注释略
public ArrayList getIdList() {
return idList;
}
public void setIdList(ArrayList idList) {
this.idList = idList;
}
public ArrayList getDescList() {
return descList;
}
public void setDescList(ArrayList descList) {
this.descList = descList;
}
// end modified by laipu
//
}

然后我在action中这样写的
public Collection showDeptName() throws Exception{
//连接数据库
getConnection();
PreparedStatement ps = null;
ResultSet rs = null;
String sql = null;
ArrayList arrayList = new ArrayList();
// modified by laipu ,相关注释略
ArrayList idList = new ArrayList();
ArrayList nameList = new ArrayList();
// end modified by laipu

try{
sql = "select distinct * from Btbl_OrganizationInfo order by DeptName";
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
while(rs.next()){
//调用DeptNameBean
// modified by laipu ,相关注释略
//arrayList.add(new DeptNameBean(rs.getInt("OrganizationID"),rs.getString("DeptName")));
idList.add(rs.getString("OrganizationID"));
nameList.add(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方法中
//显示所属机务段名称
Collection DeptName = showDeptName();
HttpSession session = httpServletRequest.getSession();
session.setAttribute("deptName",DeptName);

//显示车头类型名称
Collection HeaderTypeName = showHeaderTypeName();
session.setAttribute("headerTypeName",HeaderTypeName);

dytsoft 2003-10-16
  • 打赏
  • 举报
回复
另外,多上csdn,多多解贴。
强制结贴就会降低你的信誉值。
dytsoft 2003-10-16
  • 打赏
  • 举报
回复
你尝试一下
select的 property重新起一个名字,例如id, 我标志xxxx的地方,当然,要在form中对应,并写相应的get set方法。
<html:select name ="trainHeadForm" property="xxxxx" style="width:160" >
<html:options collection ="deptName" property ="id" labelProperty ="description" />
</html:select>
----------------
下面是以前的代码;
<html:select property="dtdId" onchange="submitForm(this.form)">
<option >请先选择设备类型</option>
<html:options collection="dtdList" property="dtdId" labelProperty="dtdName"/>
</html:select>

希望队你有用
qjhaaaaa 2003-10-16
  • 打赏
  • 举报
回复
望多多帮忙
qjhaaaaa 2003-10-15
  • 打赏
  • 举报
回复
值已经得到了
,但是下拉框的值始终显示的不是当前传过去的值
ljmok202 2003-10-15
  • 打赏
  • 举报
回复
你把值传递到下一页的时候用破post方法,下一页就可以接受到了
qjhaaaaa 2003-10-15
  • 打赏
  • 举报
回复
我用了两种方法去实现都没有成功
例如: 有两个页面trainHeadMain.jsp和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>
我是通过这样连接传值过去的
在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"));
}
结果:text显示,下拉列表框还是不显示当前传过去的值。
方法二.我是这样连接的
<a href="../trainhead/trainHead.do?operate=findtoupdateTrainHead&trainHeaderID=<bean:write name="findalltrainhead" property="trainHeaderID"/>"><bean:write name="findalltrainhead" property="trainHeaderCode"/>
</a>
我将这个trainHeaderID传过去,我在
if(trainHeadForm.getOperate().equals("findtoupdateTrainHead")){
trainHeadForm =findTrainHead(trainHeadForm.getTrainHeaderID());
Collection DeptName = showDeptName();
HttpSession session = httpServletRequest.getSession();
session.setAttribute("deptName",DeptName);
//显示车头类型名称
Collection HeaderTypeName = showHeaderTypeName();
session.setAttribute("headerTypeName",HeaderTypeName);
session.setAttribute("trainHeaderCode",trainHeadForm.getTrainHeaderCode());
session.setAttribute("deptName",trainHeadForm.getDeptName());
session.setAttribute("headerTypeNameaa",trainHeadForm.getHeaderTypeName());
session.setAttribute("telNum",trainHeadForm.getTelNum());
return (actionMapping.findForward("find_update_trainhead"));
}
这些值都是正确的,我在updateTrainHead.jsp页面取值, String trainHeaderCode = (String)session.getAttribute("trainHeaderCode");
System.out.println(trainHeaderCode);
String deptName = (String)session.getAttribute("deptName");
System.out.println(deptName);
测试打印出来的值也是正确的
在html;text的value我用jsp的格式输出来正确
但是在html:select标签中
我是这样做的
<html:select name ="trainHeadForm" property="headerTypeName" style="width:160" value ="<%=headerTypeName%>" >
<html:options collection ="headerTypeName" property ="id" labelProperty ="description"/>
</html:select>
结果,还是不显示当前传过去的值,

67,512

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧