JSF:在后备Bean中处理UI驱动的事件之后,是否尊重组件绑定方法? [英] JSF: Respecting a component binding method after UI-driven events are processed in backing beans?

查看:58
本文介绍了JSF:在后备Bean中处理UI驱动的事件之后,是否尊重组件绑定方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最后,当根据外部源定义数据表结构时,我已经设法在JSF中使用类似 DOM 的方法构建真正的动态表. (具有动态列的数据表正是我所需要的)基本上,以下代码对我来说足够了:

Finally I've managed how to build a true dynamic table with the DOM-like approach in JSF when the data table structure is defined according to an external source. ( DataTable with dynamic columns exactly what I need) Basically, the following code is quite enough for me:

<p:layoutUnit position="west">
    <!-- 2 -->
    <p:tree value="#{tableViewsPageBean.root}" var="node" dynamic="true" cache="false"
                                        selectionMode="single" selection="#{tableViewsPageBean.selectedNode}">
        <!-- 3 -->
        <p:ajax event="select" listener="#{tableViewsPageBean.onNodeSelect}"/>
        <p:treeNode id="treeNode">
            <h:outputText value="#{node}"/>
        </p:treeNode>
    </p:tree>
</p:layoutUnit>
<p:layoutUnit position="center">
    <h:panelGroup>
        <h:panelGroup rendered="#{tableViewsPageBean.isRegeneratable}">
            <p:commandButton value="#{fu:$(languageBean, 'REGENERATE')}"
                             action="#{dynamicTableViewBean.regenerate}"/>
        </h:panelGroup>
        <!-- 1 -->
        <h:panelGroup binding="#{dynamicTableViewBean.dataTableGroup}"/> 
    </h:panelGroup>
</p:layoutUnit>

但是有一个问题,我目前不知道如何解决.如果您看一下上面的代码,则可以在注释中看到数字标记,这些数字标记表示支持bean方法的流程:

But there is a problem I currently do not know how to solve. If you take a look at the code above, you can see the numeric markers in the comments that represent the flow of the backing beans methods:

  1. binding="#{dynamicTableViewBean.dataTableGroup}"
  2. selection="#{tableViewsPageBean.selectedNode}"
  3. listener="#{tableViewsPageBean.onNodeSelect}"
  1. binding="#{dynamicTableViewBean.dataTableGroup}"
  2. selection="#{tableViewsPageBean.selectedNode}"
  3. listener="#{tableViewsPageBean.onNodeSelect}"

此顺序对我不利,因为h:panelGroup的绑定必须在用户在树形视图中更改选择后定义.简而言之,最好的流程是:

This order is not good for me, because the binding of the h:panelGroup must be defined after a user changed the selection in the tree view. Simply saying, the very best flow would be:

  1. selection="#{tableViewsPageBean.selectedNode}"(以前为2)
  2. listener="#{tableViewsPageBean.onNodeSelect}"(以前为3)
  3. binding="#{dynamicTableViewBean.dataTableGroup}"(以前为1)
  1. selection="#{tableViewsPageBean.selectedNode}" (previously 2)
  2. listener="#{tableViewsPageBean.onNodeSelect}" (previously 3)
  3. binding="#{dynamicTableViewBean.dataTableGroup}" (previously 1)

所以我可以建立一个尊重用户在树中选择的数据表组件.那么,这是一种强制执行上述方法的另一顺序的方法吗?使用的PrimeFaces版本:3.2

so I could build a data table component respecting the user selection in the tree. So is it a way to force another order of executing the methods above? PrimeFaces version used: 3.2

先谢谢了.非常感谢您的帮助.

Thanks in advance. Your help is appreciated a lot.

推荐答案

在JSP上的旧JSF 1.x中,这种方式起作用,但在Facelets上的JSF 2.x中,此方式不再起作用.但是调用顺序应该无关紧要.只需将binding getter/setter设置为普通的getter/setter,并在listener方法中操纵dataTableGroup即可.毕竟是相同的对象引用.

It worked that way in old JSF 1.x on JSP, but not anymore in JSF 2.x on Facelets. But the invocation order should not matter. Just make the binding getter/setter a normal getter/setter and manipulate the dataTableGroup inside the listener method instead. It's after all the same object reference.

public void onNodeSelect(NodeSelectEvent event) {
    dataTableGroup.getChildren().add(...);
    // ...
}

您只需要将update属性添加到<p:ajax>,以便在ajax请求完成时真正更新绑定的面板组或其父级之一,否则您将在视图中看不到任何变化.

You only need to add the update attribute to <p:ajax> in order to really update the bound panelgroup or one of its parents when the ajax request completes, otherwise you will effectively see no change in the view.

例如

<h:form id="formId">
    ....
    <p:ajax event="select" listener="#{tableViewsPageBean.onNodeSelect}" update=":formId:groupId" />
    ...
    <h:panelGroup id="groupId" binding="#{dynamicTableViewBean.dataTableGroup}"/> 
    ...
</h:form>

顺便说一句,您是否了解PrimeFaces的 <p:columns>组件 ?如果您只需要动态列,则可能是一个更好的解决方案.在黑暗的JSF 1.x时代,该解决方案就没有任何UIComponent的味道,这就是为什么应用了笨拙的binding解决方法的原因.

By the way, are you aware of PrimeFaces' <p:columns> component? It may be a better solution if all you want is dynamic columns. That solution didn't exist in flavor of any UIComponent back in the dark JSF 1.x ages, that's why the awkward binding workaround was been applied.

这篇关于JSF:在后备Bean中处理UI驱动的事件之后,是否尊重组件绑定方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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