You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Mark Lowe <me...@gmail.com> on 2005/04/03 12:57:22 UTC

Re: Form Handling Question Generic

If I've understood the problem correctly, I think that a list backed
form is what you are after. Search the archieves for "indexed
properties" or last time I looked there was a wiki page explaining
this.


Marl


On Apr 1, 2005 10:40 PM, Scott Purcell <sp...@vertisinc.com> wrote:
> Thank you,
> 
> I have been trying to work with the ActionForm for this and it is not easy to configure.
> 
> I wasn't sure if I was sleeping today, or if falling back to request.getParameter is sometimes the best way to go.
> 
> Sincerely
> Scott
> 
> 
> -----Original Message-----
> From: Leon Rosenberg [mailto:struts_user@anotheria.net]
> Sent: Friday, April 01, 2005 2:31 PM
> To: 'Struts Users Mailing List'
> Subject: AW: Form Handling Question Generic
> 
> Hi,
> 
> If you really don't need to validate but just to store a list of objects in
> the db, i'd suggest to forget about action forms and work with
> request.getParameter. You can put the logic into the name of the parameter
> and/or the value.
> Example:
> 
> List of properties 1..10,
> 
> In the action which is called before the form is shown:
> 
> P_MIN = 1;
> P_MAX = 10;
> List properties = new ArrayList(P_MAX-P_MIN+1);
> for (int i=P_MIN; i<=P_MAX; i++){
>   properties.add("p"+i); //you should use constants and proper names for it,
> 'p' is just for demonstration
> }
> req.setAttribute("properties", properties);
> 
> In the page:
> <form action="pathToProcessingAction" >
> ...
> <logic:iterate name="properties" type="java.lang.String" id="property">
>   ... <!--Caption of the property --> <bean:message name="property"/>
> (assumes you have propriate name in your resources)
>   <input type="checkbox" name="<bean:write name="property"/>_checkbox"
> value="set">
> ...
> </logic:iterate>
> </form>
> 
> In the processing action :
> 
> Here you have multiple choices, you can iterate over all parameter in
> request with value set, and get the names of set checkboxes this way, or you
> can test all potential properties.
> 
> The first case:
> //first set all checkbox related objects to false
> Enumeration allNames = req.getParameterNames();
> while(allNames.hasMoreElements()){
>   String paramName = (String)allNames.nextElement();
>   String paramValue = req.getParameter(paramName);
>   if (paramValue.equals("set")){
>      //set the object related to paramName to set.
>   }
> }
> 
> Second case:
> for (int i=P_MIN; i<=P_MAX; i++){
>   String paramValue = req.getParameter("p"+i); //you should use constants
> and proper names for it, 'p' is just for demonstration
>   if (paramValue!=null && paramValue.equals("set"){
>     //set related object to 'set'
>   }else{
>     //set related object to unset.
>   }
> }
> 
> This isn't the "teached" way, that's for sure, but it's fast to implement
> and very easy to manage / extend.
> You can make it nice by using a configuration file outside of the code for
> properties definition, and applying a decorator to each element, but it
> would be a bit too much code to provide it here.
> 
> Good luck
> Leon
> 
> P.S. You can use a map to collect the 'representation objects' created by
> the action, which would make it also very dynamical on the backend side, and
> storing maps is something each persistence layer implementation is able of.
> 
> > -----Ursprüngliche Nachricht-----
> > Von: Scott Purcell [mailto:spurcell@vertisinc.com]
> > Gesendet: Freitag, 1. April 2005 20:05
> > An: user@struts.apache.org
> > Betreff: Form Handling Question Generic
> >
> > Hello,
> >
> > I have been building an appliction using the Struts framework
> > for a couple of weeks and am starting to get a good feel for
> > using it. So far I have been using ActionForms and am pleased
> > with them.
> >
> > As I was going over the job specification yesterday, I
> > noticed I am coming up on a large form page. Meaning lots of
> > checkboxes radios, etc. Here is what I am seeing on a page.
> > checkbox = [ ] button
> > radio () button
> > <form>
> > [ ]  NAME       () picture     [ ] new     [ ] battery
> > [ ]  NAME1     () picture     [ ] new     [ ] battery
> > [ ]  NAME2      () picture     [ ] new     [ ] battery
> > [ ]  NAME3     () picture     [ ] new     [ ] battery
> > [ ]  NAME4     () picture     [ ] new     [ ] battery
> > [ ]  NAME5     () picture     [ ] new     [ ] battery
> > etc, etc.
> >
> > Anyway, so far most of my forms have dealt with either a
> > single checkbox or a checkbox and a select list.
> >
> > So in my above question example, each line could be an object
> > with properties.
> >
> > I am trying to lay this out in some classes, etc, and create
> > a ActionForm for this. So I can capture the values as they
> > are changed. But I am getting lost in such a big form.
> >
> > Could someone possibly explain how I can accomplish this. I
> > do not need to validate, just need to update the values to a
> > db (standard workings) and populate the form from a query.
> >
> >
> > Thanks,
> > Scott
> >
> >
> >
> >
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
> 
>

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