从DB中的JSF编辑数据表中的行

时间:2013-06-19 20:12:19

标签: jsf datatable edit backing-beans

我不知道我是否对事情这么复杂,但我无法弄清楚如何从我的数据表更新单行,这是我的代码:

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以修改其对应列中的名称值,如您所见,它绑定到列表元素的属性的值,因此当我单击保存更改按钮调用 editar <时/ strong>函数但是调试我可以看到作为参数传递给此函数的客户在set属性中没有任何值,所以为了在他的属性中设置数据的标题,它做得不好,我做错了什么? / p>

问候!

1 个答案:

答案 0 :(得分:3)

@RequestScoped替换@ViewScoped可以解决问题。

咨询这个thread,虽然它很珍贵,它包含一个关于管理Bean范围的良好tuto(巨大的BalusC)的链接,here

祝你好运:)。