我想自动将页面中 UIInput 组件的标签属性设置为我已经放置的 HtmlOutputLabel 组件,就像 PrimeFaces 开发人员所做的那样:http ://cagataycivici.wordpress.com/2011/02/11/label-provider- for-jsf-input-components/ 只有他使用系统事件来做到这一点,这是一个仅在 JSF-2.0 中可用的功能,而我的应用程序是在 JSF 1.2 中。
是否可以使用 JSF-1.2 和 Phase Listener 来做到这一点?会有什么弊端?
提前致谢!
更新:这是我尝试使用 Phase Listener 时的样子:
@Override
public void beforePhase(PhaseEvent event) {
System.out.println("REGISTERING Label Provider");
FacesContext context = event.getFacesContext();
List<UIComponent> components = context.getViewRoot().getChildren();
for (UIComponent uiComponent : components) {
if (uiComponent instanceof HtmlOutputLabel) {
HtmlOutputLabel outputLabel = (HtmlOutputLabel) uiComponent;
System.out.println("CONFIGURING LABEL: " + outputLabel.getId());
UIComponent target = outputLabel.findComponent(outputLabel
.getFor());
if (target != null) {
target.getAttributes().put("label", outputLabel.getValue());
}
}
}
}
@Override
public PhaseId getPhaseId() {
// Only listen during the render response phase.
return PhaseId.RENDER_RESPONSE;
}
当我访问视图时,它从不打印“配置标签”部分。验证 uiComponent 是否为 HtmlOutputLabel 的正确测试是什么?或者我做错了什么?