如何从渲染的h:outputText向方法传递参数? [英] How to pass an argument to method from rendered h:outputText?

查看:317
本文介绍了如何从渲染的h:outputText向方法传递参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在显示sql查询中的数据表,并希望根据此sql查询中的一个字段值呈现代码段。

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.

查看:records.xthml

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>

控制器:ViewListBean.java

Controller: ViewListBean.java

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

有问题的两行record.xhtml代码是:

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)}" />

我希望能够在渲染检查中传递一个参数并返回一个布尔值来渲染或不渲染。假设在这个sql查询中返回了20条记录。如果当前行的 recordId 值小于或等于10,则它将返回true并且 listedRecord.dateAdded 将显示字段。否则它将返回false并显示 Archived 这个词。

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.

这是传递参数的正确方法吗从JSF生成的XHTML页面到控制bean的方法?

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?

推荐答案

你只有一个错误:必须进入EL表达式。

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

即这是无效的:

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

它应该是:

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

for the remnant it假设您的环境支持EL 2.2,它看起来应该可以正常工作。我只使用< h:dataTable> ,因为它消除了HTML样板。

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.

这篇关于如何从渲染的h:outputText向方法传递参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆