2011-08-30 3 views
1

나는 this tutorial을 읽습니다. 나는 노력하지만 오류가 발생합니다. Mysql과 netbean 7.0.1이 있습니다. 이름이 "customer"이고 열이 "ID int, name varchar, email varchar, description varchar"인 테이블입니다. 하이버 네이트를 매핑 테이블에 사용했다. myTemplateClient.xhtml, add.xhtml, edit.xhtml, details.xhtml : 보시다시피, 우리는 4보기를최대 절전 모드 + Facelet에서의 오류 JSF Managed Bean

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


} 

:

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

이 내 컨트롤러 : 이것은 내 모델 클래스입니다. 나는 "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을 사용하고 있기 때문에 URL을 문제가 것 같다. 대신 sendRedirect을 사용하십시오. 더 많은 정보는 여기에 있습니다 : http://www.javapractices.com/topic/TopicAction.do?Id=181

+0

저는 우리가 최대 절전 모드와 Facelet을 사용하고 있다고 생각하기 때문에 서블릿을 컨트롤러로 사용하지 않을 것입니다. 좀 더 자세한 정보를 보여 주시겠습니까? 저는 자바에서 초보자입니다. 고맙습니다 ! – xuanhung2401

+0

나는 그것을 고쳤다 고 생각한다. 당신의 도움을 주셔서 감사합니다. FacesContext.getCurrentInstance(). getExternalContext(). redirect ("faces/myTemplateClient.xhtml"); 대답이 도움이 되었다면 – xuanhung2401

+0

@ xuanhung2401을 대답의 왼쪽에있는 체크 박스 개요를 클릭하여 허용 된 답변으로 표시 할 수 있습니다. http://stackoverflow.com/faq#howtoask – Andrey