JSF custom component: support for arguments of custom types, the attribute setter is never invoked

北城余情 提交于 2019-12-01 05:43:42

This has nothing to do with custom types. This has to do with using literal (static) values versus EL as in attributename="attributevalue" versus attributename="#{attribute.value}".

This behavior is expected and by specification. Attribute values which are EL expressions (ValueExpressions) are been set by UIComponent#setValueExpression(). They are namely supposed to be evaluated only when they are really been requested, usually during view render time. They shouldn't be evaluated directly during baking the UIComponent instance as that would defeat the nature of dynamic value expressions (think of depending on the current iteration round of data table).

Better is to delegate the getters/setters of attributes which can hold an EL value expression to UIComponent#getStateHelper() instead of to local properties. The setValueExpression() will namely ultimately also end up in the StateHelper. The UIComponent#getAttributes() also resolves the values from the StateHelper.

public Image getImage() {
   return (Image) getStateHelper().eval("image");
}

public void setImage(Image image) {
    getStateHelper().put("image", image);
}

Note that there's no local property. So when you need the (evaluated) value of the attribute, then just call the getter.

In order to achieve your initial functional requirement, which is the logging of the set attribute, you might want to add the logging statement to the setValueExpression() override which delegates to super.

@Override
public void setValueExpression(String name, ValueExpression binding) {
    log.debug(....);
    super.setValueExpression(name, binding);
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!