You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by st...@jtheory.com on 2006/07/06 00:20:07 UTC

repopulating fields from an array in a formbean

I have two related questions...  I can probably figure out ways to hack
around these issues, but I'm hoping for a "best practices" solution.

My requirement is pretty simple: you click on a category link, the
action builds the List of items for that category, and the JSP iterates
them and shows quantity input fields using <html:text
property="quantity"/>.  That's fine.  It matches up with a String[]
quantity property in the formbean class (I'm not using dynabeans).  The
user enters quantities, and clicks the "Add to Order" submit button.  If
the quantities they entered pass formbean validation, the items get
added.  If they fail validation, the item list displays again (with
their quantity entries repopulated).

The validation error handling is where I'm stuck.  If the form bean
validate() method fails, Struts automatically shows the input (the item
listing action)... but:
1) How do I reload the item listing page with the right category info? 
I managed to get this working for now by putting the categoryId in a
hidden field on the page (submitting to a form that doesn't actually
need it... seems like a hack, but it works).
2) How can I repopulate the quantity input fields (all with name
"quantity") with the right value?  Struts populates the fields with
"[Ljava.lang.String;@36947bbe" -- the whole array, apparently, instead
of the right element.

#2 is the most important one, since it seems like there must be a
straightforward way to do this in Struts... if arrays work for incoming
params, there must be a way to use them for form repopulation, right?

Thanks for any suggestions!
Rob W.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: repopulating fields from an array in a formbean

Posted by Michael Jouravlev <jm...@gmail.com>.
Here is my own opinion, which cannot be considered an "official" or a
"best" practice, but nevertheless:

[ Approach 1 ]
You use one action class to display the page (render phase) and to
process input (submit phase). In this case I suggest not to use
"input" property entirely, and not to use automatic validation. When
your action receives a request, you decide what phase it belongs to,
and act accordingly. For render phase you need to populate the beans
for the page, for submit phase you need to validate the input.

How exactly do you differentiate between render and submit phases is
your choice. You can differentiate by HTTP method (POST for submit,
GET for render) or by presence of a command/event in the request.

[ Approach 2 ]
You use two separate actions in setup/submit pattern: a setup action
prepares data for rendering, submit action processes input. The submit
action has autovalidation turned on, the setup action has it turned
off. I suggest to render through setup action, do not render a page
directly from submit action. I mean, specify setup action, not the JSP
page in the "input" property.

This allows to separate the concerns, but raises the problem: when you
forward to setup action from submit action, the action form will be
populated again (I suppose that both actions use the same actionform
to properly setup/obtain data through the page.) Rats. And this
feature cannot be turned off.

To overcome second populate issue, you can either set some kind of
token in the request and check it in every setter -- too much work.

A simpler way is to NOT specify the form for your setup action. You
will need to use "name" attributes for HTML tags, but this is the only
major annoyance.

For other observations you may check this page (caution, work in
progress): http://wiki.apache.org/struts/StrutsManualActionClasses

Michael.

On 7/5/06, struts@jtheory.com <st...@jtheory.com> wrote:
> I have two related questions...  I can probably figure out ways to hack
> around these issues, but I'm hoping for a "best practices" solution.
>
> My requirement is pretty simple: you click on a category link, the
> action builds the List of items for that category, and the JSP iterates
> them and shows quantity input fields using <html:text
> property="quantity"/>.  That's fine.  It matches up with a String[]
> quantity property in the formbean class (I'm not using dynabeans).  The
> user enters quantities, and clicks the "Add to Order" submit button.  If
> the quantities they entered pass formbean validation, the items get
> added.  If they fail validation, the item list displays again (with
> their quantity entries repopulated).
>
> The validation error handling is where I'm stuck.  If the form bean
> validate() method fails, Struts automatically shows the input (the item
> listing action)... but:
> 1) How do I reload the item listing page with the right category info?
> I managed to get this working for now by putting the categoryId in a
> hidden field on the page (submitting to a form that doesn't actually
> need it... seems like a hack, but it works).
> 2) How can I repopulate the quantity input fields (all with name
> "quantity") with the right value?  Struts populates the fields with
> "[Ljava.lang.String;@36947bbe" -- the whole array, apparently, instead
> of the right element.
>
> #2 is the most important one, since it seems like there must be a
> straightforward way to do this in Struts... if arrays work for incoming
> params, there must be a way to use them for form repopulation, right?
>
> Thanks for any suggestions!
> Rob W.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org