JSF2.0 使用 ui:define 进行部分渲染

发布于 2024-10-10 04:41:26 字数 1429 浏览 0 评论 0原文

我正在使用 Facelet 模板,并且我认为我遇到了 ui:define 和 JSF 生命周期的问题。我的 template.xhtml 在标题中包含一个固定菜单,经过简化,它具有如下链接:

<h:commandLink value="Click Me">
    <f:ajax update="#{myBean.listener}" render="contentpanel"/>
</h:commandLink>

template.xhtml 还包含一个 ui:insert 语句:

<ui:insert name="content">
    <h:outputLabel value="content placeholder"/>
</ui:insert>

现在我有一个 content.xhtml,它看起来像:

<ui:composition template="template.xhtml">
    <ui:define name="content">
        <h:panelGroup id="contentpanel"/>
    </ui:define>
</ui:composition>

介绍就这么多。当我单击命令链接“Click Me”时,我正在呼叫我的侦听器。该侦听器在 backingbean 中设置一个引用,以根据我单击的链接动态加载内容。

当我第一次按下命令链接时,渲染并未完成。嗯,基本上看起来它首先进行重新渲染,然后调用侦听器,而不是相反。所以当我第一次点击时,似乎什么也没有发生。当我第二次单击时,我看到为第一次单击设置的内容。当我第三次点击时,我看到第二次点击的内容。

我认为这是因为 ui:define 视图已经在 J​​SF 生命周期的“恢复视图”阶段重新构建。关于如何克服这个问题有什么想法吗?

更新

看来我的假设是错误的。造成这种情况的原因似乎有所不同。 #{myBean.listener} 有一个 @SessionScoped @ManagedProperty,它在单击 CommandLink 后更新。内容面板实际上是通过#{myBean.data}(即@RequestScoped)加载数据的。此 #{myBean.data} 未正确重新加载数据。我通过将 getData() 方法直接传递给 @SessionScoped bean 解决了这个问题。

可能有点混乱。但我的结论是:它确实可以部分渲染通过facelet模板加载的组件(ui:define / ui:insert

I'm using facelet templating and I think I'm running into an issue with ui:define and the JSF lifecycle. My template.xhtml contains a fixed menu in the header, simplified it has links like this:

<h:commandLink value="Click Me">
    <f:ajax update="#{myBean.listener}" render="contentpanel"/>
</h:commandLink>

The template.xhtml also contains a ui:insert statement:

<ui:insert name="content">
    <h:outputLabel value="content placeholder"/>
</ui:insert>

Now I have a content.xhtml which looks like:

<ui:composition template="template.xhtml">
    <ui:define name="content">
        <h:panelGroup id="contentpanel"/>
    </ui:define>
</ui:composition>

So much for the introduction. When I click the commandlink 'Click Me' I'm calling my listener. This listener sets a reference in a backingbean to dynamically load the content based on the link I clicked.

This rendering is not done the first time I press the commandlink. Well it basically looks like it first does the re-render and then call the listener instead of the other way around. So when I first click, nothing seems to happen. When I click for the second time I see the content that was set for the first click. When I click for the third time I see the content of the second click.

I think it's because the ui:define view is already re-built in the 'Restore View' phase of the JSF lifecycle. Any ideas on how to overcome this?

UPDATE

Seems like my assumption was wrong. The cause of this seems to be something different. The #{myBean.listener} has a @SessionScoped @ManagedProperty which is updated after the CommandLink is clicked. The contentpanel actually loads data via the #{myBean.data} which is @RequestScoped. This #{myBean.data} did not reload the data correctly. I solved it by passing the getData() method directly to the @SessionScoped bean.

Might be a bit confusing. But my conclusion: it does work to partial render a component which is loaded via facelet templating (ui:define / ui:insert)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

千纸鹤带着心事 2024-10-17 04:41:26

看来我的假设是错误的。造成这种情况的原因似乎有所不同。 #{myBean.listener} 有一个 @SessionScoped @ManagedProperty,它在单击 CommandLink 后更新。内容面板实际上通过#{myBean.data}(即@RequestScoped)加载数据。此 #{myBean.data} 未正确重新加载数据。我通过将 getData() 方法直接传递给 @SessionScoped bean 解决了这个问题。

可能有点混乱。但我的结论是:它确实可以部分渲染通过 Facelet 模板加载的组件(ui:define / ui:insert)

Seems like my assumption was wrong. The cause of this seems to be something different. The #{myBean.listener} has a @SessionScoped @ManagedProperty which is updated after the CommandLink is clicked. The contentpanel actually loads data via the #{myBean.data} which is @RequestScoped. This #{myBean.data} did not reload the data correctly. I solved it by passing the getData() method directly to the @SessionScoped bean.

Might be a bit confusing. But my conclusion: it does work to partial render a component which is loaded via facelet templating (ui:define / ui:insert)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文