最早在完全构建的组件树上访问visitTree()的时间吗? [英] The earliest moment to visitTree() on fully built Component Tree?

查看:103
本文介绍了最早在完全构建的组件树上访问visitTree()的时间吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

早上好,JSF专家!

让我想出我要解决的问题.该应用程序具有多种形式.并且可以根据1)权限集及其为此表单元素配置的条件组合,以及2)从db加载的用户权限,标记表单中的元素以要求用户授权.

Let me fugure out the problem I'm trying to solve. The application has many forms. And elements in forms can be marked to require authorization of users according to 1) set of rights and their conditional combinations configured for this form element and 2) user rights loaded from db.

我要开发的解决方案如下:我实现了自定义组件,该组件扩展了UIComponentBase.在表单的Facelets标记中,此自定义组件将在授权下包装元素:

Solution I'm trying to develop is as follows: I implement custom component, which extends UIComponentBase. In the form's Facelets' markup this custom component would wrap elements under authorization:

<custom:applyRights id="abc">
    <p:inputText
        id="inputWithRights"
        value="Some placeholder..."
        tabindex="0"/>
</custom:applyRights>

然后,我需要修改组件树. IE.在构建组件树之后,我需要找到我的<custom:applyRights/>,访问其子树并保留组件不变,或者禁用它们,或者设置setRendered( false )等.对组件采取的具体操作还取决于组件的类型.

Then I need to modify component tree. I.e. just after the component tree is built I need to find my <custom:applyRights/>, visit its subtree and either leave components as is, or disable them, or set setRendered( false ) etc. The concrete action taken upon a component also depends on the component's type.

然后我将PhaseListener设置为:

@Override
public PhaseId getPhaseId() {
    return PhaseId.RESTORE_VIEW;
}

在其afterPhase(PhaseEvent phaseEvent)中,我得到FacesContextUIViewRoot的当前实例,构建new FullVisitContext(facesContext)并尝试viewRoot.visitTree.

In its afterPhase(PhaseEvent phaseEvent) I get current instance of FacesContext, UIViewRoot, build new FullVisitContext(facesContext) and attempt viewRoot.visitTree.

但是只有viewRoot被访问.

But then only viewRoot gets visited.

我做错了什么?也许我试图过早visitTree()?那什么时候应该做?

What am I doing wrong? Maybe I'm trying to visitTree() too early? Then when should it be done?

谢谢!

推荐答案

这是PostAddToViewEvent > UIViewRoot 本身.您可以使用 SystemEventListener 实施将其挂钩,如下所示:

That's the PostAddToViewEvent of the UIViewRoot itself. You can hook into it using a SystemEventListener implementation like below:

public class YourSystemEventListener implements SystemEventListener {

    @Override
    public boolean isListenerForSource(Object source) {
        return (source instanceof UIViewRoot);
    }

    @Override
    public void processEvent(SystemEvent event) throws AbortProcessingException {
        UIViewRoot view = (UIViewRoot) event.getSource();
        // ...
    }

}

已在faces-config.xml<application>中进行注册,如下所示:

Which is registered in <application> of faces-config.xml like below:

<system-event-listener>
    <system-event-listener-class>com.example.YourSystemEventListener</system-event-listener-class>
    <system-event-class>javax.faces.event.PostAddToViewEvent</system-event-class>
    <source-class>javax.faces.component.UIViewRoot</source-class>
</system-event-listener>

这篇关于最早在完全构建的组件树上访问visitTree()的时间吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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