gpt4 book ai didi

jsf - 如何为所有 UIInput 注册(组件)SystemEventListener

转载 作者:行者123 更新时间:2023-12-01 02:30:00 26 4
gpt4 key购买 nike

我正在尝试自定义 SystemEventListener为所有类型的实例注册 UIInput并对他们的 postValidate 使用react- 事件。根据我在网上找到的一个例子,我设法让一个运行 HtmlInputText通过在 faces-config.xml 中注册它如下:

<system-event-listener>
<source-class>javax.faces.component.html.HtmlInputText</source-class>
<system-event-class>javax.faces.event.PostValidateEvent</system-event-class>
<system-event-listener-class>com.ourcompany.ourproduct.validators.inputPostValidationListener</system-event-listener-class>
</system-event-listener>

然后我尝试 1) 将其扩展到一般的 UIInputs 和 2) 使用 @ListenerFor注释,但我似乎无法让其中任何一个工作。

对于 1) 我真的找不到任何示例或文档,所以我只是尝试了 a) 定义多个源类标签或 b) 使用 javax.faces.component.UIInput作为源类。都没有工作。

2)我试过
@ListenerFor(systemEventClass = PostValidateEvent.class, sourceClass = UIInput.class)

它既不适用于 UIInput,也不适用于 html.HtmlInputText。

现在,当我为所有其他类型的 HTML 输入复制相同的 XML 配置时,这确实有效,但它只会使 xml 变得困惑,而且对我来说似乎很烦人。

所以问题是:我通常在@ListenerFor 注释上做错了什么吗?对哪些源类可能有限制,即为什么我不能使用更通用的 UIInput?有没有比重复 XML 更有效的方法来为所有这些不同的输入注册监听器?最后:我宁愿实现 ComponentSystemEventListener .假设上述问题已解决,我只需更改 implements -声明并实现抽象 processEvent相应地,对吗?在这种情况下,它会起作用还是注册/xml-config 不同(例如,可能是 <component-system-event-listener> 而不仅仅是 <system-event-listener>

(以及作为后记:是只有我还是很难在网上找到此类东西的任何非平凡示例?)

最佳答案

@ListenerFor应该设置在 UIComponent 上或 Renderer实现,而不是独立的 SystemEventListener执行。另见 javadoc (强调我的):

The default implementation must support attaching this annotation to UIComponent or Renderer classes. In both cases, the annotation processing described herein must commence during the implementation of any variant of Application.createComponent() and must complete before the UIComponent instance is returned from createComponent(). The annotation processing must proceed according to an algorithm semantically equivalent to the following.

...


为了拥有一个全局监听器,而不是特定于 UIComponentRenderer ,最好的办法是创建和注册 PhaseListener它将监听器订阅到 View 根。
public class PostValidateListener implements PhaseListener {

@Override
public PhaseId getPhaseId() {
return PhaseId.PROCESS_VALIDATIONS;
}

@Override
public void beforePhase(PhaseEvent event) {
event.getFacesContext().getViewRoot()
.subscribeToViewEvent(PostValidateEvent.class, new InputPostValidationListener()); // Capitalize class name?
}

@Override
public void afterPhase(PhaseEvent event) {
// NOOP.
}

}
要使其运行,请在 faces-config.xml 中按如下方式注册它:
<lifecycle>
<phase-listener>com.example.PostValidateListener</phase-listener>
</lifecycle>
您甚至可以制作您的 InputPostValidationListener本身是 PhaseListener .
public class InputPostValidationListener implements PhaseListener, SystemEventListener {

@Override
public void beforePhase(PhaseEvent event) {
event.getFacesContext().getViewRoot().subscribeToViewEvent(PostValidateEvent.class, this);
}

// ...
}

关于jsf - 如何为所有 UIInput 注册(组件)SystemEventListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13748202/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com