开发者

Error with hibernate + Facelet JSF Managed Bean

I read this tutorial. I try, but i get error. We have Mysql and netbean 7.0.1. A table with name : "customer", columns : "id int, name varchar, email varchar, description varchar". I used Hibernate to mapping table. This is my model class :

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

This is my controller :

   /*
 * 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 curr开发者_如何学编程entCustomer;
    }
    
    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";
    }
    
   
}

As you see, we have 4 view : myTemplateClient.xhtml, add.xhtml, edit.xhtml, details.xhtml. i navigate by " return "viewname"; " command. Problem are :

  1. Address bar is not the same address with page i am viewing. Example : i'm reading myTemplateClient.xhtml, but address bar is : localhost:8080/iDo_Hibernate/faces/details.xhtml ( it must be : localhost:8080/iDo_Hibernate/faces/myTemplateClient.xhtml). After that, when i jump to add.xhtml, address bar is : localhost:8080/iDo_Hibernate/faces/myTemplateClient.xhtml.

  2. After i add new customer, it's redirect to "myTemplateClient" page ( this is index page, it shows all customer) with address bar is : localhost:8080/iDo_Hibernate/faces/add.xhtml. Now, when i refresh my browser, it adds more customer with the same information. I try to clear added object, but still error.

    Please help me fix that errors (Forgive me because my English is not good). Thanks for reading.


It seems that the issue with urls is because you are using forward to navigate between pages. Use sendRedirect instead. More information here: http://www.javapractices.com/topic/TopicAction.do?Id=181

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜