RE: Creating html:hidden ... dynamically

2001-04-30 Thread Niall Pemberton

 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---




RE: Creating html:hidden ... dynamically

2001-04-30 Thread Lewis Henderson

You could use html:hidden property=hidden[1] value=value1/

Look at the struts-documentation for indexed properties...

Your getter/setter methods use an int parameter... 

String getHidden(int index){}
and
void setHidden(int index, String value){}

Any help?


Lewis
-Original Message-
From: Anil Guntgatti [mailto:[EMAIL PROTECTED]]
Sent: 30 April 2001 16:13
To: [EMAIL PROTECTED]
Subject: Creating html:hidden ...  dynamically


Hi,

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.

input type=hidden name=hidden1 value=value1
input type=hidden name=hidden2 value=value2
...
input type=hidden name=hiddenn value=valuen

As per the struts framework, we need to define getter and setter for each
and every html form objects in the java form object. How do we go about
doing this.
Any help would be appreciated.

Anil.
Mail to: [EMAIL PROTECTED]






RE: Creating html:hidden ... dynamically

2001-04-30 Thread Jason Chaffee
Title: RE: Creating html:hidden ...  dynamically





Is there a reason you don't want to store this information in a bean, It makes more sense than storing it in hidden fields. You are essentially doing your own session scope management. Why not let your container do this for you?

-Original Message-
From: Anil Guntgatti [mailto:[EMAIL PROTECTED]]
Sent: Monday, April 30, 2001 8:13 AM
To: [EMAIL PROTECTED]
Subject: Creating html:hidden ...  dynamically



Hi,


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.


input type=hidden name=hidden1 value=value1
input type=hidden name=hidden2 value=value2
...
input type=hidden name=hiddenn value=valuen


As per the struts framework, we need to define getter and setter for each
and every html form objects in the java form object. How do we go about
doing this.
Any help would be appreciated.


Anil.
Mail to: [EMAIL PROTECTED]