2013-06-19 30 views
1

我不知道如果我那麼多複雜的事情,但我無法弄清楚如何從我的數據表更新單行,這裏是我的代碼:編輯從排DB

listado。 XHTML

<?xml version='1.0' encoding='UTF-8' ?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" 
     xmlns:h="http://java.sun.com/jsf/html" 
     xmlns:f="http://java.sun.com/jsf/core"> 
    <h:head> 
     <title>Facelet Title</title> 
    </h:head> 
    <h:body> 

     <h:form> 
     <h:dataTable border="1" value="#{guardarBean.listaCustomer}" var="o"> 

      <h:column> 
       <f:facet name="header">Customer ID</f:facet> 
       #{o.customerId} 
      </h:column> 

      <h:column> 
       <f:facet name="header">Discount Code</f:facet> 
       #{o.discountCode.discountCode} 
      </h:column> 

      <h:column> 
       <f:facet name="header">Zip</f:facet> 
       #{o.zip.zipCode} 
      </h:column> 

      <h:column> 
       <f:facet name="header">Name</f:facet> 
       <h:inputText value="#{o.name}" rendered="#{guardarBean.isEditable}"/>    
       <h:outputText value="#{o.name}" rendered="#{not guardarBean.isEditable}"/> 
      </h:column> 

      <h:column> 
       <f:facet name="header">Address 1</f:facet> 
       <h:outputText value="#{o.addressline1}" /> 
      </h:column> 

      <h:column> 
       <f:facet name="header">Address 2</f:facet> 
       #{o.addressline2} 
      </h:column> 

      <h:column> 
       <f:facet name="header">City</f:facet> 
       #{o.city} 
      </h:column> 

      <h:column> 
       <f:facet name="header">State</f:facet> 
       #{o.state} 
      </h:column> 

      <h:column> 
       <f:facet name="header">Phone</f:facet> 
       #{o.phone} 
      </h:column> 

      <h:column> 
       <f:facet name="header">Fax</f:facet> 
       #{o.fax} 
      </h:column> 

      <h:column> 
       <f:facet name="header">Email</f:facet> 
       #{o.email} 
      </h:column> 

      <h:column> 
       <f:facet name="header">Credit Limit</f:facet> 
       #{o.creditLimit} 
      </h:column> 

      <h:column> 
       <f:facet name="header">Edit</f:facet>     
       <h:commandButton action="#{guardarBean.editAction()}" value="Editar" />         
      </h:column> 
      <h:column> 
       <f:facet name="header">Save</f:facet>         
       <h:commandButton value="Save Changes" action="#{guardarBean.editar(o)}"> 
        <f:ajax render="@form" execute="@form"/> 
       </h:commandButton>     
      </h:column> 
      <h:column> 
       <f:facet name="header">Delete</f:facet>     
       <h:commandButton action="#{guardarBean.borrar(o)}" value="Borrar"> 
       <f:ajax render="@form" /> 
       </h:commandButton>     
      </h:column> 

     </h:dataTable> 
     </h:form> 
    </h:body> 
</html> 

guardarBean.java

import app.dao.CustomerFacadeLocal; 
    import app.dao.DiscountCodeFacadeLocal; 
    import app.dao.MicroMarketFacadeLocal; 
    import app.entity.Customer; 
    import app.entity.DiscountCode; 
    import app.entity.MicroMarket; 
    import java.util.List; 
    import javax.ejb.EJB; 
    import javax.faces.bean.ManagedBean; 
    import javax.faces.bean.RequestScoped; 
    import javax.faces.context.FacesContext; 




@ManagedBean 
@RequestScoped 
public class GuardarBean { 
    @EJB 
    private CustomerFacadeLocal customerFacade1; 
    @EJB 
    private MicroMarketFacadeLocal microFacade; 
    @EJB 
    private DiscountCodeFacadeLocal discFacade; 


    public Integer getId(){ 
     return id; 
    } 

    public void setId(Integer id){ 
     this.id = id; 
    } 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 


    public String getAddress1() { 
     return address1; 
    } 

    public void setAddress1(String address1) { 
     this.address1 = address1; 
    } 

    public String getAddress2() { 
     return address2; 
    } 

    public void setAddress2(String address2) { 
     this.address2 = address2; 
    } 

    public String getCity() { 
     return city; 
    } 

    public void setCity(String city) { 
     this.city = city; 
    } 

    public String getState() { 
     return state; 
    } 

    public void setState(String state) { 
     this.state = state; 
    } 

    public String getPhone() { 
     return phone; 
    } 

    public void setPhone(String phone) { 
     this.phone = phone; 
    } 

    public String getFax() { 
     return fax; 
    } 

    public void setFax(String fax) { 
     this.fax = fax; 
    } 

    public String getEmail() { 
     return email; 
    } 

    public void setEmail(String email) { 
     this.email = email; 
    } 

    public Integer getCredit_limit() { 
     return credit_limit; 
    } 

    public void setCredit_limit(Integer credit_limit) { 
     this.credit_limit = credit_limit; 
    }   

    public String getDiscount() { 
    return discount; 
    } 

    public void setDiscount(String discount) { 
     this.discount = discount; 
    } 

    public String getZip() { 
     return zip; 
    } 

    public void setZip(String zip) { 
     this.zip = zip; 
    } 



    private Integer id; 
    private String name; 
    private String address1; 
    private String address2; 
    private String city; 
    private String state; 
    private String phone; 
    private String fax; 
    private String email; 
    private Integer credit_limit; 
    private String discount; 
    private String zip; 
    private boolean isEditable; 

    private List<DiscountCode> listaDiscount; 
    private List<Customer> listaCustomer; 



    public List<Customer> getListaCustomer() { 
     //FacesContext.getCurrentInstance().getExternalContext().getSession(true); 
     listaCustomer =(List<Customer>)customerFacade1.findAll(); 
     return listaCustomer; 
    } 

    public void setListaCustomer(List<Customer> listaCustomer) { 
     this.listaCustomer = listaCustomer; 
    } 

    public List<DiscountCode> getListaDiscount() { 
     listaDiscount = (List<DiscountCode>)discFacade.findAll(); 
     return listaDiscount; 
    } 

    public void setListaDiscount(List<DiscountCode> listaDiscount) { 
     this.listaDiscount = listaDiscount; 
    } 


    /** 
    * Creates a new instance of GuardarBean 
    */ 
    public GuardarBean() { 
    } 


    public void insertar(){ 

     Customer customer = new Customer();  

     DiscountCode dc = discFacade.find(discount.toCharArray()[0]); 
     customer.setDiscountCode(dc); 

     MicroMarket mm = microFacade.find(zip); 
     customer.setZip(mm); 

     customer.setName(name); 
     customer.setCustomerId(id); 
     customer.setAddressline1(address1); 
     customer.setAddressline2(address2); 
     customer.setCity(city); 
     customer.setCreditLimit(credit_limit); 
     customer.setEmail(email); 
     customer.setFax(fax); 
     customer.setPhone(phone); 
     customer.setState(state);    
     customerFacade1.create(customer); 

    } 

    public boolean isIsEditable() { 
      return isEditable; 
     } 

     public void setIsEditable(boolean isEditable) { 
      this.isEditable = isEditable; 
     } 

    public void editAction() { 

     setIsEditable(true);    
    } 

    public void editar(Customer customer){ 

     customerFacade1.edit(customer); 
     setIsEditable(false); 

    } 


    public void borrar(Customer c) 
    { 
     customerFacade1.remove(c);    
    } 

} 

它簡單地說,通過「getListaCustomer「檢索它在數據表中呈現的客戶列表,這個數據表有一個編輯列,當它被按下時調用editAction()設置isEditable變量爲true以顯示inputText以修改其通信員列中的名稱值你可以看到,它的值綁定到他的屬性列表中的元素,所以當我點擊保存更改按鈕調用編輯器功能,但調試我可以看到,作爲參數傳遞給客戶的這個函數在set屬性中沒有任何價值所以它沒有正確完善數據的標題以建立他的屬性,我做錯了什麼?

問候!

+2

嘗試用'@ ViewScoped'替換'@ RequestScoped'。 – Akheloes

+0

謝謝!現在就像一個魅力! – Enot

+1

很高興:) – Akheloes

回答

3

替換@RequestScoped@ViewScoped有訣竅。

請查閱這個thread,儘管它很珍貴,但它包含了一個關於Managed Bean Scopes(here)的良好tuto(關於巨大BalusC)的鏈接。

祝你好運:)。