2016-07-30 8 views
0

を返すです。のgetAttributes()のUIComponentコンストラクタで(「attributeNameと」)を取得私は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> 

だから私の質問は、問題を引き起こすものと、それを処理する方法である

おかげ

+0

ここでBalusCの答えを読んでください。説明: https://stackoverflow.com/questions/30404268/cannot-get-custom-component -attribute-from-backing-bean – Pazura

答えて

0

私は属性を取得するために、コンストラクタの体を使用していますが、オーバーライドencodeBegin方法で属性を取得しない

...私はNPEの問題を回避する方法を見つけ出すことができると思います。

@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); 
    } 

このように機能していると思いますが、この解決策では問題ありません。私はそれが最も最適な方法であるかどうか分からないので、いくつかのスニペットを共有してください...

関連する問題