0

I am trying to create a regular contact POJO the takes in a phone number and a name. Then on the JSF I am reading in those properties and trying to call a backing method to add them to a contact list. But for some reason the contact keeps coming back as null. I am trying to use the manage bean property, but no luck. Any help would be appreciated thanks. Here is my Contact

    package com.contact.TO;

import java.io.Serializable;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.SessionScoped;
@ManagedBean
@RequestScoped
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;
    }

}

Here is my backing class

package com.contact.backing;

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

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.SessionScoped;
import javax.faces.view.ViewScoped;

import com.contact.TO.ContactTO;

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

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

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

    public void addContact(){
        contacts.add(contact);
        contact=null; 
    }
    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;
    }

}

Here is my jsf:

<!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 render="contactList" />             
    </h:commandButton>
    <h:panelGroup id="contactList">
        <ui:repeat var="c" value="#{contactBacking.contacts}" varStatus="status">
            <h:inputText value="#{c}"></h:inputText>

        </ui:repeat>
    </h:panelGroup>
    </h:form>
</h:body></html>

As you can see I just have two inputs that take in the properties name and phoneNumber and action button calling the backing method contactBacking.addContact. But all I get back is null for the contact.Do have the managebean or scope wrong?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555

2 Answers2

2

Your inputs content are not posted, when using f:ajax, the default execute is @this, meaning only the action is posted. Add execute="@form".

More information :

Alexandre Lavoie
  • 8,711
  • 3
  • 31
  • 72
0

Your import are wrong, use javax.faces.bean and not javax.faces.view for managed bean Scopes, the package javax.faces.view is part of CDI spec.

Eder F. Freitas
  • 48
  • 2
  • 11