I've developped an ArgumentTag that allow to set attributes of the direct
parent tag.
so, with little evolution in MessageTag code (move "doStartTag" to
"doEndTag" and inherit BodyTagSupport) + in struts-bean.tld (set bodycontent
to "JSP" instead of "empty"), I can set bean:message arguments with a
bean:write tag


<bean:message key="welcome">
    <bean:argument arg="arg0">
        <bean:write name="user" property="name"/>
    </bean:argument>
</bean:message>


I think it could be used for other tags where attribute value could be
dynamically setted without scriptlet notation.


Here is the code from my argument tag :


import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.*;
import java.lang.reflect.*;

/**
 * Custom tag that sets attributes of it's parent tag
 * to allow use of bean:write tag to set attribute value.
 *
 * @author Nicolas De Loof
 */
public class ArgumentTag extends BodyTagSupport {

    // -------------------------------------------- Properties


    protected String arg = null;

    public String getArg() {
       return (this.arg);
    }

    public void setArg(String arg) {
       this.arg = arg;
    }

    // -------------------------------------------- Public Methods

    /**
     * Process the start tag.
     *
     */
    public int doStartTag() {
       // Process the body of this tag
       return (EVAL_BODY_TAG);
    }

    /**
     * Process the end tag.
     *
     */
    public int doEndTag() throws JspException {

       // Set argument to parent tag
       Tag parentTag = getParent();

       if (arg == null) {
          JspException e = new JspException
             ("arg is a mandatory attribute");
       }

       String setterName = "set"
          + arg.substring(0,1).toUpperCase()
          + arg.substring(1);
       try {
          System.out.println("setterName "+setterName);
          Method setter = parentTag.getClass().getMethod(
             setterName, new Class[] { String.class } );

          setter.invoke(parentTag,
             new Object[] {getBodyContent().getString()} );

        }
        catch (NoSuchMethodException nsme) {
           JspException e = new JspException
           ("incorrect argument name");
        }
        catch (IllegalAccessException iae) {
           JspException e = new JspException
             ("java.lang.reflect exception");
        }
        catch (InvocationTargetException ite) {
           JspException e = new JspException
              ("incorrect parent tag or argument name");
        }
        return (EVAL_PAGE);
     }


     /**
      * Release any acquired resources.
      */
     public void release() {
        super.release();
        arg = null;
     }

}


--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to