【发布时间】:2012-09-01 15:06:44
【问题描述】:
我的 JSF 渲染有问题。表达式语言中的给定条件不会以正确的方式执行。 例如:
示例 1
<f:param name="cat" value="#{product.category.uri}" rendered="#{product.category.parent.uri == null}" />
<f:param name="cat" value="#{product.category.parent.uri}" rendered="#{product.category.parent.uri != null}" />
示例 2
<c:if test="#{product.category.parent.uri == null}">
<f:param name="cat" value="#{product.category.uri}" />
</c:if>
<c:if test="#{product.category.parent.uri != null}">
<f:param name="cat" value="#{product.category.parent.uri}" />
</c:if>
问题
在这两个示例中,我的两个参数都将添加到我周围的 h:outputLink。 我不确定要添加什么其他代码,所以如果你们需要其他任何东西来帮助我,我很乐意提供。
提前致谢。
示例 3(根据要求)
<?xml version='1.0' encoding='UTF-8' ?>
<ui:composition template="./WEB-INF/templates/base.xhtml"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:c="http://java.sun.com/jsp/jstl/core">
<ui:define name="content">
<c:choose>
<c:when test="#{webshop.productlist.size() > 0}">
<div id="spacer">
<ui:repeat value="#{webshop.productlist}" var="product">
<div id="block">
<p>
<h:outputLink value="product.xhtml">
#{product.name}
<c:choose>
<c:when test="#{product.category.parent.uri == null}">
<f:param name="cat" value="#{product.category.uri}" rendered="" />
</c:when>
<c:otherwise>
<f:param name="cat" value="#{product.category.parent.uri}" />
</c:otherwise>
</c:choose>
<f:param name="product" value="#{product.uri}" />
</h:outputLink>
</p>
</div>
</ui:repeat>
</div>
</c:when>
<c:otherwise>
(...)
</c:otherwise>
</c:choose>
</ui:define>
</ui:composition>
我已经对这个示例进行了一些清理,但精髓就在那里。 我已经用 when/otherwise 构造替换了第一个示例,无论我的 product.category.parent.uri 是否为空,在这种情况下它都会给我第一个结果。
【问题讨论】:
-
不,给定的 .uri 实际上是空的。
-
其实你应该调试一下为什么产品不在范围内。
-
为什么你的EL有“#”号?不应该是“$”吗?
-
您可能正在做某事。 $ 语句将立即呈现,而 # 语句将在需要时呈现。鉴于我的产品实例正在 ui:repeat 中使用,$-statement 可能会解决问题。我会在测试后立即报告。
-
示例 1 中带有 $ 注释的 EL 语句将导致相同的问题,即两个名为“cat”的参数。
标签: java jsf rendering el conditional-statements