Search code examples
jsfejbmanaged-beanprettyfaces

ManagedBean losing data when named


I'm using JSF 2.2 with PrettyFaces 3.3.3 in my Entreprise Application.

I mapped my Bean with Annotations (AdminCompaniesController.java) :

@ManagedBean
@ViewScoped
@URLMappings(mappings={
     @URLMapping(id = "admin-companies", pattern = "/admin/companies", viewId = "/admin/companies.jsf")
})
public class AdminCompaniesController implements Serializable {
     @EJB
     private CompanyService companyService;
     private Collection<Company> companies = new ArrayList<>();

     Company company;

     @PostConstruct
     public void init() {
          companies = companyService.getAllCompanys();
     }
}

In my view, I display a table with the data (companies.xhtml) :

<ui:repeat value="#{adminCompaniesController.companies}" var="company">
    <tr>
        <td><h:outputText value="#{company.name}" /></td>
    </tr>
</ui:repeat>

This works fine, i get 29 companies in the table. But as soon as I name my Bean : @ManagedBean(name = "companiesBean"), I lose all data. The view displays 0 result.

Does it have to do with the bean scope? Or maybe the EJB injection needs a name too?


Solution

  • You need to update the EL expressions in your .xhtml to match the name of the bean. If the bean is named "companiesBean", then your .xhtml should NOT be:

    <ui:repeat value="#{adminCompaniesController.companies}" var="company">
        <tr>
            <td><h:outputText value="#{company.name}" /></td>
        </tr>
    </ui:repeat>
    

    It should be the following, instead:

    <ui:repeat value="#{companiesBean.companies}" var="company">
        <tr>
            <td><h:outputText value="#{company.name}" /></td>
        </tr>
    </ui:repeat>
    

    Note the updated value in the <ui:repeat value='...'> attriute.