hibernate + Facelet JSF Managed Bean出错

时间:2011-08-30 09:22:07

标签: hibernate jsf

我读了this tutorial。我试试,但我得到错误。我们有Mysql和netbean 7.0.1。一个名为“customer”的表,列:“id int,name varchar,email varchar,description varchar”。我使用Hibernate映射表。这是我的模特课:

 /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model;

import domain.Customer;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author xuanhung2401
 */
public class CustomerModel {

    Session session = null;

    public CustomerModel(){
        session = HibernateUtil.getSessionFactory().getCurrentSession();
    }

    public List<Customer> getAllCustomer(int startId, int endId){
        List<Customer> list = null;
        try{
            session = HibernateUtil.getSessionFactory().getCurrentSession();
            Transaction ts = session.beginTransaction();
            Query query = session.createQuery("from Customer");
            list = (List<Customer>)query.list();
        }
        catch(Exception ex){
            ex.printStackTrace();
        }
        return list;
    }

    public Customer getById(int id){
        Customer c = new Customer();
        try{
            Transaction ts = session.beginTransaction();
            Query query = session.createQuery("from Customer as c where c.id = "+id );
            c = (Customer)query.uniqueResult();
        }catch(Exception ex){
            ex.printStackTrace();

        }

        return c;
    }

    public boolean updateCustomer(Customer c){
        try{
            Transaction ts = session.beginTransaction();
            session.update(c);  
            ts.commit();
            return true;

        }catch(Exception ex){
            ex.printStackTrace();
            return false;
        }       
    }

    public boolean addCustomer(Customer c){
        try{
            Transaction ts = session.beginTransaction();
            session.save(c);            
            ts.commit();            
            return true;
        }catch(Exception ex){
            ex.printStackTrace();
            return false;
        }        
    }

    public boolean deleteCustomer(Customer c){
        try{
            Transaction ts = session.beginTransaction();
            session.delete(c);
            ts.commit();
            return true;
        }catch(Exception ex){
            ex.printStackTrace();
            return false;
        }
    }
}

这是我的控制者:

   /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import domain.Customer;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;
import javax.faces.view.facelets.FaceletContext;
import model.CustomerModel;

/**
 *
 * @author xuanhung2401
 */
@ManagedBean
@SessionScoped
public class CustomerController {

    CustomerModel model;
    DataModel customers;
    Customer currentCustomer;
    /** Creates a new instance of CustomerController */
    public CustomerController() {
        model = new CustomerModel();
    }

    public DataModel getCustomers(){
        if (customers==null) {
            customers = new ListDataModel(model.getAllCustomer(1, 3));
        }
        return customers;
    }

    public void recreateModel(){
        customers = null;

    }


    public Customer getCurrentCustomer(){
        if (currentCustomer==null) {
            currentCustomer = new Customer();
        }
        return currentCustomer;
    }

    public String editCustomer(){
        int id = 0;        
        try {
            id = Integer.parseInt(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("id")) ;        
            currentCustomer = model.getById(id);
            if (currentCustomer!=null) {
                return "edit";
            }
        }catch(Exception ex){

        }                
        return "myTemplateClient";
    }
    public String editProcess(){  
        try{
            model.updateCustomer(currentCustomer);
            recreateModel();
        }catch(Exception ex){

        }        
        return "myTemplateClient";
    }

    public String addCustomer(){
        currentCustomer = new Customer();
        return "add";
    }

    public String addProcess(){   
        if (currentCustomer!=null) {
            model.addCustomer(currentCustomer);                     
            currentCustomer = new Customer();
            recreateModel();
        }        
        return "myTemplateClient";
    }

    public String deleteCustomer(){
        int id = 0;        
        id = Integer.parseInt(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("id")) ;        
        currentCustomer = model.getById(id);
        model.deleteCustomer(currentCustomer);
        recreateModel();
        return "myTemplateClient";
    }

    public String goIndex(){
        return "myTemplateClient";
    }

    public String prepareView(){
        currentCustomer = (Customer) customers.getRowData();
        if (currentCustomer !=null) {
            return "details";
        }
        return "myTemplateClient";
    }


}

如您所见,我们有4个视图:myTemplateClient.xhtml,add.xhtml,edit.xhtml,details.xhtml。我通过“return”viewname“;”命令导航。问题是:

  1. 地址栏与我正在查看的页面的地址不同。示例:我正在阅读myTemplateClient.xhtml,但地址栏是:localhost:8080 / iDo_Hibernate / faces / details.xhtml(必须是:localhost:8080 / iDo_Hibernate / faces / myTemplateClient.xhtml)。之后,当我跳转到add.xhtml时,地址栏是:localhost:8080 / iDo_Hibernate / faces / myTemplateClient.xhtml。

  2. 添加新客户后,重定向到“myTemplateClient”页面(这是索引页面,显示所有客户),地址栏为:localhost:8080 / iDo_Hibernate / faces / add.xhtml。现在,当我刷新浏览器时,它会为更多客户添加相同的信息。我尝试清除添加的对象,但仍然是错误。

    请帮我修复错误(原谅我,因为我的英语不好)。感谢您的阅读。

1 个答案:

答案 0 :(得分:1)

似乎网址问题是因为您使用forward在页面之间导航。请改用sendRedirect。更多信息请访问:http://www.javapractices.com/topic/TopicAction.do?Id=181