简体   繁体   中英

JSF java list getting overwritten when new item is added

I have a simple POJO that takes in a name and number, and a backing class that saves this name and number to a list. Information is entered through the JSF page. However every time I add a new item to the list the value is overwritten with the new value and the a null value is added to the list. Here is my Object:

 package com.contact.TO;

import java.io.Serializable;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
@ManagedBean
@ViewScoped
public class ContactTO implements Serializable {

/**
 * 
 */
private static final long serialVersionUID = 1L;
private String name;
private String phoneNumber;


public ContactTO() {
    name=null;
    phoneNumber=null;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public String getPhoneNumber() {
    return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
    this.phoneNumber = phoneNumber;
}

}

My Backing:

package com.contact.backing;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import com.contact.TO.ContactTO;

@ManagedBean(name = "contactBacking")
@ViewScoped
public class ContactBacking implements Serializable {

/**
 * 
 */
private static final long serialVersionUID = 1L;
@ManagedProperty(value = "#{contactTO}")
private ContactTO contact;
private List<ContactTO> contacts;

public ContactBacking() {
    super();
    // TODO Auto-generated constructor stub
}

@PostConstruct
public void init() {

    contacts = new ArrayList<ContactTO>();

}

public void addContact() {
    contacts.add(contact);
    contact = new ContactTO();
    System.out.println("SIZE ="+contacts.size());

}

public void editContact(ContactTO editContact) {
    this.contact = editContact;

}

public void saveContact(String name, String phone, String oldName, String oldPhone) {
    for (ContactTO c : contacts) {
        if (c.getName().equalsIgnoreCase(oldName) && c.getPhoneNumber().equalsIgnoreCase(oldPhone)) {
            c.setName(name);
            c.setPhoneNumber(phone);
        } else {
            addContact();
        }
    }
}

public List<ContactTO> getContacts() {
    return contacts;
}

public void setContacts(List<ContactTO> contacts) {
    this.contacts = contacts;
}

public ContactTO getContact() {
    return contact;
}

public void setContact(ContactTO contact) {
    this.contact = contact;
}

}

And my jsf page:

    <!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://xmlns.jcp.org/jsf/html"
       xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
        xmlns:f="http://java.sun.com/jsf/core">
      <h:head>
        <title></title>
         <link href="./css/styles.css" rel="stylesheet" type="text/css" />
      </h:head>
    <h:body>
        <h:form>
            <h:inputText value="#{contactTO.name}" />
             <h:inputText value="#{contactTO.phoneNumber}" />
        <h:commandButton value="Save" action="#{contactBacking.addContact}">
            <f:ajax execute="@form" render="contactList" />
        </h:commandButton>
        <br />
        <h:panelGroup id="contactList">
            <h:dataTable
                value="#{contactBacking.contacts}" var="dataItem">
                <h:column>
                    <f:facet name="header">
                        <h:outputText value="Name" />
                    </f:facet>
                    <h:outputText value="#{dataItem.name}" />
                </h:column>

                <h:column>
                    <f:facet name="header">
                        <h:outputText value="Phone" />
                    </f:facet>
                    <h:outputText value="#{dataItem.phoneNumber}" />
                </h:column>
                <h:column>
                    <f:facet name="header">
                        <h:outputText value="Edit" />
                    </f:facet>
                    <h:commandButton value="Edit Here"
                        action="#{contactBacking.editContact(dataItem)}">

                        <f:ajax execute="@form" render="contactList" />
                    </h:commandButton>
                </h:column>

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

so I enter the first value as "jim" and "1234567" save it gets added to the list and I see on the page
Name Phone
jim 1234567 Edit(button)

Then if I enter a new value say "mary" "12345" I see on the page my old value replace an empty name and phone row entries with a edit button, and the size of my array/list is increased to two
Name Phone
mary 12345 Edit(button)
Edit(button)

When I look at it on debug the value/object being passed is null.

Thanks

Your problem is that ContactTO is a managed bean, because you annotated the class with

@ManagedBean
@ViewScoped
public class ContactTO implements Serializable {
...
}

In your EL code, you access the one and only one instance of this managed bean :

 <h:form>
       <h:inputText value="#{contactTO.name}" />
       <h:inputText value="#{contactTO.phoneNumber}" />
 ...

That's why you overwrite again and again your values.

I would advise you :

  • not to make ContactTo a managed bean
  • to hold and instance for edition as a property of your ContactBacking managed bean
  • to edit this instance attributes by accessing them in your EL code

     <h:form> <h:inputText value="#{contactBacking.contact.name}" /> <h:inputText value="#{contactBacking.contact.phoneNumber}" /> ... 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM