0

I have a search screen where I search for a customer id and it consumes a webservice a returns list of objects. I display the results in a datatable.For a specific field , I have a method which provides the value based on a key value in each row of the list being iterated. The key value is productID. I set that in a bean named output. In the getCustomerValue method I call the method which provides the relevant value by passing the value of "productID". I use the below listed code to do the same.

<h:outputText id="customerID" binding="#{myBean.output}" value="#{customerBean.customervalue}">
<f:attribute name="myID" value="#{item2.customerService.productID}"/>
</h:outputText> 

The value gets displayed properly when the page is loaded. I have hyperlink in the same page which basically calls the same webservice and renders the same page.But this time around all the values except the value listed above is being displayed. When I print the value of attribute "item2.customerService.productID" in the method "customervalue" , it is displayed as null.I am not sure why this value isn't being passed.

Punter Vicky
  • 15,954
  • 56
  • 188
  • 315

1 Answers1

1

You're displaying this in a <h:dataTable>. The <f:attribute> is specific to the component itself, not to its generated HTML output. The <f:attribute> is evaluated during view build time, not during the view render time. At the moment JSF builds the view, the #{item2} is not present in the scope. It's only present in the scope when JSF renders the view.

You need to look for the solution by alternate means. It's unclear what JSF version you're using, but based on your question you're using JSF 1.2 (in the future questions, please explicitly mention the JSF impl/version you're using; in JSF 2.0 a lot of things can be done differently and much more elegantly).

My answer on your previous question of Passing parameters to a method in h:outputtext tag should be the best answer to your current problem. This is apparently not an option somehow. In that case, there are at least 3 alternative ways:

  1. Move the property to the class behind #{item2}:

    <h:outputText value="#{item2.customervalue}">
    

    You've in there instant access to the customerservice property.

  2. Get the current item inside the getter by evaluating EL programmatically:

    public String getCustomervalue() {
        FacesContext context = FacesContext.getCurrentInstance();
        Long productID = context.getApplication().evaluateExpressionGet(context, "#{item2.customerService.productID}", Long.class);
        // ...
    }
    

    (I assume that productID is a Long)

  3. Bind the datatable's value to a DataModel:

    private DataModel<Item2> items;
    

    with

    <h:dataTable value="#{customerBean.items}" var="item2">
    

    and

    public String getCustomervalue() {
        Item2 item2 = items.getRowData();
        // ...
    }
    
Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thanks once again BalusC for a quick / detailed and useful answer. I will try these. One of the things that I am confused is that the value is getting displayed each alternate time I click the link. – Punter Vicky Dec 12 '11 at 16:52
  • I am amazed at how you can co-relate all my posts!! you help so many people - how can you even manage to keep track of these :) – Punter Vicky Dec 12 '11 at 17:03
  • 1
    I am a moron - I did something wrong while implementing your previous suggestion and thought my container doesn't support it :( I tried your previous suggestion again.This works great - – Punter Vicky Dec 12 '11 at 17:08
  • Your username links to [your profile](http://stackoverflow.com/users/1042646/user1042646) where you can find all information about previous questions. This is the first what I do when someone doesn't tell the exact JSF version :) – BalusC Dec 12 '11 at 17:09
  • Sorry BalusC - I will definitely mention the version going forward. Thanks a ton. – Punter Vicky Dec 12 '11 at 17:11