UIComponent 构造函数中的 getAttributes().get("attributeName") returns null
getAttributes().get("attributeName") in UIComponent constructor returns null
我正在测试 JSF 组件,但出现 NullPointerException :( 问题代码是:
@FacesComponent(value="mycomponent0")
public class MyComponent extends HtmlPanelGroup{
MyComponent(){
String a0=this.getAttributes().get("attr0");
}
}
taglib.xml 的标签包含 attr0 属性,标签用法为:
<abc:mycomponent attr0="helloworld"></abc:mycomponent>
所以我的问题是导致问题的原因以及如何处理?
谢谢
我想我可以找到解决 NPE 问题的方法...
我不使用构造函数的主体来获取属性,而是在覆盖的 encodeBegin 方法中获取属性;
@Override
public void encodeBegin(FacesContext context) throws IOException {
String id=this.getAttributes().get("id").toString();
System.out.println("debug: attribute id="+id);
String color=this.getAttributes().get("attr0").toString();
System.out.println("debug: attribute attr0="+attr0);
HtmlOutputLabel label0=new HtmlOutputLabel();
label0.setValue("attribute attr0 value="+attr0);
this.getChildren().add(label0);
super.encodeBegin(context);
}
所以它工作正常,我认为我可以接受这个解决方案;我不确定这是不是最佳方式,所以请分享一些片段...
我正在测试 JSF 组件,但出现 NullPointerException :( 问题代码是:
@FacesComponent(value="mycomponent0")
public class MyComponent extends HtmlPanelGroup{
MyComponent(){
String a0=this.getAttributes().get("attr0");
}
}
taglib.xml 的标签包含 attr0 属性,标签用法为:
<abc:mycomponent attr0="helloworld"></abc:mycomponent>
所以我的问题是导致问题的原因以及如何处理?
谢谢
我想我可以找到解决 NPE 问题的方法...
我不使用构造函数的主体来获取属性,而是在覆盖的 encodeBegin 方法中获取属性;
@Override
public void encodeBegin(FacesContext context) throws IOException {
String id=this.getAttributes().get("id").toString();
System.out.println("debug: attribute id="+id);
String color=this.getAttributes().get("attr0").toString();
System.out.println("debug: attribute attr0="+attr0);
HtmlOutputLabel label0=new HtmlOutputLabel();
label0.setValue("attribute attr0 value="+attr0);
this.getChildren().add(label0);
super.encodeBegin(context);
}
所以它工作正常,我认为我可以接受这个解决方案;我不确定这是不是最佳方式,所以请分享一些片段...