0

Here is my code

Pojo

public class Deal implements Serializable {

private int id;
private String name;
private String description;
private Customer customer;
    //getter setter omitted
}

public class Customer implements Serializable {

private int id;
private String name;
private String email;
private String phone;
    //getter setter and equal hashcode omitted
}

Managed Bean

@ManagedBean(name="dealBean")
@ViewScoped
public class DealBean implements Serializable {

private List<Customer> customerList;
private List<Deal> dealList;
private Deal deal;

    @PostConstruct
    public void init() {
    deal = new Deal();
    dealList = new ArrayList<Deal>();
    customerList = new ArrayList<Customer>();
    customerList.add(new Customer(1, "MPRL", "mprl@mail.com", "1234455"));
    customerList.add(new Customer(2, "Total", "total@mail.com", "3434323"));
    customerList.add(new Customer(3, "Petronas", "petronas@mail.com", "8989876"));

}
    //getter setter omitted

}

Customer Converter

@FacesConverter("customerConverter")
public class CustomerConverter implements Converter {

@Override
public Object getAsObject(FacesContext arg0, UIComponent arg1, String customerID) {
    DealBean dealBean = (DealBean)  FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("dealBean");
    if (dealBean != null) {
        List<Customer> customerList = dealBean.getCustomerList();
        for (Customer customer : customerList) {
            if (customerID.equals(String.valueOf(customer.getId()))) {
                return customer;
            }
        }
    }
    return null;
}

@Override
public String getAsString(FacesContext arg0, UIComponent arg1, Object obj) {
    if (obj != null) {
        return String.valueOf(((Customer)obj).getId());
    }
    return null;
}

}

XHTML

Customer : <h:selectOneMenu id="customer" value="#{dealBean.deal.customer}">
        <f:converter converterId="customerConverter" />
        <f:selectItems value="#{dealBean.customerList}" var="cus"
            itemLabel="#{cus.name}" itemValue="#{cus}" />
    </h:selectOneMenu>

When the managed bean is in request or session scope, the Customer pojo is set correctly to Deal pojo. The problem is when the managed bean is in View scope, the Customer pojo is set to Deal pojo as NULL.

I am using JSF 2.2.0

Thanks much for the help in advance.

1 Answers1

1

It's not the converter, is the view scoped the one broken: Since you're using JSF tags, you cannot use @ViewScoped annotation, because it was removed from specification and recovered only for CDI usage. You could use omnifaces view scoped or the components of apache myFaces (I personally recommend omnifaces). You can confirm this creating a

System.out.print("Creating");

in the constructor and checking how is called each Ajax request, so the bean is not recovered and since is marked as view and is a partial request, the values are not setted again (unless you send all the form, which is not a nice solution), other workaround could be making the bean request and recover all the data each request, making it Session (but will be alive for the session), or the @ConvesationScoped, in which you'll have to destroy and start the conversation manually. Again, my first recommendation could be change to a Java ee server compliant and use the CDI annotations since JSF are being depreciated and not updated anymore

rekiem87
  • 1,565
  • 18
  • 33
  • Thanks for the answer. But why i cannot use JSF tags with @ViewScoped? That scope is added in JSF2, isn't it? Sure I will have a look at omnifaces. – user3655705 May 20 '14 at 09:15
  • Yep, but was recovered as CDI annotation, you can look, the `javax.faces.view.ViewScoped` is the working one (CDI), meanwhile `javax.faces.bean.ViewScoped` (JSF) is broken in the new implementations, they even where put in different package with the intention of remove the second one in the near future, all of this because the Java EE specification has grow and now covers all the JSF lifecycle, so it's considered as not necessary now – rekiem87 May 20 '14 at 09:22