You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@struts.apache.org by Chris Miller <ch...@hotmail.com> on 2000/08/20 18:16:11 UTC

multipart/form-data

I have created a form that has a <struts:file> element, and hence have set
enctype="multipart/form-data" in the <struts:form> tag. I also have some
other text and textarea fields I am submitting with the form.

The problem is that my validatingActionForm does not seem to get it's
properties set for the text & textarea fields, so my validation always fails
(and when the form is redisplayed the field values are all set back to blank
entries). If I don't have the enctype set the form properties get set
correctly.

Is this a limitation with HTTP or struts? My understanding is that all the
form data should still get submitted - perhaps it needs special handling for
a multipart/form-data submission?


Checkboxes

Posted by Chris Miller <ki...@vardus.com>.
Hi everyone,

I've had enough! I've decided I'm going to implement a solution for the
problem with not being able to unselect checkboxes properly (since an
unselected checkbox doesn't get submitted with a form). I think I've got a
solution that, from the user's point of view, simply means adding an
attribute to a <struts:checkbox> tag and everything else is taken care of by
struts.

Here's what I propose (and am halfway through coding, so if you can see any
problems with this let me know before I code myself into a corner ;-).

1) Add a new attribute to the <struts:checkbox> tag called 'managed'.
Supplying a managed="true" attribute will activate the new functionality
(this way, backwards compatibility is not compromised).

2) When managed="true", the <struts:checkbox> tag will render *two* fields
for the form - the first one is the normal checkbox field, and the second is
a hidden field that corresponds to the checkbox field. ie:

<struts:checkbox property="published" managed="true"/>

will result in (assuming the 'pulished' property is currently 'false'):

<input type="checkbox" name="published"><input type="hidden"
name="strutsCBpublished" value="false">

Note that the hidden field has a 'strutsCB' prefix.

3) Next, when the BeanUtils.populate method is called, for any field that
has a strutsCB prefix and a value="true", a check will be made to find the
corresponding checkbox field. If it's not found, then we can assume that the
user has unchecked the checkbox on the form, so we'll set the property back
to false.

Does this sound OK?

There are a couple of things I'm not sure about in the struts code - what
are the mapping.getFormPrefix() calls all about? That's *bound* to affect
me, but I don't know exactly what those calls are there for. I'm
investigating further, but if someone has a quick answer...
Also, what happens if lots of checkboxes have the same name but different
values? I'll look into this one too, but all tips appreciated.

Hope this is of interest to some of you,

Chris


Re: multipart/form-data

Posted by Chris Miller <ki...@vardus.co.uk>.
Hmmm. I'm currently using the file upload code from www.servlets.com which
seems to do most of what I want as far as getting the file attachment goes.
So I guess my options are:
1) Just live with the limitation and use multiple forms, one with the text
and one for the file (urgh!).
2) Patch Struts to handle the extra work involved in accepting
multipart/form-data parameters. If I get time I'd like to attempt this,
however I would also like it to be a generic solution for two reasons.
First, it could become part of the base product so everyone could use it,
and second, I wouldn't have to patch Struts everytime I download a new
version!

Do you think this is feasible, or would the code have to be tied to the
www.servlets.com code? I haven't looked into it yet but I would imagine it
wouldn't be too difficult to provide a way to plug-in the file handling in a
generic fashion.

Also, I take it this would really just be a stop-gap until the feature is
implemented in the servlet API (although I couldn't see mention of it in the
2.3 draft).

Any comments or pointers would be appreciated.

Chris


----- Original Message -----
From: "Craig R. McClanahan" <Cr...@eng.sun.com>
To: <st...@jakarta.apache.org>
Sent: Sunday, August 20, 2000 11:51 PM
Subject: Re: multipart/form-data


> Chris Miller wrote:
>
> > I have created a form that has a <struts:file> element, and hence have
set
> > enctype="multipart/form-data" in the <struts:form> tag. I also have some
> > other text and textarea fields I am submitting with the form.
> >
> > The problem is that my validatingActionForm does not seem to get it's
> > properties set for the text & textarea fields, so my validation always
fails
> > (and when the form is redisplayed the field values are all set back to
blank
> > entries). If I don't have the enctype set the form properties get set
> > correctly.
> >
> > Is this a limitation with HTTP or struts? My understanding is that all
the
> > form data should still get submitted - perhaps it needs special handling
for
> > a multipart/form-data submission?
>
> Yes, it does.  The servlet container only parses the request parameters
for you
> if you are using content type "application/x-www-form-urlencoded", not
> "multipart/form-data".  Struts will need to be enhanced to extract the
> parameters in the latter case -- as well as to provide some sort of
configurable
> handling of the uploaded file.
>
> Craig
>
>
>


Re: multipart/form-data

Posted by "Craig R. McClanahan" <Cr...@eng.sun.com>.
Chris Miller wrote:

> I have created a form that has a <struts:file> element, and hence have set
> enctype="multipart/form-data" in the <struts:form> tag. I also have some
> other text and textarea fields I am submitting with the form.
>
> The problem is that my validatingActionForm does not seem to get it's
> properties set for the text & textarea fields, so my validation always fails
> (and when the form is redisplayed the field values are all set back to blank
> entries). If I don't have the enctype set the form properties get set
> correctly.
>
> Is this a limitation with HTTP or struts? My understanding is that all the
> form data should still get submitted - perhaps it needs special handling for
> a multipart/form-data submission?

Yes, it does.  The servlet container only parses the request parameters for you
if you are using content type "application/x-www-form-urlencoded", not
"multipart/form-data".  Struts will need to be enhanced to extract the
parameters in the latter case -- as well as to provide some sort of configurable
handling of the uploaded file.

Craig