简体   繁体   English

f:ajax默认为false

[英]f:ajax rendered=false by default

Suppose I've got this small piece of code: 假设我有这小段代码:

<h:panelGroup id="panel1" rendered="falseByDefault">
    <h:selectBooleanCheckbox value="#{bean.booleanProperty}">
        <f:ajax event="click" render="alwaysrendered">
    </h:selectBooleanCheckbox>
</h:panelGroup>

<h:panelGroup id="alwaysrendered">
    <h:panelGroup id="panel2" rendered="#{!bean.booleanProperty}">
        <p>Some stuff here</p>
    </h:panelGroup>
</h:panelGroup>

panel1 is not rendered until I press another component. 在我按下另一个组件之前,panel1不会渲染。 booleanProperty is true by default until I press the booleanCheckBox, so the panel2 should appear in that moment because of the ajax request to re-render alwaysrendered. 在我按下booleanCheckBox之前,默认情况下booleanProperty才为true,因此,由于ajax请求重新渲染alwaysrendered,panel2应该在那一刻出现。

It doesn't work. 没用 I think because the "f:ajax" tag is inside a component that is not rendered when the page loads. 我认为是因为“ f:ajax”标记位于页面加载时未呈现的组件内。

What do you think about this? 你怎么看待这件事? Do you know any other way to make this work? 您知道其他方法可以完成这项工作吗?

Summary: I want to render a component with a tag that is inside a panel not rendered by default. 简介:我想使用面板内默认未渲染的标签来渲染组件。

Thanks! 谢谢!

It doesn't work. 没用 I think because the tag is inside a component that is not rendered when the page loads. 我认为是因为标记位于页面加载时未呈现的组件内。

That's exactly the problem. 这就是问题所在。 JSF ajax update work by replacing the elements in the DOM-Tree of the browser with the new version. 通过使用新版本替换浏览器的DOM-Tree中的元素,JSF ajax更新工作。 But if there is no such element with the ID you just re-rendered, because it wasn't rendered before, then the browser doesn't know where to put that updated element and ignores it. 但是,如果没有这样的元素,而您刚刚重新渲染过的ID(因为它之前未渲染过),则浏览器将不知道将更新后的元素放在何处,并会忽略它。

Try something like this instead: 尝试这样的事情:

<h:panelGroup id="panel2" >
    <h:panelGroup rendered="#{!bean.booleanProperty}">
        <p>Some stuff here</p>
    </h:panelGroup>
</h:panelGroup>

You also need to set your bean to at least @ViewScoped . 您还需要将bean至少设置为@ViewScoped This is because it works like this: 这是因为它的工作方式如下:

Initially your first panel is not visible because of " rendered="falseByDefault" ". 最初,您的第一个面板不可见,原因是“ rendered="falseByDefault" “。 Then you click something else and that condition becomes true making your panel visible, but only for that one request+response. 然后,您单击其他内容,该条件变为true使您的面板可见,但仅针对该一个请求+响应。 Now you click on the checkbox in that visible panel and want to set #{bean.booleanProperty} to another value in order to show panel2. 现在,您单击该可见面板中的复选框,并想要将#{bean.booleanProperty}设置为另一个值以显示panel2。 The request will be sent, but JSF performs some validity checks on that request. 该请求将被发送,但是JSF对该请求执行一些有效性检查。 It also checks that the checkbox you just clicked, could be clicked (that means it was visible). 它还检查是否可以单击您刚刚单击的复选框(这意味着它是可见的)。 But because the panel around that checkbox is not visible anymore (it was visible only for one request), JSF decides that this request is not valid and does not process it the way you want it. 但是,由于该复选框周围的面板不再可见(仅对一个请求可见),因此JSF决定此请求无效,并且不会按照您希望的方式对其进行处理。 That's why the boolean value of your checkbox is never changed and your panel2 will not be visible. 这就是为什么复选框的布尔值永远不会更改并且panel2将不可见的原因。

As a rule of thumb: never use @RequestScoped together with Ajax requests. 根据经验:切勿将@RequestScoped与Ajax请求一起使用。

You need to put a wrapping tag with an id around the panelGroup "pane2" and then reference that one in the ajax render attribure. 您需要在panelGroup“ pane2”周围放置一个带有ID的包装标签,然后在ajax渲染属性中引用该包装标签。 This is needed because javascript tries to update the components by id. 这是必需的,因为javascript会尝试通过id更新组件。 But when it is not rendered and therefore not found no update will occur. 但是,如果未呈现它,因此找不到它,则不会进行更新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM