本文介绍了JSF自定义组件:自定义类型的参数支持,该属性设置器永远不会调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经建立了我的自定义的 JSF 据很多教程的一个组成部分(我用的 PrimeFaces ),我添加了这是顺利通过了一个说法组件。

I've created my custom JSF component according to one of many tutorials (I'm using PrimeFaces), I've added an argument which was successfully passed to component.

<c:custom command="command"/>
public void setCommand(String command) {
    log.debug("setCommand {}", command);
    this.command = command;
}

但我需要自定义类型的参数,并且在这个时候,我无法找到的教程,这是只处理最简单的情形。

But I need the argument of custom type, and that's something that I couldn't find in tutorials, which are handling only the most trivial cases.

<c:custom image="#{currentImageBean.image}"/>
public void setImage(Object image) {
    log.debug("setImage {}", image);
    this.image = (Image) image;
}

这个bean是返回类型图片的对象,但二传手不叫。我预计这将正常工作,否则一个很好的教程要提这样的话,但现在我坚持的错误更糟:没有任何反应,也没有什么日志中暗示为什么......那么,什么是错的,我需要改变,到哪里寻找潜在的错误?

The bean is returning the object of type Image, but the setter is not called. I've expected that this will work, because otherwise a good tutorial should mention that case, but now I'm stuck with the worse of the errors: nothing happens, and there's nothing in logs suggesting why... So, what is wrong, what I need to change, where to look for potential error?

推荐答案

这无关与自定义类型。这与在属性名=的AttributeValue属性名=#{} attribute.value使用文字(静态)值与EL的做

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}".

这是正常现象,并通过规范。属性这是EL前pressions值(<一个href=\"http://docs.oracle.com/javaee/6/api/javax/el/ValueEx$p$pssion.html\"><$c$c>ValueEx$p$pssions)都被设置由<一个href=\"http://docs.oracle.com/javaee/6/api/javax/faces/component/UIComponent.html#setValueEx$p$pssion%28java.lang.String,%20javax.el.ValueEx$p$pssion%29\"><$c$c>UIComponent#setValueEx$p$pssion().他们本来就是只进行评估时,他们的真正的被请求,一般认为在渲染时间。他们不应该被评估的直接烘烤的> UIComponent 实例因为这会打败动态值前pressions的性质(认为取决于当前迭代轮数据表的)。

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).

更好的是委托属性的getter / setter方法​​,可容纳一个EL值前pression到<$c$c>UIComponent#getStateHelper()代替本地特性。在 setValueEx pression()将最终即也是在 StateHelper 结束。在<$c$c>UIComponent#getAttributes()还解决从<$c$c>StateHelper.

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

请注意,有没有本地属性。所以,当你需要的属性(评估)的值,则只需调用的getter。

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

为了实现你最初的功能需求,这是一组属性的记录,你可能想记录语句添加到 setValueEx pression()覆盖委托给超级

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

这篇关于JSF自定义组件:自定义类型的参数支持,该属性设置器永远不会调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 20:02