本文介绍了有条件地渲染< h:commandButton>使用< c:if>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用jstl进行小型测试。并且它不起作用它应该如何工作

I am using jstl for small testing. And it is not working how it is supposed to work

这里是小代码:

<c:set var="id" value="#{mBlog.blog.id}"/>
                    ${id}    //printing  4
                    <c:if test="${id > 0}">
                        <h:commandButton value="Update" action="#{mBlog.update}"/>  //is not rendered
                    </c:if>

                    <c:if test="${id == 0}">
                        <h:commandButton value="Save" action="#{mBlog.save}"/>  //is not rendered
                    </c:if>

我不知道有什么问题。在显示器中我只看到4,没有别的。

I do not know what is wrong. in the display i see only 4, nothing else.

推荐答案

JSTL标签和JSF UI组件不会同步运行期待编码。长话短说:阅读本文小心。

JSTL tags and JSF UI components doesn't run in sync as you'd expect from the coding. Long story short: JSTL in JSF2 Facelets... makes sense? Read this carefully.

在您的特定情况下,JSTL < c:if> 标记条件似乎取决于JSF < f:event> 标记的结果。目前< c:if> 运行时,在视图构建期间,< f:event> 尚未运行,因为它在预渲染视图事件期间运行。因此#{mBlog.blog.id} 始终是 null 或任何其他默认值。

In your particular case, the JSTL <c:if> tag condition seems to be dependent on the result of the JSF <f:event> tag. At the moment the <c:if> runs, during the view build time, the <f:event> hasn't run yet, because it runs during pre render view event. Hence the #{mBlog.blog.id} is always null or any other default.

您需要使用JSF组件的呈现的属性。它还可以使代码更清晰。

You need to use JSF component's rendered attribute instead. It also keeps your code cleaner.

<h:commandButton value="Update" action="#{mBlog.update}" rendered="#{mBlog.blog.id gt 0}" />
<h:commandButton value="Save" action="#{mBlog.save}" rendered="#{mBlog.blog.id eq 0}" />

然而,在调用操作时,您可能会遇到另一个潜在的未来问题。确保将bean放在视图范围内,而不是放在请求范围内。

You've however another potential future problem when invoking the action. Make sure that the bean is put in view scope, not in the request scope.

这篇关于有条件地渲染&lt; h:commandButton&gt;使用&lt; c:if&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 09:13