webtier@glassfish.java.net

Re: [webtier] JSF2: MethodExpression attribute doesn't get set

From: <webtier_at_javadesktop.org>
Date: Sun, 02 Aug 2009 16:32:17 PDT

Actually, you can do a bit better than the RI. The MethodRule in the RI assumes that the tag attributes are implemented as component properties. If you use attributes, the rule won't grip. This modification works for either case:

[pre]
public class MethodRule extends MetaRule {
   private String name;
   private Class<?> returnType;
   private Class<?>[] parameterTypes;
   
   public MethodRule(String name, Class<?> returnType, Class<?>[] parameterTypes) {
      this.name = name;
      this.returnType = returnType;
      this.parameterTypes = parameterTypes;
   }
   
   @Override
   public Metadata applyRule(final String name, final TagAttribute attribute,
         MetadataTarget meta) {
      if (!this.name.equals(name)) return null;
      final Method writeMethod = meta.getWriteMethod(name);
      return new Metadata() {
         @Override
         public void applyMetadata(FaceletContext ctx, Object instance) {
            MethodExpression expr = attribute.getMethodExpression(ctx, returnType, parameterTypes);
            try {
               if (writeMethod == null) ((UIComponent) instance).getAttributes().put(name, expr);
               else writeMethod.invoke(instance, expr);
            } catch (InvocationTargetException e) {
               throw new TagAttributeException(attribute, e.getCause());
            } catch (Exception e) {
               throw new TagAttributeException(attribute, e);
            }
         }
      };
   }
}
[/pre]
[Message sent by forum member 'cayhorstmann' (cayhorstmann)]

http://forums.java.net/jive/thread.jspa?messageID=358541