【问题标题】:JSF 2.2 Table conditional rendering not workingJSF 2.2 表格条件渲染不起作用
【发布时间】:2014-07-18 00:00:59
【问题描述】:

我正在尝试使用以下标签库显示带有@Entity 信息的简单表格:

xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:c="http://xmlns.jcp.org/jsp/jstl/core"

这是映射的实体属性,一个布尔值:

@NotNull
@Column(name = "ACTIVE", columnDefinition = "BIT", length = 1)
private boolean active;

//... getters and setters
public boolean getActive() {
    return active;
}

public boolean isActive() {
    return active;
}

public void setActive(boolean active) {
    this.active= active;
}

ps.:通过调试 backing bean,我得到了想要的列表。

这是我的 .xhtml(jsf 页面)sn-p:

<ui:repeat var="u" value="#{usersList}">
            <tr>
                <td>#{u.name}</td>
                <td>#{u.login}</td>
                <td>#{u.email}</td>
                <td>
                    <c:choose>
                        <c:when test="${u.active}">
                            <span style="color: green" class="glyphicon glyphicon-ok"></span>
                        </c:when>
                        <c:otherwise>
                            <span style="color: red"
                                                            class="glyphicon glyphicon-remove"></span>
                        </c:otherwise>
                    </c:choose>
                </td>
            </tr>
        </c:when>
</ui:repeat>

尽管列表不为空,但显示的是空消息,并且没有显示任何行。

如果我强制显示行,删除条件渲染,我会收到以下错误:

java.lang.IllegalArgumentException: Cannot convert 1 of type class java.lang.Long to class java.lang.Boolean

对于属性active

在询问之前我已经尝试了一些解决方案,例如:

How does Java expression language resolve boolean attributes? (in JSF 1.2)

javax.el.PropertyNotFoundException when trying to resolve Boolean properties in EL

更新 1:查看我的代码,我意识到我是 empty 测试一个对象,而不是一个列表,同时更新我的​​ sn-p,所以,条件行现在显示正常,但 active 属性问题,仍然抛出错误。

【问题讨论】:

  • 它抛出了同样的异常,还尝试将数据类型更改为字符串,显示 true,使用 EL #{u.active},但使用 #{u.active == 'true'} 作为条件,不返回 true,至少,它不会抛出任何错误。
  • jsf sn-p:&lt;td&gt;#{u.active} | #{u.active == 'true'} | &lt;c:if test="${u.active == 'true'}"&gt; active &lt;/c:if&gt;&lt;/td&gt; 显示:true | true |

标签: jsf-2.2 glassfish-4 java-ee-7


【解决方案1】:

问题在于 JSF 标记(例如 ui:repeat)在与 JSTL 标记(例如 c:when)不同的(稍后)阶段进行评估。因此,在评估 c:when 语句时,您的用户列表的值不可用。有时这会导致奇怪的行为,因此看起来这些值是可用的。请查看this answer 了解详情。

你应该使用一些JSF组件的rendered属性:

<c:choose>
  <c:when test="${u.active}">
     <span style="color: green" class="glyphicon glyphicon-ok"></span>
  </c:when>
  <c:otherwise>
    <span style="color: red" class="glyphicon glyphicon-remove"></span>
  </c:otherwise>
</c:choose>

变成:

<h:panelGroup rendered="#{u.active}">
  <span style="color: green" class="glyphicon glyphicon-ok"></span>
</h:panelGroup>
<h:panelGroup rendered="#{not u.active}">
  <span style="color: red" class="glyphicon glyphicon-remove"></span>
</h:panelGroup>

您也可以将它用于如下值:

<h:outputText rendered="#{u.active}" value="#{u.name}" />

另请参阅:

【讨论】:

  • 完美!根据最近的研究考虑使用渲染,但由于我很长时间没有使用 jsf,我仍然习惯于新的东西。感谢您的出色回答和参考!
猜你喜欢
  • 2014-01-17
  • 1970-01-01
  • 1970-01-01
  • 2012-11-28
  • 1970-01-01
  • 2022-09-27
  • 2014-08-07
  • 1970-01-01
  • 2017-09-10
相关资源
最近更新 更多