Java Mailing List Archive

http://www.junlu.com/

Home » Home (12/2007) » Struts 2 »

[s2] newbie - setting multiple values in an action

Martin Fanta

2007-03-13

Replies:

Hi all,

I started playing around with Struts 2.0.6 on Saturday. All went well until
I've run into the need to call an Action setter for a list of strings
submitted from an html form (will switch to dates later, but I want to solve
the multiple aspect first).

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):

<form id="Process" name="Process" onsubmit="return true;"
action="/wp/Process.action" method="POST">
<table class="wwFormTable">
  <tr>
   <td class="tdLabel"><label for="Process_days_0_"
class="label">13.3.07:</label></td>
   <td><input type="text" name="days[0]" value="" id="Process_days_0_"/></td>
  </tr>
  <tr>
   <td class="tdLabel"><label for="Process_days_1_"
class="label">14.3.07:</label></td>
   <td><input type="text" name="days[1]" value="" id="Process_days_1_"/></td>
  </tr>
  <tr>
   <td colspan="2"><div align="right"><input type="submit" id="Process_0"
value="Submit"/></div></td>
  </tr>
</table>
</form>

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.

The reality is the setDays() is called twice, but in both cases, the days list
is empty (The user has filled in both text values).

What am I doing wrong? Also, I have browsed through the docs on the struts
website, but did not find anything explaining how this can be done. Can you
please point me to the right document if it exists?

Thanks in advance.
Martin


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@(protected)
For additional commands, e-mail: user-help@(protected)

©2008 junlu.com - Jax Systems, LLC, U.S.A.