You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Renaud Waldura <re...@waldura.org> on 2001/09/13 23:32:20 UTC

Serializing form beans

I'm attempting to serialize a form bean to a hidden field, but having a hard
time doing it right. Looks like the serialized data is not encoding
properly.

Once I have serialized my bean, what encoding should I apply, if any, to
have it transmitted back to me in good shape? I'm using URLEncoder.encode(),
but I have a feeling it's losing bits. Or maybe it's the String constructor.

I know people on this list are doing this daily, what is the magic
incantation?

--Renaud


Re: Serializing form beans

Posted by Renaud Waldura <re...@waldura.org>.
For wizard-type dialogs.

Instead of stuffing the form bean into the session (where it can be accessed
by multiple instances of the same browser, wreaking complete havoc), the
form bean is present in the request only. It's propagated from request to
request with a hidden field inside the forms.

In order to save the form bean to a hidden field, I serialize and encode it.
Serializing was easy, but I get back an array of bytes that needs to be
encoded before it can safely be written to a Web page. Hence the use of
java.net.URLEncoder.

Unfortunately URLEncoder.encode() performs all kinds of conversions related
to multibyte chars and such, that don't apply to this case -- and actually
mess up my data bytes. I had to write my own encoding routine, mimicked
after URLEncoder.encode().

If this sounds like extra work, it did to me also. I'm eager to hear about
other solutions to this common problem.

--Renaud





----- Original Message -----
From: "Erik Hatcher" <ja...@ehatchersolutions.com>
To: <st...@jakarta.apache.org>
Sent: Friday, September 14, 2001 7:21 PM
Subject: Re: Serializing form beans


> Just out of curiosity, what is the purpose of serializing and encoding the
> form bean?
>
>
> ----- Original Message -----
> From: "Renaud Waldura" <re...@waldura.org>
> To: <st...@jakarta.apache.org>
> Sent: Friday, September 14, 2001 2:28 PM
> Subject: Re: Serializing form beans
>
>
> > Take that back. There's still an issue with the encoding. My guess is
that
> > URLEncoder and/or String() constructor don't convert full bytes.
Grrrrrr.
> >
> > At this point I think I'm going to write my own encoding...
>
>
>



Re: Serializing form beans

Posted by Erik Hatcher <ja...@ehatchersolutions.com>.
Just out of curiosity, what is the purpose of serializing and encoding the
form bean?


----- Original Message -----
From: "Renaud Waldura" <re...@waldura.org>
To: <st...@jakarta.apache.org>
Sent: Friday, September 14, 2001 2:28 PM
Subject: Re: Serializing form beans


> Take that back. There's still an issue with the encoding. My guess is that
> URLEncoder and/or String() constructor don't convert full bytes. Grrrrrr.
>
> At this point I think I'm going to write my own encoding...



Re: Nested elements

Posted by ma...@tumbleweed.com.
As you surmised, <set-property> is currently limited to name/value pairs.

The 'id' attribute (which is an attribute of every element in the struts
config DTD) is an implicit identifier which is not directly reflected in the
code. It is not intended to be specified explicitly.

--
Martin Cooper


----- Original Message -----
From: "Frederick N. Brier" <fb...@multideck.com>
To: <st...@jakarta.apache.org>
Sent: Friday, September 14, 2001 3:46 PM
Subject: Nested <set-property> elements


> I've read the several excellent messages describing deriving classes from
> ActionMapping and using the set-property element in struts-config.xml.
One
> in particular was:
>
> http://www.mail-archive.com/struts-user@jakarta.apache.org/msg08755.html
>
> What I would like to do is have a sequence of elements nested in the
> <action> element which in turn have a sequence of elements.  This should
> become a property of a type such as an ArrayList of an ArrayList in the
> derived ActionMapping class.  It appears that the <set-property> is
limited
> to simple (name,value) pairs, although the <set-property> DTD definition
> does specify a "id" attribute.  Is that true?  Is what I am trying to do
> possible with set-property? Or am I actually going to have to modify the
> struts-config_1_1.dtd?  Are there any other alternatives?  Thank you.
>
>
>
> Frederick N. Brier
> Sr. Software Engineer
> Multideck Corporation
>



Nested elements

Posted by "Frederick N. Brier" <fb...@multideck.com>.
I've read the several excellent messages describing deriving classes from 
ActionMapping and using the set-property element in struts-config.xml.  One 
in particular was:

http://www.mail-archive.com/struts-user@jakarta.apache.org/msg08755.html

What I would like to do is have a sequence of elements nested in the 
<action> element which in turn have a sequence of elements.  This should 
become a property of a type such as an ArrayList of an ArrayList in the 
derived ActionMapping class.  It appears that the <set-property> is limited 
to simple (name,value) pairs, although the <set-property> DTD definition 
does specify a "id" attribute.  Is that true?  Is what I am trying to do 
possible with set-property? Or am I actually going to have to modify the 
struts-config_1_1.dtd?  Are there any other alternatives?  Thank you.



Frederick N. Brier
Sr. Software Engineer
Multideck Corporation


Re: Serializing form beans

Posted by Renaud Waldura <re...@waldura.org>.
Take that back. There's still an issue with the encoding. My guess is that
URLEncoder and/or String() constructor don't convert full bytes. Grrrrrr.

At this point I think I'm going to write my own encoding...



----- Original Message -----
From: "Renaud Waldura" <re...@waldura.org>
To: <st...@jakarta.apache.org>
Sent: Friday, September 14, 2001 10:28 AM
Subject: Re: Serializing form beans


> Answering my own question, I found out the correct way of serializing a
form
> bean.
> Here is code that works for me:
>
>
>
>  /**
>   * Serialize the form bean and return it as a string suited to
>   * be written into a Web page.
>   *
>   * @param form the form bean instance
>   * @return the serialized bean data as an encoded string
>   *
>   * @throws IOException if the serialization failed
>   */
>  public static String serializeAndEncode(ActionForm form)
>   throws IOException
>  {
>   byte[] serialized = serialize(form);
>   return java.net.URLEncoder.encode( new String(serialized) );
>  }
>
>  /**
>   * Decode and de-serialize a form bean.
>   *
>   * @param s the encoded and serialized form bean data
>   * @return a form bean instance
>   *
>   * @throws IOException if the de-serialization failed
>   * @throws ClassNotFoundException if the de-serialization failed
>   */
>  public static ActionForm decodeAndDeserialize(String s)
>   throws IOException, ClassNotFoundException
>  {
>   byte[] serialized = java.net.URLDecoder.decode(s).getBytes();
>   return deserialize( serialized );
>  }
>
>  /**
>   * Serialize a form bean to a byte array.
>   *
>   * @param form the form bean to be serialized
>   * @return the serialized bean as a byte array
>   *
>   * @throws IOException if the serialization failed
>   */
>  public static byte[] serialize(ActionForm form)
>   throws IOException
>  {
>   // store the serialized instance will be written to
>   ObjectOutput out = null;
>
>   // support stream to hold the serialized instance
>   ByteArrayOutputStream temp = new ByteArrayOutputStream();
>
>   try
>   {
>    out = new ObjectOutputStream(temp);
>    out.writeObject(form);
>   }
>   finally
>   {
>    // also closes the support stream "temp"
>    if (out != null) out.close();
>   }
>
>   return temp.toByteArray();
>  }
>
>  /**
>   * De-serialize a form bean from a byte array.
>   *
>   * @param data serialized bytes of the bean
>   * @return a ActionForm instance of the serialized bean
>   *
>   * @throws IOException if the de-serialization failed
>   * @throws ClassNotFoundException if the de-serialization failed
>   */
>  public static ActionForm deserialize(byte[] data)
>   throws IOException, ClassNotFoundException
>  {
>   // store the serialized instance will be read from
>   ObjectInput in = null;
>
>   // the object representation of the serialized instance
>   ActionForm form = null;
>
>   try
>   {
>    in = new ObjectInputStream( new ByteArrayInputStream(data) );
>    form = (ActionForm) in.readObject();
>   }
>   finally
>   {
>    if (in != null) in.close();
>   }
>
>   return form;
>  }
>
>
>
>
>
>
> ----- Original Message -----
> From: "Renaud Waldura" <re...@waldura.org>
> To: <st...@jakarta.apache.org>
> Sent: Thursday, September 13, 2001 2:32 PM
> Subject: Serializing form beans
>
>
> > I'm attempting to serialize a form bean to a hidden field, but having a
> hard
> > time doing it right. Looks like the serialized data is not encoding
> > properly.
> >
> > Once I have serialized my bean, what encoding should I apply, if any, to
> > have it transmitted back to me in good shape? I'm using
> URLEncoder.encode(),
> > but I have a feeling it's losing bits. Or maybe it's the String
> constructor.
> >
> > I know people on this list are doing this daily, what is the magic
> > incantation?
> >
> > --Renaud
> >
> >
>
>


Re: Serializing form beans

Posted by Renaud Waldura <re...@waldura.org>.
Answering my own question, I found out the correct way of serializing a form
bean.
Here is code that works for me:



 /**
  * Serialize the form bean and return it as a string suited to
  * be written into a Web page.
  *
  * @param form the form bean instance
  * @return the serialized bean data as an encoded string
  *
  * @throws IOException if the serialization failed
  */
 public static String serializeAndEncode(ActionForm form)
  throws IOException
 {
  byte[] serialized = serialize(form);
  return java.net.URLEncoder.encode( new String(serialized) );
 }

 /**
  * Decode and de-serialize a form bean.
  *
  * @param s the encoded and serialized form bean data
  * @return a form bean instance
  *
  * @throws IOException if the de-serialization failed
  * @throws ClassNotFoundException if the de-serialization failed
  */
 public static ActionForm decodeAndDeserialize(String s)
  throws IOException, ClassNotFoundException
 {
  byte[] serialized = java.net.URLDecoder.decode(s).getBytes();
  return deserialize( serialized );
 }

 /**
  * Serialize a form bean to a byte array.
  *
  * @param form the form bean to be serialized
  * @return the serialized bean as a byte array
  *
  * @throws IOException if the serialization failed
  */
 public static byte[] serialize(ActionForm form)
  throws IOException
 {
  // store the serialized instance will be written to
  ObjectOutput out = null;

  // support stream to hold the serialized instance
  ByteArrayOutputStream temp = new ByteArrayOutputStream();

  try
  {
   out = new ObjectOutputStream(temp);
   out.writeObject(form);
  }
  finally
  {
   // also closes the support stream "temp"
   if (out != null) out.close();
  }

  return temp.toByteArray();
 }

 /**
  * De-serialize a form bean from a byte array.
  *
  * @param data serialized bytes of the bean
  * @return a ActionForm instance of the serialized bean
  *
  * @throws IOException if the de-serialization failed
  * @throws ClassNotFoundException if the de-serialization failed
  */
 public static ActionForm deserialize(byte[] data)
  throws IOException, ClassNotFoundException
 {
  // store the serialized instance will be read from
  ObjectInput in = null;

  // the object representation of the serialized instance
  ActionForm form = null;

  try
  {
   in = new ObjectInputStream( new ByteArrayInputStream(data) );
   form = (ActionForm) in.readObject();
  }
  finally
  {
   if (in != null) in.close();
  }

  return form;
 }






----- Original Message -----
From: "Renaud Waldura" <re...@waldura.org>
To: <st...@jakarta.apache.org>
Sent: Thursday, September 13, 2001 2:32 PM
Subject: Serializing form beans


> I'm attempting to serialize a form bean to a hidden field, but having a
hard
> time doing it right. Looks like the serialized data is not encoding
> properly.
>
> Once I have serialized my bean, what encoding should I apply, if any, to
> have it transmitted back to me in good shape? I'm using
URLEncoder.encode(),
> but I have a feeling it's losing bits. Or maybe it's the String
constructor.
>
> I know people on this list are doing this daily, what is the magic
> incantation?
>
> --Renaud
>
>