How to disable elements from within a ViewHandler after jsf has embedded the composite component?

喜欢而已 提交于 2020-01-11 12:32:35

问题


I'm using a ViewHandler to block all input elements on any accessed page, if certain criteria is met.

This works great for the input elements in the 'primary' xhtml files, but the input elements within composite components aren't being blocked. I figured it has to do with the fact that JSF embeds these components only after my ViewHandler has finished it's job.

Does anyone have an idea of how I can disable the elements in the composite as well?


回答1:


A ViewHandler is the wrong tool for the job. It's intented to create, build and restore views and to generate URLs for usage in JSF forms and links. It's not intented to manipulate components in a view.

For your particular functional requirement, a SystemEventListener on PostAddToViewEvent is likely the best suit. I just did a quick test, it works for me on inputs in composites as well.

public class MyPostAddtoViewEventListener implements SystemEventListener {

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

    @Override
    public void processEvent(SystemEvent event) throws AbortProcessingException {
        UIInput input = (UIInput) event.getSource();

        if (true) { // Do your check here.
            input.getAttributes().put("disabled", true);
        }
    }

}

To get it to run, register it as follows inside <application> of faces-config.xml:

<system-event-listener>
    <system-event-listener-class>com.example.MyPostAddtoViewEventListener</system-event-listener-class>
    <system-event-class>javax.faces.event.PostAddToViewEvent</system-event-class>
</system-event-listener>


来源:https://stackoverflow.com/questions/15029135/how-to-disable-elements-from-within-a-viewhandler-after-jsf-has-embedded-the-com

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