1

我想删除 jsf 数据表中的一行。我正在使用jsf、hibernate和spring。但删除操作不起作用。Customermanagedbean.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>

怎么了?请帮我。

4

1 回答 1

0

Check your CustomerDaoImpl class's deleteCustomer method. Your may try as like as below sample

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

In your CustomerBoImpl class, change this

   @Autowired
   ICustomerDao customerDaoImpl;

The workaround is create a custom class (CustomHibernateDaoSupport) and extends the “HibernateDaoSupport” and auto wire the session factory, and your DAO classes extends this class

     public abstract class CustomHibernateDaoSupport extends HibernateDaoSupport{    
          @Autowired
          public void anyMethodName(SessionFactory sessionFactory){
              setSessionFactory(sessionFactory);
          }
      }
于 2012-07-13T05:14:50.553 回答