我创建了一个简单的HtmlInputText
<h:inputText binding="#{IndexBean.objUIInput}" />
Run Code Online (Sandbox Code Playgroud)
然后在我的托管bean中,它是: -
private UIInput objUIInput;
public UIInput getObjUIInput() {
objUIInput.setValue("laala");
return objUIInput;
}
public void setObjUIInput(UIInput objUIInput) {
System.out.println("Set!!");
this.objUIInput = objUIInput;
}
Run Code Online (Sandbox Code Playgroud)
但我总是得到NullpointerException.我需要在JSF页面上做任何额外的事情吗?就像我们做jsp:usebean setproperty?请帮我.
每当您想在显示之前更改组件的默认状态/行为时,您需要自己实例化它.即在宣言期间:
private UIInput objUIInput = new HtmlInputText();
Run Code Online (Sandbox Code Playgroud)
或在施工期间:
public Bean() {
this.objUIInput = new HtmlInputText();
}
Run Code Online (Sandbox Code Playgroud)
或者,正如Bozho建议的那样,使用@PostConstruct:
@PostConstruct
public void init() {
this.objUIInput = new HtmlInputText();
}
Run Code Online (Sandbox Code Playgroud)
(将在构造bean和初始化/设置所有托管属性后进行).
事实上,你最好不要在getter/setter中做任何业务逻辑.它们将用于访问bean属性,并且可以在bean的生命周期中多次调用它们.
根据注释,您也可以将UIInput#setValue()调用移动到setter方法.在预先创建组件之后,JSF将直接调用它一次.
public void setObjUIInput(UIInput objUIInput) {
this.objUIInput = objUIInput;
this.objUIInput.setValue("laala");
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3502 次 |
| 最近记录: |