Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSF 2 Custom components having Expression Language for attribute value don't trigger the attribute setter

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?

like image 479
RaresI Avatar asked Aug 08 '12 14:08

RaresI


1 Answers

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 (and binding) 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 the setValueExpression() (because rendering of the view would otherwise crash when the id (or binding) 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. The setValueExpression() ultimately also end up in the StateHelper (note that it doesn't call the setter at all; just call the getter if you need the data) and the getAttributes() also resolves the values from the StateHelper.

public String getName() {
   return (String) getStateHelper().eval("name");
}

public void setName(String name) {
    getStateHelper().put("name", name);
}

Note that you can safely remove the getId() and setId() methods, because they're already definied in the UIComponentBase superclass which you're extending from.

like image 178
BalusC Avatar answered Oct 14 '22 18:10

BalusC