0

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?

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
Thrax
  • 1,926
  • 1
  • 17
  • 32

1 Answers1

2

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.

Lincoln
  • 3,151
  • 17
  • 22
  • It worked. Thanks a lot! Seems like the name annotation overrides the class name for data binding in the view. – Thrax Oct 17 '14 at 07:23
  • 1
    The @Named annotation defaults to the class name, until you override it by manually specifying a name ;) – Lincoln Oct 18 '14 at 03:05