Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
656 views
in Technique[技术] by (71.8m points)

jsf 2 - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...