我看過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「;」命令進行導航。問題是:
地址欄與我正在查看的頁面地址欄不一樣。例如:我正在閱讀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。
當我添加新客戶後,它重定向到「myTemplateClient」頁面(這是索引頁面,顯示所有客戶),地址欄爲:localhost:8080/iDo_Hibernate/faces/add.xhtml。現在,當我刷新瀏覽器時,它會向更多客戶添加相同的信息。我嘗試清除添加的對象,但仍然錯誤。
請幫我解決這個錯誤(原諒我,因爲我的英文不好)。謝謝閱讀。
因爲我認爲我們使用hibernate和Facelet,所以我們不會使用servlet作爲控制器。你能告訴我更多的細節,我只是在java中的新手。謝謝 ! – xuanhung2401
我想我修好了。謝謝你的幫助。 FacesContext.getCurrentInstance()。getExternalContext()。redirect(「faces/myTemplateClient.xhtml」); – xuanhung2401
@ xuanhung2401如果答案對您有幫助,可以通過點擊答案左側的複選框大綱將其標記爲接受的答案。 http://stackoverflow.com/faq#howtoask – Andrey