How to pass an argument to method from rendered h:

2019-05-30 15:33发布

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?

1条回答
来,给爷笑一个
2楼-- · 2019-05-30 15:40

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.

查看更多
登录 后发表回答