有没有办法在像bean这样的中心点注册组件的事件监听器?
这样的事情可能吗?
@PostConstruct
public void setup() {
FacesContext facesContext = FacesContext.getCurrentInstance();
UIViewRoot view = facesContext.getViewRoot();
view.getComponentByName("toolBar:save").addActionListener(com.sample.SaveListener);
view.getComponentByName("form:save").addActionListener(com.sample.SaveListener);
}
答案 0 :(得分:2)
并非所有组件都必须在bean(post)构造期间可用。只要EL首次需要解析#{bean}
,就会构建bean,这可能为时尚早。而是在预渲染视图事件期间执行此操作。
将以下标记添加到您的视图中:
<f:event type="preRenderView" listener="#{bean.setup}" />
然后你可以用这种方法做必要的工作:
public void setup() {
UIViewRoot view = FacesContext.getCurrentInstance().getViewRoot();
((UICommand) view.findComponent("toolBar:save")).addActionListener(new com.sample.SaveListener());
((UICommand) view.findComponent("form:save")).addActionListener(new com.sample.SaveListener());
}