问题
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