Deflandrex
Deflandrex

Reputation: 11

JSF h link takes last item in list instead of current

I got a list of objects displayed in a page. This is the view:

<h:form>
    <h:dataTable var="o" value="#{opleidingController.opleidingen}" border="0" >
        <h:column>
            <f:facet name="header">Code</f:facet>
            <h:link value="#{o.opl_code}" outcome="#{opleidingController.getOpleidingByCode(o.opl_code)}" />
        </h:column>
        <h:column>
            <f:facet name="header">Titel</f:facet>
            <h:outputText value="#{o.opl_titel} "></h:outputText>
        </h:column>
        <h:column>
            <f:facet name="header">Thema</f:facet>
            <h:outputText value="#{o.opl_thema} "></h:outputText>
        </h:column>
    </h:dataTable>
</h:form>             

This displays everything very well. Like you can see I got the code part of the object set as a link. With this code I get an object out of the database, from which I display the details.

The problem here is that every link displays the details of the last object of the list instead of the current object where I click the link.

This is the function I call (which works):

public String getOpleidingByCode(String code) {
    this.opleiding = this.opleidingFacade.getOpleidingByCode(code);
    return "details";
}

I hope any of this makes any sense?

Sorry if the solution is super clear but I am new to all of this.

Thanks in advance! Kind regards Deflandrex

EDIT: This is the function that I use to call the first view (from which I provided the code)

public String getOpleidingenOpThema() {
    this.opleidingen = this.opleidingFacade.getOpleidingenOpThema();
    return "resultaat";
}

Both the functions are located in 1 controller.

Upvotes: 0

Views: 52

Answers (1)

OTM
OTM

Reputation: 656

What happens with the current code :

The outcome value is processed for each link element in the datatable sequentially by calling the opleidingController. getOpleidingByCode(o.opl_code). The backing bean method sets the this.opleiding value corresponding to the o.opl_code. Once all the links's outcome values are processed this.opleiding holds the value of the last o.opl_code. Hence, you are seeing last value from the list in the details page.

One Proposed solution:

The this.opleiding value should be set in the controller based on the o.opl_code when you process the details after clicking the link and not when the link is displayed and the outcome is set. You need to

  1. Set outcome="details" in the h:link tag as this value is static and does not change based on any logic in the backing bean and remove method binding.

  2. Pass the value of o.opl_code as a request parameter to the link as given below within the h:link tag.

    <f:param name="opl_code" value="#{o.opl_code}"/>

  3. Have the detail backing bean load the this.opleiding value based on the request parameter opl_code. This way you will have details for specific opl_code based on what link you click.

Upvotes: 2

Related Questions