You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Mike Jasnowski <mj...@bea.com> on 2003/04/17 15:44:40 UTC

RE: How to accept a form whose fields are determined at run time?

Not sure, maybe the indexed properties feature of struts would allow you to
do this?

-----Original Message-----
From: David Chelimsky [mailto:david@chelimsky.org]
Sent: Thursday, April 17, 2003 9:45 AM
To: Struts Users Mailing List
Subject: How to accept a form whose fields are determined at run time?


I'm developing an app that allows clients to create questions w/
arbitrary numbers of choices for display in forms that will appear on
their websites for their users to fill out. I need to be able to manage
groups of  fields related to one choice, but I don't know how many such
groups will be present before run time. My understanding of the HTTP
spec is that I can't rely on this approach:

<input type="hidden" name="choiceId" value="123">
<input type="text" name="choiceText">
<input type="hidden" name="choiceId" value="456">
<input type="text" name="choiceText">
<input type="numChoices" value="2">
..........
//in ActionForm
public void setChoiceId(String[] choiceId) { this.choiceId = choiceId; }
public void setChoiceId(String[] choiceText) { this.choiceText =
choiceText; }
..........
//in Action
for (int i = 0; i < numChoices; i++) {
    Choice c = new Choice();
    c.setId(form.getChoiceId()[i]);
    c.setText(form.getChoiceText()[i]);
    c.setRank(i + 1);
    choices.add(c);
}
..........
Because there's no guarantee that the arrays are correctly ordered. I
implemented something like this before using JSPs and a CustomTag (not
Struts) using this approach:

<input type="hidden" name="choiceId0" value="123">
<input type="text" name="choiceText0">
<input type="hidden" name="choiceId1" value="456">
<input type="text" name="choiceText1">
<input type="numChoices" value="2">
..........
for (int i = 0; i < numChoices; i++) {
    Choice c = new Choice();
    c.setId(request.getParameter("choiceId" + i));
    c.setText(request.getParameter("choiceText" + i));
    c.setRank(i + 1);
    choices.add(c);
}
..........
And that worked well, but I don't see how this would work in Struts
since I'm never going directly after request.getParameter(). Anybody
have any advice as to how to manage these in the ActionForm?

Thanks,
David

ps - I'm concerned with how to handle submission, not how to render the
form on the JSP.
pps - One solution that occurred to me is to modify the state of the
form via javascript so the form submitted looks like this:
    choiceId=123,456
    choiceText=firstChoice,secondChoice
and then parse the lists in my Action class. I'd prefer to avoid this
approach and keep the logic in the java layer where I can test it (and
guarantee that even javascriptless browsers can use this).



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



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


Re: How to accept a form whose fields are determined at run time?

Posted by David Chelimsky <da...@chelimsky.org>.
Of course! Thanks.

Mike Jasnowski wrote:

>Not sure, maybe the indexed properties feature of struts would allow you to
>do this?
>
>-----Original Message-----
>From: David Chelimsky [mailto:david@chelimsky.org]
>Sent: Thursday, April 17, 2003 9:45 AM
>To: Struts Users Mailing List
>Subject: How to accept a form whose fields are determined at run time?
>
>
>I'm developing an app that allows clients to create questions w/
>arbitrary numbers of choices for display in forms that will appear on
>their websites for their users to fill out. I need to be able to manage
>groups of  fields related to one choice, but I don't know how many such
>groups will be present before run time. My understanding of the HTTP
>spec is that I can't rely on this approach:
>
><input type="hidden" name="choiceId" value="123">
><input type="text" name="choiceText">
><input type="hidden" name="choiceId" value="456">
><input type="text" name="choiceText">
><input type="numChoices" value="2">
>..........
>//in ActionForm
>public void setChoiceId(String[] choiceId) { this.choiceId = choiceId; }
>public void setChoiceId(String[] choiceText) { this.choiceText =
>choiceText; }
>..........
>//in Action
>for (int i = 0; i < numChoices; i++) {
>    Choice c = new Choice();
>    c.setId(form.getChoiceId()[i]);
>    c.setText(form.getChoiceText()[i]);
>    c.setRank(i + 1);
>    choices.add(c);
>}
>..........
>Because there's no guarantee that the arrays are correctly ordered. I
>implemented something like this before using JSPs and a CustomTag (not
>Struts) using this approach:
>
><input type="hidden" name="choiceId0" value="123">
><input type="text" name="choiceText0">
><input type="hidden" name="choiceId1" value="456">
><input type="text" name="choiceText1">
><input type="numChoices" value="2">
>..........
>for (int i = 0; i < numChoices; i++) {
>    Choice c = new Choice();
>    c.setId(request.getParameter("choiceId" + i));
>    c.setText(request.getParameter("choiceText" + i));
>    c.setRank(i + 1);
>    choices.add(c);
>}
>..........
>And that worked well, but I don't see how this would work in Struts
>since I'm never going directly after request.getParameter(). Anybody
>have any advice as to how to manage these in the ActionForm?
>
>Thanks,
>David
>
>ps - I'm concerned with how to handle submission, not how to render the
>form on the JSP.
>pps - One solution that occurred to me is to modify the state of the
>form via javascript so the form submitted looks like this:
>    choiceId=123,456
>    choiceText=firstChoice,secondChoice
>and then parse the lists in my Action class. I'd prefer to avoid this
>approach and keep the logic in the java layer where I can test it (and
>guarantee that even javascriptless browsers can use this).
>
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
>For additional commands, e-mail: struts-user-help@jakarta.apache.org
>
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
>For additional commands, e-mail: struts-user-help@jakarta.apache.org
>
>
>
>
>  
>



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