The earliest moment to visitTree() on fully built Component Tree?

落花浮王杯 提交于 2019-12-02 06:53:57

问题


Good morning, JSF experts!

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.

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>

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.

Then I imlement PhaseListener with:

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

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

But then only viewRoot gets visited.

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

Thanks!


回答1:


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();
        // ...
    }

}

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>


来源:https://stackoverflow.com/questions/29691625/the-earliest-moment-to-visittree-on-fully-built-component-tree

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!