UIComponent构造函数中的getAttributes()。get(" attributeName")返回null

时间:2016-07-30 14:37:52

标签: jsf constructor attributes custom-component

我正在测试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>

所以我的问题是导致问题的原因以及如何处理它?<​​/ p>

由于

1 个答案:

答案 0 :(得分:0)

我想我可以弄清楚如何解决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);
    }

所以它正在工作,我认为我对这个解决方案没问题;我不确定它是最优化的方式,所以请分享一些片段......

相关问题