How to pass an argument to method from rendered h:outputText?

扶醉桌前 提交于 2019-12-08 04:26:15

问题


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:


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.



来源:https://stackoverflow.com/questions/9436088/how-to-pass-an-argument-to-method-from-rendered-houtputtext

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!