JSF 2: Change rendered atribute of a component on

2019-07-23 20:08发布

问题:

Hy guys,

In JSF 2 How can I change the rendered atribute of a h:InputText component using a PhaseListener.

Before the jsf page be rendered I have to verify all id of the h:inputtexts, and after that I will change the atribute to be rendered or not.

Am I clear?

回答1:

On GET requests, the view root is not created yet during the before phase of the render response and during the after phase it's too late because the response is already been rendered and sent to the client. The view root is however available for modification during the "pre render view" system event.

public class PreRenderViewListener implements SystemEventListener {

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

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

}

To get it to run, register it as follows in faces-config.xml:

<application>
    <system-event-listener>
        <system-event-listener-class>com.example.PreRenderViewListener</system-event-listener-class>
        <system-event-class>javax.faces.event.PreRenderViewEvent</system-event-class>
    </system-event-listener>
</application>