Question

I don't know if I complicate things so much but I can't figure out how to update a single row from my datatable, here's my code:

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);            
    }  

}

It's simply, via "getListaCustomer" retrieve a list of customers that it's render in datatable, this datatable has an edit column that's when is pressed calls editAction() that set isEditable variable to true for show an inputText for modify the name value in his correspondent column as you can see, the value it's binding to his attribute of the element of the list so when I click in save changes button calls editar function but debugging I can see that customer passed as parameter to this functions has no value in set attribute so it's not doing properly well the caption of data in order to set up in his attribute, what I'm doing wrong?

Regards!

Was it helpful?

Solution

Replacing @RequestScoped with @ViewScoped does the trick.

Consult this thread, precious as it is, it includes a link toward a good tuto (of the immense BalusC ) about Managed Bean Scopes, here.

Best of luck :).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top