2011-08-30 93 views
1

我看过this tutorial。我尝试,但我得到错误。我们有Mysql和netbean 7.0.1。一个名为“customer”的表,列:“id int,name varchar,email varchar,description varchar”。我用Hibernate来映射表。这是我的模型类:Hibernate + Facelet JSF Managed Bean出错

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

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

+0

因为我认为我们使用hibernate和Facelet,所以我们不会使用servlet作为控制器。你能告诉我更多的细节,我只是在java中的新手。谢谢 ! – xuanhung2401

+0

我想我修好了。谢谢你的帮助。 FacesContext.getCurrentInstance()。getExternalContext()。redirect(“faces/myTemplateClient.xhtml”); – xuanhung2401

+0

@ xuanhung2401如果答案对您有帮助,可以通过点击答案左侧的复选框大纲将其标记为接受的答案。 http://stackoverflow.com/faq#howtoask – Andrey