gpt4 book ai didi

java - 删除jsf数据表中的行

转载 作者:行者123 更新时间:2023-12-01 05:14:43 25 4
gpt4 key购买 nike

我想删除jsf数据表中的一行。我正在使用 jsf 和 hibernate 和 spring。但删除操作不起作用。客户管理bean.java

@ManagedBean(name="CustomerMB")
@RequestScoped
public class Customermanagedbean implements Serializable{
@ManagedProperty(value="#{CustomerBoImpl}")
ICustomerBo customerBoImpl;
List<Customer> CustomerList;
public int customerId;
public String name;
public String address;
public String createdDate;



public ICustomerBo getCustomerBoImpl() {
return customerBoImpl;
}
public void setCustomerBoImpl(ICustomerBo customerBoImpl) {
this.customerBoImpl = customerBoImpl;
}

public List<Customer> getCustomerList() {
CustomerList=new ArrayList<Customer>();
CustomerList.addAll(getCustomerBoImpl().findAllCustomer());

return CustomerList;
}



public void setCustomerList(List<Customer> customerList) {
CustomerList = customerList;
}
public String deleteCustomer(Customer customer){
getCustomerBoImpl().deleteCustomer(customer);

return "";


}// getter and setter method

CustomerBoImpl.java

@Transactional(readOnly = true)
public class CustomerBoImpl implements ICustomerBo{

ICustomerDao customerDaoImpl;



public ICustomerDao getCustomerDaoImpl() {
return customerDaoImpl;
}

public void setCustomerDaoImpl(ICustomerDao customerDaoImpl) {
this.customerDaoImpl = customerDaoImpl;
}
@Transactional(readOnly = false)
@Override
public void deleteCustomer(Customer customer){
getCustomerDaoImpl().deleteCustomer(customer);
}

@Override
public List<Customer> findAllCustomer(){

return getCustomerDaoImpl().findAllCustomer();
}
}

CustomerDaoImpl.java

public class CustomerDaoImpl implements ICustomerDao{

private SessionFactory sessionFactory;
public SessionFactory getSessionFactory() {
return sessionFactory;}
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
public void deleteCustomer(Customer customer){
sessionFactory.openSession();
getSessionFactory().getCurrentSession().delete(customer);
}


public List<Customer> findAllCustomer(){
sessionFactory.openSession();

List list = getSessionFactory().getCurrentSession

().createQuery("from Customer").list();
return list;

}
}

默认.xhtml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
>
<h:head>
<h:outputStylesheet library="css" name="table-style.css" />
</h:head>

<h:body>



<h:dataTable value="#{CustomerMB.getCustomerList()}" var="c"
styleClass="order-table"
headerClass="order-table-header"
rowClasses="order-table-odd-row,order-table-even-row"
>

<h:column>
<f:facet name="header">
Customer ID
</f:facet>
#{c.customerId}
</h:column>

<h:column>
<f:facet name="header">
Name
</f:facet>
#{c.name}
</h:column>

<h:column>
<f:facet name="header">
Address
</f:facet>
#{c.address}
</h:column>

<h:column>
<f:facet name="header">
Created Date
</f:facet>
#{c.createdDate}

</h:column>
<h:column>

<f:facet name="header">Action</f:facet>

<h:commandButton value="Delete" action="#{CustomerMB.deleteCustomer(c)}" />

</h:column>

</h:dataTable>

出了什么问题?请帮助我。

最佳答案

检查 CustomerDaoImpl 类的 deleteCustomer 方法。您可以尝试如下示例

   public void deleteCustomer(Customer customer){
getHibernateTemplate().delete(customer);
}

在您的 CustomerBoImpl 类中,更改此内容

   @Autowired
ICustomerDao customerDaoImpl;

解决方法是创建一个自定义类 (CustomHibernateDaoSupport) 并扩展“HibernateDaoSupport”并自动连接 session 工厂,并且您的 DAO 类扩展此类

     public abstract class CustomHibernateDaoSupport extends HibernateDaoSupport{    
@Autowired
public void anyMethodName(SessionFactory sessionFactory){
setSessionFactory(sessionFactory);
}
}

关于java - 删除jsf数据表中的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11433666/

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