我想使用下拉列表并从数据库中获取值,下拉列表应包含用于保存目的的公司代码&公司描述显示目的。
以下是我的代码:
Bean类:
package com.ims.master.company.bean;
public class CompanyBean {
private String id;
private String cmpCode;
private String cmpDes;
private String cmpStatus;
private String cmpCreated;
public CompanyBean(String cmpCode, String cmpDes) {
super();
this.cmpCode = cmpCode;
this.cmpDes = cmpDes;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getCmpCreated() {
return cmpCreated;
}
public void setCmpCreated(String cmpCreated) {
this.cmpCreated = cmpCreated;
}
public String getCmpCode() {
return cmpCode;
}
public void setCmpCode(String cmpCode) {
this.cmpCode = cmpCode;
}
public String getCmpDes() {
return cmpDes;
}
public void setCmpDes(String cmpDes) {
this.cmpDes = cmpDes;
}
public String getCmpStatus() {
return cmpStatus;
}
public void setCmpStatus(String cmpStatus) {
this.cmpStatus = cmpStatus;
}
}
DAO课程:
package com.ims.master.company.DAO;
import java.util.ArrayList;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import com.ims.hibernate.HibernateUtil;
import com.ims.master.company.bean.CompanyBean;
public class CompanyDAO {
SessionFactory factory = HibernateUtil.getFactory();
Session session = factory.openSession();
ArrayList<CompanyBean> recList = new ArrayList<CompanyBean>();
@SuppressWarnings("unchecked")
public ArrayList<CompanyBean> retrieveCmpCode()
{
System.out.println("=====inside DAO======");
Query query = session.createQuery("select b.cmpCode,b.cmpDes from CompanyBean b where b.cmpStatus=:val");
query.setParameter("val", "Y");
recList = (ArrayList<CompanyBean>) query.list();
System.out.println("=====value====="+recList);
return recList;
}
}
行动类:
package com.ims.master.masterData;
import java.util.ArrayList;
import com.ims.master.company.DAO.CompanyDAO;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;
public class MasterLookUp extends ActionSupport {
ArrayList companyCode;
public String getCompany()
{
CompanyDAO companyCodeValue = new CompanyDAO();
companyCode = companyCodeValue.retrieveCmpCode();
return SUCCESS;
}
public ArrayList getCompanyCode() {
return companyCode;
}
public void setCompanyCode(ArrayList companyCode) {
this.companyCode = companyCode;
}
}
JSP:
<s:select name="companyName" list="companyCode" key="label.companyName" listKey="cmpCode" listValue="cmpDes"/>
请建议我如何降价。 还建议如何在编辑部分中显示下拉值中的值。
答案 0 :(得分:1)
您无法将返回值转换为ArrayList<CompanyBean>
,因为在您的情况下,Hibernate会将查询返回的数据转换为List<Object[]>
。要返回List<CompanyBean>
,您可以使用其他查询。
您需要打开Hibernate会话来执行查询,完成后,您应该关闭会话。只有在由其他工具管理的情况下,您才必须关闭会话。您可以在http://httpd.apache.org/docs/2.4/install.html#customize中找到有关如何使用Hibernate会话的详细说明,以及链接的答案。
如果更改查询和属性类型,查询可能会返回List<CompanyBean>
,因此您可以在不进行强制转换的情况下分配值。
public class CompanyDAO {
public List<CompanyBean> retrieveCmpCode() throws Exception
{
System.out.println("=====inside DAO======");
SessionFactory factory = HibernateUtil.getFactory();
Session session = factory.openSession();
List<CompanyBean> recList;
try {
Query query = session.createQuery("from CompanyBean b where b.cmpStatus=:val");
query.setParameter("val", "Y");
recList = query.list();
System.out.println("=====value====="+recList);
return recList;
} finally {
session.close();
}
}
}
注意: @SuppressWarnings("unchecked")
不再需要。
在JSP中,您应该将select标记绑定到返回List<CompanyBean>
的action属性,就像您已经完成的那样。
<s:select name="companyName" list="companyCode" key="label.companyName" listKey="cmpCode" listValue="cmpDes"/>
行动
public class MasterLookUp extends ActionSupport {
private List<CompanyBean> companyCode;
public List<CompanyBean> getCompanyCode() {
return companyCode;
}
public void setCompanyCode(List<CompanyBean> companyCode) {
this.companyCode = companyCode;
}
private String cmpCode;
public String getCmpCode() {
return cmpCode;
}
public void setCmpCode(String companyCode) {
this.cmpCode = cmpCode;
}
public String getCompany() throws Exception
{
CompanyDAO companyCodeValue = new CompanyDAO();
companyCode = companyCodeValue.retrieveCmpCode();
return SUCCESS;
}
}
注意:获取/设置您应提供cmpCode
属性的select标记的默认/选定值。
答案 1 :(得分:0)
您可以在JSP中使用以下代码
<html:select property ="cmpDes">
<html:optionsCollection name ="cmpDes" />
</html:select>
当在您的JSP中添加上面的代码时,您的下拉列表将具有从DB获取的cmp描述。
以下是有学习struts-1的完美示例的网站,也与您提出一些想法的问题有关。 http://www.javabeat.net/struts-html-optionscollection-tag-htmloptionscollection/