gpt4 book ai didi

java - 从下拉列表中的数据库检索值() using Struts 2 and Hibernate

转载 作者:行者123 更新时间:2023-11-30 08:03:21 25 4
gpt4 key购买 nike

我想使用下拉菜单并从数据库中获取下拉菜单中的值,下拉菜单应包含用于保存目的的公司代码和用于显示目的的公司描述。

下面是我的代码:

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;
}
}

Action 类:

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"/>

请建议我如何将值放入下拉列表中。还建议在编辑部分选择如何显示下拉列表中的值。

最佳答案

您无法将返回值转换为 ArrayList<CompanyBean> ,因为在您的情况下 Hibernate 会将查询返回的数据转换为 List<Object[]> 。返回List<CompanyBean>您可以使用另一个查询。

您需要打开 Hibernate session 来执行查询,完成后您应该关闭 session 。仅当 session 由其他工具管理时,您才不必关闭它。您可以在How to display a list of database records (retrieved via Hibernate) to a JSP page in Struts 2中找到如何使用Hibernate session 的详细说明,以及链接的答案。

查询可能返回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 标记绑定(bind)到返回 List<CompanyBean> 的操作属性。 ,与您已经做过的类似。

<s:select name="companyName" list="companyCode" key="label.companyName" listKey="cmpCode" listValue="cmpDes"/>

Action

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属性。

关于java - 从下拉列表中的数据库检索值(<s :select>) using Struts 2 and Hibernate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31549372/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com