You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by ancatdubher <an...@gmail.com> on 2008/04/12 19:26:39 UTC

STruts 2 - populating an ArrayList from the JSP

Hi,

My action looks like this:

class TravellerAction extends ActionSupport{
    private Plan itinerary;
    // More..
}

The class "Plan" is a travel plan for a user and contains an ArrayList of
cities you wanna visit:

class Plan {
   private ArrayList cities;
   // More..
}

Now, when a user is entering data on a JSP, he should be able to enter the
names of upto 10 cities he wants to visit. Upon submit, the JSP calls the
TravellerAction.

I managed to display the cities by doing this:

    <s:iterator value="itinerary.cities">
	<s:property/>&nbsp;
    </s:iterator>

How do I populate the cities from the JSP, back into the Action? (ie. with
10 text fields)

Thanks,
Ancat
-- 
View this message in context: http://www.nabble.com/STruts-2---populating-an-ArrayList-from-the-JSP-tp16653470p16653470.html
Sent from the Struts - User mailing list archive at Nabble.com.


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


Re: [S2]: When do I need to extend "ActionSupport"?

Posted by Wes Wannemacher <we...@wantii.com>.
On Mon, 2008-04-14 at 12:49 -0700, Dave Newton wrote:
> --- "Zheng, Xiahong" <Xi...@FMR.COM> wrote:
> > Struts 2 promotes POJO based action. However I found most of the Action
> > classes from the sample application extends ActionSupport? By looking at
> > the ActionSupport class in Xwork, I found it implements quite a few
> > interfaces, e.g. Valicateable, ValidationAware, TextProvider,
> > LocalProvider and Action. My question is if I don't extend
> > ActionSupport, do I lose all the above functionalities, validation,
> > locale, etc, even if I have the corresponding interceptors configured?
> 
> Yes (more or less; some annotations may provide functionality without
> explicit interface implementation).
> 
> The interceptors (and other functions that expect the interfaces, like I18N)
> use the interfaces as markers to indicate that a various action should be
> performed, identify the existence of functionality, and so on.
> 
> Dave
> 

I will add to what Dave says, and ask why you wouldn't want to extend
ActionSupport? Although extending ActionSupport may seem like you are
tying yourself to the framework, I have never had a problem because the
functionality that ActionSupport adds is only loosely coupled to the
framework. Perhaps a better way to say it is that I've never had to
create mock objects to unit test an action that extends ActionSupport.
So, a MyAction action = new MyAction(); String res = action.execute();
Will work in JUnit w/o any special housekeeping (except calling
appropriate setters). If you still aren't convinced, then wrap your
POJOs with a thin class that implements ActionSupport i.e. -

public class MyAction extends ActionSupport {

    private MyPOJO pojo;
    public MyPOJO getPojo() {
        return pojo;
    }
    public void setPojo( MyPOJO pojo) {
        this.pojo = pojo;
    }
}

Then you can name your form fields like - 
<s:textfield name="pojo.memberVar" />

This way your POJOs are completely de-coupled from the framework. And,
to make things easier, you can implement ModelDriven which will allow
you to deal with your POJO directly within your form widgets. I would
add a code example, but it is all well-documented in the online docs.

-Wes 


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


Re: [S2]: When do I need to extend "ActionSupport"?

Posted by Dave Newton <ne...@yahoo.com>.
--- "Zheng, Xiahong" <Xi...@FMR.COM> wrote:
> Struts 2 promotes POJO based action. However I found most of the Action
> classes from the sample application extends ActionSupport? By looking at
> the ActionSupport class in Xwork, I found it implements quite a few
> interfaces, e.g. Valicateable, ValidationAware, TextProvider,
> LocalProvider and Action. My question is if I don't extend
> ActionSupport, do I lose all the above functionalities, validation,
> locale, etc, even if I have the corresponding interceptors configured?

Yes (more or less; some annotations may provide functionality without
explicit interface implementation).

The interceptors (and other functions that expect the interfaces, like I18N)
use the interfaces as markers to indicate that a various action should be
performed, identify the existence of functionality, and so on.

Dave


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


[S2]: When do I need to extend "ActionSupport"?

Posted by "Zheng, Xiahong" <Xi...@FMR.COM>.
Struts 2 promotes POJO based action. However I found most of the Action
classes from the sample application extends ActionSupport? By looking at
the ActionSupport class in Xwork, I found it implements quite a few
interfaces, e.g. Valicateable, ValidationAware, TextProvider,
LocalProvider and Action. My question is if I don't extend
ActionSupport, do I lose all the above functionalities, validation,
locale, etc, even if I have the corresponding interceptors configured?

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


Re: STruts 2 - populating an ArrayList from the JSP

Posted by Musachy Barroso <mu...@gmail.com>.
Look at your HTML and you will see why, you need to add %{} around
#stat.index, so the name of the fields are students[0], students[1],
etc.

musachy

On Mon, Apr 14, 2008 at 6:54 PM,  <js...@altec.org> wrote:
> I am having a similar problem to Ancat:
>
>  I have an arrayList named 'students' in an action. I use this action to
>  load values for a .jsp which contains the following code:
>
>  <s:form action="Instructor/Home!saveChanges">
>
>  <s:iterator status="stat" value="students">
>    <s:textfield name="students[#stat.index].lastName" /></td>
>    <s:textfield name="students[#stat.index].firstName" /></td>
>    <s:textfield name="students[#stat.index].userName" /></td>
>    <s:textfield name="students[#stat.index].userPassword" /></td>
>    </s:iterator>
>
>  </s:form>
>
>  This displays my arrayList correctly, but when I submit to my action, I
>  cannot retrieve the modified arrayList.
>
>  Am I mistaken in thinking I simply need a getter/setter for an arrayList
>  named "students" in my receiving action?
>
>
>
>  ---------------------------------------------------------------------
>  To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>  For additional commands, e-mail: user-help@struts.apache.org
>
>



-- 
"Hey you! Would you help me to carry the stone?" Pink Floyd

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


Re: STruts 2 - populating an ArrayList from the JSP

Posted by Jukka Välimaa <va...@gmail.com>.
Hi,
You do need a getter for list. I don't think you need a setter, though.
Your textfields should have a value attribute in addition to name, to access
the getters, which I suppose you have even if you don't show them, since it
displays your arraylist correctly.  Is using #stat.index enough without
using ognl? Look at your page source. If the #stat.indexes are not replaced
by numbers, use this: %{#stat.index}.

Jukka


On Mon, Apr 14, 2008 at 9:54 PM, <js...@altec.org> wrote:

> I am having a similar problem to Ancat:
>
> I have an arrayList named 'students' in an action. I use this action to
> load values for a .jsp which contains the following code:
>
> <s:form action="Instructor/Home!saveChanges">
>
> <s:iterator status="stat" value="students">
>   <s:textfield name="students[#stat.index].lastName" /></td>
>   <s:textfield name="students[#stat.index].firstName" /></td>
>   <s:textfield name="students[#stat.index].userName" /></td>
>   <s:textfield name="students[#stat.index].userPassword" /></td>
>   </s:iterator>
>
> </s:form>
>
> This displays my arrayList correctly, but when I submit to my action, I
> cannot retrieve the modified arrayList.
>
> Am I mistaken in thinking I simply need a getter/setter for an arrayList
> named "students" in my receiving action?
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>

STruts 2 - populating an ArrayList from the JSP

Posted by js...@altec.org.
I am having a similar problem to Ancat:

I have an arrayList named 'students' in an action. I use this action to
load values for a .jsp which contains the following code:

<s:form action="Instructor/Home!saveChanges">

<s:iterator status="stat" value="students">
   <s:textfield name="students[#stat.index].lastName" /></td>
   <s:textfield name="students[#stat.index].firstName" /></td>
   <s:textfield name="students[#stat.index].userName" /></td>
   <s:textfield name="students[#stat.index].userPassword" /></td>
   </s:iterator>

</s:form>

This displays my arrayList correctly, but when I submit to my action, I
cannot retrieve the modified arrayList.

Am I mistaken in thinking I simply need a getter/setter for an arrayList
named "students" in my receiving action?

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


Re: STruts 2 - populating an ArrayList from the JSP

Posted by Jukka Välimaa <va...@gmail.com>.
Hi Ancat

like this:
<s:iterator value="itinerary.cities" status="city">
<s:textfield name="itinerary.cities[%{#city.index}]" value="[0]"
</s:iterator>

Struts should take care of populating the array when you submit the form to
the action.

On Sat, Apr 12, 2008 at 8:26 PM, ancatdubher <an...@gmail.com> wrote:

>
> Hi,
>
> My action looks like this:
>
> class TravellerAction extends ActionSupport{
>    private Plan itinerary;
>    // More..
> }
>
> The class "Plan" is a travel plan for a user and contains an ArrayList of
> cities you wanna visit:
>
> class Plan {
>   private ArrayList cities;
>   // More..
> }
>
> Now, when a user is entering data on a JSP, he should be able to enter the
> names of upto 10 cities he wants to visit. Upon submit, the JSP calls the
> TravellerAction.
>
> I managed to display the cities by doing this:
>
>    <s:iterator value="itinerary.cities">
>        <s:property/>&nbsp;
>    </s:iterator>
>
> How do I populate the cities from the JSP, back into the Action? (ie. with
> 10 text fields)
>
> Thanks,
> Ancat
> --
> View this message in context:
> http://www.nabble.com/STruts-2---populating-an-ArrayList-from-the-JSP-tp16653470p16653470.html
> Sent from the Struts - User mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>