This is my first code contribution to this forum. From information I 
received from the Struts web site, this seems to be the appropriate place 
to post; I apologize if it is not.

THE PROBLEM:

The tag:  <nested:text property="value" size="10" maxlength="12" /> allows you to pull 
the data for the text box from a bean property, but 
does not allow you to dynamically set the text box's size or maxlength 
from a property...you must either hard code them or use scriptlet syntax. 
In the solution I came up with for a project I am working on, the tag 
becomes:

<nested:text property="value" size="dataSize" maxlength="dataMaxSize" />

where the bean in question pulls its size and maxlength from properties 
specified in the bean itself. This would be useful in many applications 
where fields, etc. are created dynamically. My code allows you to specify 
literal numbers for size and maxlength as well, where it will behave like 
it currently does. This idea could be extended to other nested tags as 
well to allow more dynamic behavior.

-----------------------------------------------------------------------------------------
//THE SOLUTION:

package org.apache.struts.taglib.nested.html;

import javax.servlet.jsp.tagext.Tag;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.taglib.nested.NestedPropertyHelper;
import org.apache.struts.taglib.nested.html.NestedTextTag;
import org.apache.struts.util.RequestUtils;

public class ExtendedNestedTextTag extends NestedTextTag {
        private String sz;
        private String maxLen;

        public ExtendedNestedTextTag () {
                super();
        }

        public String getMaxlength() {
                return maxLen;
        }
 
        public void setMaxlength(String property){
 
                if(property == null)
                        return;
                else if(property.charAt(0) >= '0' && 
                        property.charAt(0) <='9') //specifying a literal number
                        super.setMaxlength(property);
                else{ //specifying a bean property
                        Object bean; 
                        try{
                                Tag parentTag = 
NestedPropertyHelper.getNestingParentTag(this); 
                                String propName = 
NestedPropertyHelper.getNestedProperty(property,parentTag);
 
                                bean = RequestUtils.lookup(pageContext, 
name, null);
                                this.maxLen = BeanUtils.getProperty(bean,propName);
                                super.setMaxlength(this.maxLen);
                        }catch(Exception e){
                                System.out.println("Exception: "+e);
                        }
                }
        }

        public String getSize() {
                return sz;
        }
 
        public void setSize(String property) { 
                if(property == null)
                        return;
                else if(property.charAt(0) >= '0' && 
                        property.charAt(0) <='9') //specifying a literal number
                        super.setSize(property);
                else{ //specifying a bean property
                        Object bean; 
                        try{
                                Tag parentTag = 
NestedPropertyHelper.getNestingParentTag(this); 
                                String propName = 
NestedPropertyHelper.getNestedProperty(property,parentTag);
                                bean = RequestUtils.lookup(pageContext, 
name, null);
                                this.sz = BeanUtils.getProperty(bean,propName);
                                super.setSize(this.sz);
                        }catch(Exception e){
                                System.out.println("Exception: "+e);
                        }
                }
        }
}

-------------------------------------------------------------
Then, you would just need to change the text tag in the struts-nested.tld 
file so that it pointed to my class instead of NestedTextTag. Let me know 
what you think. If there is interest, I can document it better or make 
other additions/changes.

Thanks,
Michael McElwee

Reply via email to