You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Niall Pemberton <ni...@btInternet.com> on 2001/05/01 03:36:02 UTC

RE: Creating dynamically

> We plan to store bean data has hidden form objects , so that it could be
> passed to the subsequent pages, without having to make the scope of the
> beans to be session. We do not know how many attributes the bean objects
> can have, and plan to store it in an array of hidden form objects like
> this.

How about using a Tag to generate the hidden fields for each of the beans
properties.

I have written the following StoreBeanTag which does this.

Niall


-------------------- Start of StoreBeanTag--------------------------->
package mytaglib;

import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;

import javax.servlet.jsp.tagext.TagSupport;
import javax.servlet.jsp.JspException;

import org.apache.struts.util.PropertyUtils;
import org.apache.struts.util.RequestUtils;
import org.apache.struts.util.ResponseUtils;

/**
 *
 * This tag takes a Bean and stores all its properties/values
 * as "hidden" input type html tags.
 *
 * @author Niall Pemberton
 * @version 1.0
 */

public class StoreBeanTag extends TagSupport {

    public static String QUOTE = "\"";

    protected String name = null;
    protected String property = null;
    protected String scope = null;

    /** Get the name */
    public String getName() {
        return (this.name);
    }

    /** Set the name */
    public void setName(String name) {
        this.name = name;
    }

    /** Get the property  */
    public String getProperty() {
        return (this.property);
    }

    /** Set the property */
    public void setProperty(String property) {
        this.property = property;
    }

    /** Get the scope of the property */
    public String getScope() {
        return (this.scope);
    }

    /** Set the scope of the property */
    public void setScope(String scope) {
        this.scope = scope;
    }

    /**
     * Process the tag
     */
    public int doStartTag() throws JspException {

      StringBuffer buffer = new StringBuffer();

      // Find the specified Bean
      Object bean = null;
      if (property == null) {
        bean = RequestUtils.lookup(pageContext, name, scope);
      } else {
        bean = RequestUtils.lookup(pageContext, name, property, scope);
      }

      if (bean == null) {
//         return (SKIP_BODY);
         JspException e = new JspException("StoreBeanTag: Missing bean " +
name+" "+property);
         RequestUtils.saveException(pageContext, e);
         throw e;
      }

      // Get the beans properties
      PropertyDescriptor[] descriptors =
PropertyUtils.getPropertyDescriptors(bean);
      for (int i = 0; i < descriptors.length; i++) {
          String name = descriptors[i].getName();
          if (!(name.equals("class"))) {
            try {
              Object value = PropertyUtils.getSimpleProperty(bean, name);
              name = property == null ? name : property+"."+name;
              buffer.append(createHiddenField(name, value));
            }
            catch (NoSuchMethodException ex) {}
            catch (InvocationTargetException ex) {}
            catch (IllegalAccessException ex) {}
          }
      }

      // Print the created message to our output writer
      ResponseUtils.write(pageContext, buffer.toString());

      // Continue processing this page
      return (SKIP_BODY);
    }


    /**
     * Create a "hidden" field for the property
     */
    protected String createHiddenField(String property, Object obj) {

      if (obj == null)
        return "";

      String value = obj.toString();
      if (value == null  || value.equals(""))
        return "";

      return "<input type="+QUOTE+"hidden"+QUOTE+
                   " name="+QUOTE+property+QUOTE+
                  " value="+QUOTE+value+QUOTE+"/>\r\n";

    }
    /**
     * Release all allocated resources.
     */
    public void release() {

	super.release();

	name = null;
	property = null;
	scope = null;
    }

}
-------------------- End of StoreBeanTag--------------------------->