1
我正在開發一個包含AJAX客戶端行爲的組件。行爲取決於提供給組件的參數(例如渲染和執行目標)。由於性能原因,我不想使用基於facelet的複合組件來實現組件。JSF:何時將ClientBehavior添加到自己的組件
我的問題是,當客戶行爲應該被添加到組件。如果我在encodeBegin()
方法中調用addClientBehavior()
,我會收到NullPointerException
的UIComponentBase.restoreBehaviorsState()
。如果我在構建時添加行爲,則ajax請求可行,但參數尚不可用。有適當的方法或事件何時適當地添加行爲?
我使用mojarra 2.1.7。下面是我的組件的簡短例子沒有動態的AJAX的東西:
@FacesComponent(value="simpleTestLink")
public class SimpleTestLink extends HtmlCommandLink{
private Logger logger=LoggerFactory.getLogger(getClass());
private AjaxBehavior ajax;
enum PropertyKeys{aProp};
public SimpleTestLink() {
logger.debug("SimpleTestLink created");
// adding ajax here works, but no parameters available yet
ajax = new AjaxBehavior();
ajax.setExecute(Arrays.asList(new String[]{"@this"}));
ajax.setRender(Arrays.asList(new String[]{"@form"}));
addClientBehavior(getDefaultEventName(), ajax);
}
@Override
public void encodeBegin(FacesContext context) throws IOException {
logger.debug("encodeBegin num: " + getAttr());
// adding ajax here fails
super.encodeBegin(context);
}
@Override
public boolean getRendersChildren() {
return true;
}
@Override
public void encodeChildren(FacesContext context) throws IOException {
Object attr = getAttr();
HtmlOutputText outputText = new HtmlOutputText();
outputText.setValue("testlink["+ attr+"]\n");
outputText.encodeAll(context);
super.encodeChildren(context);
}
private Object getAttr() {
return getAttributes().get(PropertyKeys.aProp.name());
}
}
感謝您的幫助,
延