Tot
Tot

Reputation: 207

Unable to get <h:outputText value from Java class

I have a problem with taking names of tUsers and print them on the screen. May be i dont call the method for getting names properly () because when i call the function listAgencies, it prints them correctly in the Eclipse console. Thanks for any advices!

In .xthml file, I have:

<h:panelGrid id="panel2" columns="2" cellpadding="5">
            <c:forEach items="${agencyBean.listAgencies()}" var="inputBoxes">
                <h:outputText value="${inputBoxes.gettUser().name}" />
                <h:inputText />
            </c:forEach>
        </h:panelGrid>

My bean class:

@ManagedBean(name = "agencyBean")
@SessionScoped
public class AgencyBean {

    private TAgency tEventType = new TAgency();

    public void listAgencies() {
        EntityManager em = HibernateUtil.getEntityManager();
        // read the existing entries and write to console
        Query q = em.createQuery("select u from TAgency u");
        List<TAgency> agencyList = q.getResultList();
        for (TAgency agency : agencyList) {
            System.out.println("NAme: " + agency.gettUser().getName());

        }
    }

    public TAgency gettEventType() {
        return tEventType;
    }

    public void settEventType(TAgency tEventType) {
        this.tEventType = tEventType;
    }
}

TUser is another entity from where i want to get the name. I have getName() method which is public.

Upvotes: 0

Views: 474

Answers (1)

Luiggi Mendoza
Luiggi Mendoza

Reputation: 85789

The problem is here:

<c:forEach items="${agencyBean.listAgencies()}" ... >

It should look for a getter method for listAgencies attribute, but instead it is a void method that will be executed and there's nothing to access to.

The best bet would be:

  • Creating an attribute in your class called List<TAgency> listAgencies.
  • Define proper getter and setter methods for listAgencies attribute. NEVER define business logic in managed bean getters. Related: Why JSF calls getters multiple times
  • Probably, change the scope of the bean to @RequestScope to load this list every time users access to this view. Related: How to choose the right bean scope?
  • Load the list using @PostConstruct method.

Based on these advices, the code would look like this:

@ManagedBean(name = "agencyBean")
@RequestScoped
public class AgencyBean {

    private TAgency tEventType = new TAgency();
    private List<TAgency> listAgencies;

    @PostConstruct
    public void init() {
        EntityManager em = HibernateUtil.getEntityManager();
        // read the existing entries and write to console
        Query q = em.createQuery("select u from TAgency u");
        List<TAgency> agencyList = q.getResultList();
        for (TAgency agency : agencyList) {
            System.out.println("NAme: " + agency.gettUser().getName());
        }
    }

    public TAgency gettEventType() {
        return tEventType;
    }

    public void settEventType(TAgency tEventType) {
        this.tEventType = tEventType;
    }

    public List<TAgency> getListAgencies() {
        return listAgencies;
    }

    public void setListAgencies(List<TAgency> listAgencies) {
        this.listAgencies = listAgencies;
    }
}

And your JSF code:

<!-- Note: no parenthesis usage -->
<c:forEach items="#{agencyBean.listAgencies}" var="inputBoxes">
    <!-- no need to call the getter verbosely, Expression Language call it for you automatically -->
    <h:outputText value="#{inputBoxes.user.name}" />
    <!-- what you want to do here? -->
    <h:inputText />
</c:forEach>

Also, probably you don't want to use <c:forEach> but <ui:repeat> instead. Related: JSTL in JSF2 Facelets... makes sense?

Upvotes: 1

Related Questions