On 3/13/07 3:45 AM, "Martin Fanta" <[EMAIL PROTECTED]> wrote:

> I have a jsp with this body:
> 
> <s:form action="Process">
> <s:iterator value="dates" status="day">
>   <s:textfield label="%{top}" labelposition="left"
> name="days[%{#day.index}]"/>
> </s:iterator>
> <s:submit/>
> </s:form>
> 
> It is meant to display a label and an edit box for each of the dates from the
> 'dates' list. The 'dates' is a list set in the "prepare" action and it is
> displayed correctly - here's the rendered html (re-formatted to make it
> readable):
>
> [SNIP] 
> 
> The ProcessAction code:
> 
> public class ProcessAction extends ActionSupport {
> 
> private List<String> days;
> 
> @Override
> public String execute() throws Exception {
>   System.out.println("ProcessAction.execute()");
>   return SUCCESS;
> }
> 
> public List<String> getDays() {
>   return days;
> }
> 
> public void setDays(List<String> days) {
>   System.out.println("ProcessWeightsAction.setDays()");
>   System.out.println(days.size());
> }
> }
> 
> What I want to achieve is let Struts call the setDays() with a List of two
> values filled in by the user in the html form.

I don't think Struts 2 will do that, at least not without using the type
conversion system. (Which I'm not familiar with.)

The issue as I see it is that you have no list for Struts 2 to set the input
against. You need to do something like the list before Struts 2 populate the
parameters from the HttpRequest:

public class ProcessAction extends ActionSupport implements Preparable {

  private List<String> days;

  public void prepare () {
    this.days = new ArrayList<String> ();
    for (int ii = 0; ii < 10; ii++) {
      this.days.add ("");
    }
  } 

  @Override
  public String execute() throws Exception {
    System.out.println("ProcessAction.execute()");
    return SUCCESS;
  }

  public List<String> getDays() {
    return days;
  }

  public void setDays(List<String> days) {
    System.out.println("ProcessWeightsAction.setDays()");
    System.out.println(days.size());
  }
}

What this does is create the list List<String> ahead of time. Then OGNL can
populate it using your name expression from your textfield tag,
name="days[%{#day.index}]", which results in days[0]. I believe OGNL will do
the following with that expression:

days will call getDays on your action.
[0] will call the set(0, <value from httprequest>) method on the List.

For a version of this technique using Maps take a look at:

    http://www.vitarara.org/cms/node/81

Later,

Mark

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to