JSF中的条件渲染 [英] Conditional rendering in JSF

查看:143
本文介绍了JSF中的条件渲染的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我有以下代码可以有条件地呈现页面中的组件:

Hello I have this code to conditionally render components in my page:

<h:commandButton action="#{Bean.method()}"  value="Submit">
   <f:ajax execute="something" render="one two" />
</h:commandButton>

<p><h:outputFormat rendered="#{Bean.answer=='one'}" id="one" value="#{messages.one}"/></p>
<p><h:outputFormat rendered="#{Bean.answer=='two'}" id="two" value="#{messages.two}"/></p>

它得到了答案并呈现了组件,但是为了在我的页面上看到它,我需要刷新页面.我该如何解决这个问题?有什么建议吗?

It gets the answer and renders the component but in order to see it on my page, I need to refresh the page. How can I fix this problem? Any suggestions?

推荐答案

JSF组件的rendered属性是服务器端设置,用于控制JSF是否应生成所需的HTML.

The JSF component's rendered attribute is a server-side setting which controls whether JSF should generate the desired HTML or not.

<f:ajax>标记的render属性应指向JSF生成的HTML元素的(相对)客户端ID,JavaScript可以通过document.getElementById()从HTML DOM树中获取该元素,以便在替换完所有内容后替换其内容. ajax请求.

The <f:ajax> tag's render attribute should point to a (relative) client ID of the JSF-generated HTML element which JavaScript can grab by document.getElementById() from HTML DOM tree in order to replace its contents on complete of the ajax request.

但是,由于您要指定JSF永远不会呈现的HTML元素的客户端ID(由于renderedfalse),因此JavaScript无法在HTML DOM树中找到它.

However, since you're specifying the client ID of a HTML element which is never rendered by JSF (due to rendered being false), JavaScript can't find it in the HTML DOM tree.

您需要将其包装在一个容器组件中,该组件始终显示 ,因此始终在HTML DOM树中可用.

You need to wrap it in a container component which is always rendered and thus always available in the HTML DOM tree.

<h:commandButton action="#{Bean.method()}"  value="Submit">
   <f:ajax execute="something" render="messages" />
</h:commandButton>

<p>
    <h:panelGroup id="messages">
        <h:outputFormat rendered="#{Bean.answer=='one'}" value="#{messages.one}"/>
        <h:outputFormat rendered="#{Bean.answer=='two'}" value="#{messages.two}"/>
    </h:panelGroup>
</p>


无关与具体问题无关,您可能存在设计错误.为什么不只是在action方法中创建一个使用所需消息设置的#{Bean.message}属性,以便可以使用:


Unrelated to the concrete problem, you've there a possible design mistake. Why would you not just create a #{Bean.message} property which you set with the desired message in the action method instead, so that you can just use:

<h:commandButton action="#{Bean.method()}"  value="Submit">
   <f:ajax execute="something" render="message" />
</h:commandButton>

<p>
    <h:outputFormat id="message" value="#{Bean.message}" />
</p>

这篇关于JSF中的条件渲染的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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