1

I am displaying a table of data from an sql query and want to render a section of code based on one of the field values from this sql query.

View: records.xthml

<table>
  <thead>
    <tr>
      <td>#{messages['table.header.id']}</td>
      <td>#{messages['table.header.name']}</td>
      <td>#{messages['table.header.date.added']}</td>
      <td>&nbsp;</td>
    </tr>
  </thead>
  <tbody>
    <a4j:repeat value="recordListBean.records" var="listedRecord" rowKeyVar="index">
      <tr>
        <td><h:outputText value="#{listedRecord.id}</td>
        <td><h:outputText value="#{listedRecord.name}</td>
        <td>
          <h:outputText value="#{listedRecord.dateAdded}" rendered="#{viewRecordBean.currentRecord(listedRecord.id)}" />
          <h:outputText value="#{messages['table.header.record.archived']}" rendered="!#{viewRecordBean.currentRecord(listedRecord.id)}" />
        </td>
      </tr>
    </a4j:repeat>
  </tbody>
</table>

Controller: ViewListBean.java

public boolean currentRecord(Long recordId) {
  Long maxRecordId = 10;
  if (recordId <= maxRecordId) {
    return true;
  } else {
    return false;
  }
}

The two rows of records.xhtml code in question are:

<h:outputText value="#{listedRecord.candidate}" rendered="#{viewRecordBean.currentRecord(listedRecord.id)}" />
<h:outputText value="#{messages['table.header.record.archived']}" rendered="#{!viewRecordBean.currentRecord(listedRecord.id)}" />

I want to be able to pass an argument within the rendered check and return a boolean to render or not. Let's say that there are 20 records returned in this sql query. If the recordId value of the current row is less than or equal to 10, it will return true and the listedRecord.dateAdded field will be displayed. Otherwise it will return false and the word Archived will be displayed.

Is this the correct way to pass an argument from a JSF generated XHTML page to the controlling bean's method?

Is there a better or more efficient way of doing this?

DrewShirts
  • 147
  • 1
  • 6
  • 19
  • If I were you, I would do something like that: `value="#{viewRecordBean.currentRecord(listedRecord.id) ? listedRecord.candidate : messages['table.header.record.archived']}" `. Instead of two lines you have only one and you check the condition only one time. – Piotr Sagalara Apr 12 '17 at 07:45

1 Answers1

1

You've only one mistake: the ! has to go inside the EL expression.

I.e. this is invalid:

rendered="!#{viewRecordBean.currentRecord(listedRecord.id)}" 

it should be:

rendered="#{!viewRecordBean.currentRecord(listedRecord.id)}" 

For the remnant it look as it should work just fine, assuming that your environment supports EL 2.2. I'd only use a <h:dataTable> as that eliminates HTML boilerplate.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thanks for the quick response!!I'm quite the newbie to JSF/Java/EL, etc. How do I tell what EL my stack supports? – DrewShirts Feb 24 '12 at 18:54
  • Depends on server and web.xml version. See also http://stackoverflow.com/questions/5273729/how-to-call-a-method-with-a-parameter-in-jsf – BalusC Feb 24 '12 at 18:56
  • It appears I am using jboss-el-2.0.1. with Mojarra. – DrewShirts Feb 24 '12 at 20:26
  • For now, I need to keep the HTML boilerplate, so I still need to use the `a4j:repeat` option. When I do this, the `listedRecord.id` refuses to come through to the `viewRecordBean.currentRecord(listedRecord.id)` method as anything but null. I'm not sure how to get the actual value to come through. – DrewShirts Feb 27 '12 at 17:34
  • My mistake... this is actually working. I've now been able to verify that the parameter value is actually being passed through correctly. Thanks again for your help!! – DrewShirts Feb 27 '12 at 19:02