I have build a custom component in JSF 2.0
The tag looks like this:
<x:myTag id="1" name="AAA" />
The corresponding java class:
@FacesComponent("a.b.c.MyTag")
public class UIMyTag extends UIInput {
private String name;
private String id;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public void encodeBegin(FacesContext context) throws IOException {
ResponseWriter writer = context.getResponseWriter();
logger.debug(getName()); //prints null for name="#{dummyBean.name}"
// and AAA for name="AAA"
logger.debug(getAttributes().get("name")); // always correct value
...
}
....
}
If I use
<x:myTag id="1" name="AAA" />
everything works as expected, but when I use EL for myTag attributes the setName() method never gets called. So for,
<x:myTag id="#{dummyBean.id}" name="#{dummyBean.name}" />
I always get null for the name property inside my encodeBegin method. After debugging I’ve noticed that the setName method never gets called. I thought that maybe something regarding EL messes things up (and I still believe that the reason is related to that), but what’s really weird is that the id property works good: the setter gets called, and the value is as expected when the econding begins.
I have to mention that if I call getAttributes().get("name") from the encodeBegin method I get the correct name value, but I’m intrigued why it doesn’t work with getter and setter.
Any ideas what’s missing to my component?
This behavior is expected and by specification. Attribute values which are value expressions are set by
UIComponent#setValueExpression(). They are namely supposed to be evaluated only when they are really been used, usually during view render time.The
id(andbinding) attribute has special treatment: it’s evaluated during view build time before it’s been set, so the “regular” setter would be called instead of thesetValueExpression()(because rendering of the view would otherwise crash when theid(orbinding) attribute dynamically evaluates to a different value than it was during the view build time for some reason).Better is to delegate the getters/setters to
UIComponent#getStateHelper()instead of to local properties. ThesetValueExpression()ultimately also end up in theStateHelper(note that it doesn’t call the setter at all; just call the getter if you need the data) and thegetAttributes()also resolves the values from theStateHelper.Note that you can safely remove the
getId()andsetId()methods, because they’re already definied in theUIComponentBasesuperclass which you’re extending from.