1

嗨,伙计们,

在 JSF 2 中,如何使用 PhaseListener 更改 ah:InputText 组件的渲染属性。

在呈现 jsf 页面之前,我必须验证 h:inputtexts 的所有 id,然后我将更改要呈现或不呈现的属性。

我清楚了吗?

4

1 回答 1

1

在 GET 请求上,在呈现响应的前阶段尚未创建视图根,而在后阶段则为时已晚,因为响应已被呈现并发送到客户端。然而,视图根在“预渲染视图”系统事件期间可用于修改。

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;
    }

}

要让它运行,请在以下位置注册它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>
于 2011-10-01T12:00:25.323 回答