You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Justin Chin <ju...@justinchin.com> on 2012/07/25 22:33:25 UTC

struts-tags Iterator usage

I am trying to update a List of Person Objects via jsp.  I have one 
Action Class used in the DispatchAction pattern.

I begin the exercise with method1 which creates a new peopleList and 
populates each Person object in the list with a firstname.

*public*String method1() *throws*Exception {

System./out/.println("Just Called method1 method");

peopleList= *new*ArrayList<Person>();

*for*(*int*i = 0; i < 10; i++) {

Person thisOne = *new*Person();

thisOne.setFirstname("FirstName"+ i);

peopleList.add(thisOne);

}

*return*/SUCCESS/;

}


I direct to JSP1.jsp and it displays fine:

<%@page language=/"java"/ contentType=/"text/html; charset=UTF-8"/ 
pageEncoding=/"UTF-8"/%>

<%@taglib prefix=/"s"/ uri=/"/struts-tags"/%>

<html>

<head>

<title>JSP1</title>

</head>

<body>

<tableborder=/"1"/ "width=/"100%"/>

<tr>

<td>_firstname_</td>

<td>_lastname_</td>

<td>number</td>

</tr>

<tr>

<s:formaction=/"method2test"/>

<s:iteratorvalue=/"peopleList"/>

<tr>

<td><s:propertyvalue=/"firstname"/ /></td>

<td><s:propertyvalue=/"lastname"/ /></td>

<td><s:propertyvalue=/"number"/ /></td>

<s:hiddenvalue=/"dn"//>

</tr>

</s:iterator>

<s:submit/>

</s:form>

</tr>

</table>

</body>

</html>

I submit from this JSP1.jsp and call method2.  Here peopleList is now 
empty. _Why doesn't JSP2.jsp submit peopleList to method2?_



Below are some of the important code snippets I believe are needed to 
help resolve my misunderstanding.

struts.xml below:

<?xmlversion=/"1.0"/ encoding=/"UTF-8"/ ?>

<!DOCTYPEstruts PUBLIC

"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"

"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>

<constantname=/"struts.enable.DynamicMethodInvocation"/ value=/"false"/ />

<constantname=/"struts.devMode"/ value=/"false"/ />

<constantname=/"struts.ui.theme"/ value=/"simple"/ />

<packagename=/"default"/ namespace=/"/"/ extends=/"struts-default"/>

<default-action-refname=/"index"/ />

<global-results>

<resultname=/"error"/>/error._jsp_</result>

</global-results>

<global-exception-mappings>

<exception-mappingexception=/"java.lang.Exception"/ result=/"error"//>

</global-exception-mappings>

<actionname=/"index"/>

<resulttype=/"redirectAction"/>

<paramname=/"actionName"/>method1test</param>

<paramname=/"namespace"/>/example</param>

</result>

</action>

</package>

<includefile=/"example.xml"//>

<!-- Add packages here -->

</struts>


example.xml below:

<?xmlversion=/"1.0"/ encoding=/"UTF-8"/ ?>

<!DOCTYPEstruts PUBLIC

"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

<packagename=/"example"/ namespace=/"/"/ extends=/"default"/>

<actionname=/"method1test"/ class=/"example.Action1"/ method=/"method1"/>

<result>/example/JSP1._jsp_</result>

</action>

<actionname=/"method2test"/ class=/"example.Action1"/method=/"method2"/>

<resultname=/"input"/>/example/JSP1._jsp_</result>

<result>/example/JSP2._jsp_</result>

</action>

<actionname=/"method3test"/ class=/"example.Action1"/method=/"method3"/>

<resultname=/"input"/>/example/JSP2._jsp_</result>

<result>/example/JSP3._jsp_</result>

</action>

</package>

</struts>

Action1.java

*package*example;

*import*java.util.ArrayList;

*import*java.util.List;

*import*com.opensymphony.xwork2.ActionSupport;

*public**class*Action1 *extends*ActionSupport {

*private**static**final**long*/serialVersionUID/= -8528061245644461238L;

*private*List<Person> peopleList;

*public*List<Person> getPeopleList() {

*return*peopleList;

}

*public**void*setPeopleList(List<Person> peopleList) {

*this*.peopleList= peopleList;

}

*public*String method1() *throws*Exception {

System./out/.println("Just Called method1 method");

peopleList= *new*ArrayList<Person>();

*for*(*int*i = 0; i < 10; i++) {

Person thisOne = *new*Person();

thisOne.setFirstname("FirstName"+ i);

peopleList.add(thisOne);

}

*return*/SUCCESS/;

}

*public*String method2() *throws*Exception {

System./out/.println("Just Called method2 method");

System./out/.println("Is peopleList null?"+ peopleList== *null*);

*if*(peopleList!= *null*) {

List<Person> newPeopleList = *new*ArrayList<Person>();

*for*(*int*i = 0; peopleList!= *null*&& i < peopleList.size(); i++) {

Person thisOne = peopleList.get(i);

thisOne.setLastname("LastName"+ i);

newPeopleList.add(thisOne);

}

peopleList.clear();

peopleList.addAll(newPeopleList);

}

*return*/SUCCESS/;

}

}

Person.java:

*package*example;

*public**class*Person {

String firstname, lastname, number, dn;

*public*String getDn() {

*return*dn;

}

*public**void*setDn(String dn) {

*this*.dn= dn;

}

*public*String getFirstname() {

*return*firstname;

}

*public**void*setFirstname(String firstname) {

*this*.firstname= firstname;

}

*public*String getLastname() {

*return*lastname;

}

*public**void*setLastname(String lastname) {

*this*.lastname= lastname;

}

*public*String getNumber() {

*return*number;

}

*public**void*setNumber(String number) {

*this*.number= number;

}

}



JSP1.jsp and JSP2.jsp are identical except for 
<s:formaction=/"method2test"/>, which simple calls method1test and 
method2test respectively:

<%@page language=/"java"/ contentType=/"text/html; charset=UTF-8"/ 
pageEncoding=/"UTF-8"/%>

<%@taglib prefix=/"s"/ uri=/"/struts-tags"/%>

<html>

<head>

<title>JSP1</title>

</head>

<body>

<tableborder=/"1"/ "width=/"100%"/>

<tr>

<td>firstname</td>

<td>lastname</td>

<td>number</td>

</tr>

<tr>

<s:formaction=/"method2test"/>

<s:iteratorvalue=/"peopleList"/>

<tr>

<td><s:propertyvalue=/"firstname"/ /></td>

<td><s:propertyvalue=/"lastname"/ /></td>

<td><s:propertyvalue=/"number"/ /></td>

<s:hiddenvalue=/"dn"//>

</tr>

</s:iterator>

<s:submit/>

</s:form>

</tr>

</table>

</body>

</html>







RE: Open session in view (JPA, struts2, Spring 3, Hibernate4)

Posted by Martin Gainty <mg...@hotmail.com>.
How do you want Spring to find the Action?
autowire by name?
autowire by type?

IF you want Spring to create the Action then...

However, sometimes you might want the bean to be completely managed by 
Spring. 
(This is useful, for example, if you wish to apply more complex AOP or 
Spring-enabled technologies, such as Acegi, to your beans.)
To do this, all you 
have to do is configure the bean in your Spring applicationContext.xml 
and then 
change the class attribute from your Action in the 
struts.xml to use the bean name defined in Spring instead of the class 
name.
Your struts.xml file would then have the Action class attributes 
changed..as defined here


struts.xml
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
    <include file="struts-default.xml"/>

    <package name="default" extends="struts-default">
        <action name="foo" class="com.acme.Foo">
            <result>foo.ftl</result>
        </action>
    </package>

    <package name="secure" namespace="/secure" extends="default">
        <action name="bar" class="bar">
            <result>bar.ftl</result>
        </action>
    </package>
</struts>
Martin 
______________________________________________ 
Verzicht und Vertraulichkeitanmerkung/Note de déni et de confidentialité

Diese Nachricht ist vertraulich. Sollten Sie nicht der vorgesehene Empfaenger sein, so bitten wir hoeflich um eine Mitteilung. Jede unbefugte Weiterleitung oder Fertigung einer Kopie ist unzulaessig. Diese Nachricht dient lediglich dem Austausch von Informationen und entfaltet keine rechtliche Bindungswirkung. Aufgrund der leichten Manipulierbarkeit von E-Mails koennen wir keine Haftung fuer den Inhalt uebernehmen.
Ce message est confidentiel et peut être privilégié. Si vous n'êtes pas le destinataire prévu, nous te demandons avec bonté que pour satisfaire informez l'expéditeur. N'importe quelle diffusion non autorisée ou la copie de ceci est interdite. Ce message sert à l'information seulement et n'aura pas n'importe quel effet légalement obligatoire. Étant donné que les email peuvent facilement être sujets à la manipulation, nous ne pouvons accepter aucune responsabilité pour le contenu fourni.


> Date: Mon, 20 Aug 2012 14:02:36 +0200
> Subject: Re: Open session in view (JPA, struts2, Spring 3, Hibernate4)
> From: lukaszlenart@apache.org
> To: user@struts.apache.org
> 
> 2012/8/9 Mounir Benzid <mb...@meetingmasters.de>:
> > sorry for the late response. web.xml ---> http://pastebin.com/XPiQ3ZJB
> >
> > I guess the problem might be that the transaction boundary starts and ends
> > within the controller class I wrote to manage entities. The action itself
> > doesn't participate in the transaction. If my understanding is correct then
> > the OSIV pattern needs an active transaction. Should I let spring manage the
> > action in order to propagate the transaction to it?
> 
> Why not to extend OSIV to /* ?
> 
> 
> Regards
> -- 
> Łukasz
> + 48 606 323 122 http://www.lenart.org.pl/
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
> 
 		 	   		  

Re: Open session in view (JPA, struts2, Spring 3, Hibernate4)

Posted by Lukasz Lenart <lu...@apache.org>.
2012/8/9 Mounir Benzid <mb...@meetingmasters.de>:
> sorry for the late response. web.xml ---> http://pastebin.com/XPiQ3ZJB
>
> I guess the problem might be that the transaction boundary starts and ends
> within the controller class I wrote to manage entities. The action itself
> doesn't participate in the transaction. If my understanding is correct then
> the OSIV pattern needs an active transaction. Should I let spring manage the
> action in order to propagate the transaction to it?

Why not to extend OSIV to /* ?


Regards
-- 
Łukasz
+ 48 606 323 122 http://www.lenart.org.pl/

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


RE: Open session in view (JPA, struts2, Spring 3, Hibernate4)

Posted by Martin Gainty <mg...@hotmail.com>.


HTTP Session captures dynamic Session variables from Session initialisation to Session invalidation or timeout
a Database Transaction captures database information from Start to (commit or rollback)
Is there a reason why you would want your Session to be disconnected from the Database Transaction?

Martin 
______________________________________________ 
Jogi és Bizalmassági kinyilatkoztatás/Verzicht und Vertraulichkeitanmerkung/Note de déni et de confidentialité
 Ez az
üzenet bizalmas.  Ha nem ön az akinek szánva volt, akkor kérjük, hogy
jelentse azt nekünk vissza. Semmiféle továbbítása vagy másolatának
készítése nem megengedett.  Ez az üzenet csak ismeret cserét szolgál és
semmiféle jogi alkalmazhatósága sincs.  Mivel az electronikus üzenetek
könnyen megváltoztathatóak, ezért minket semmi felelöség nem terhelhet
ezen üzenet tartalma miatt.

Diese Nachricht ist vertraulich. Sollten Sie nicht der vorgesehene Empfaenger sein, so bitten wir hoeflich um eine Mitteilung. Jede unbefugte Weiterleitung oder Fertigung einer Kopie ist unzulaessig. Diese Nachricht dient lediglich dem Austausch von Informationen und entfaltet keine rechtliche Bindungswirkung. Aufgrund der leichten Manipulierbarkeit von E-Mails koennen wir keine Haftung fuer den Inhalt uebernehmen.
Ce message est confidentiel et peut être privilégié. Si vous n'êtes pas le destinataire prévu, nous te demandons avec bonté que pour satisfaire informez l'expéditeur. N'importe quelle diffusion non autorisée ou la copie de ceci est interdite. Ce message sert à l'information seulement et n'aura pas n'importe quel effet légalement obligatoire. Étant donné que les email peuvent facilement être sujets à la manipulation, nous ne pouvons accepter aucune responsabilité pour le contenu fourni.


> Date: Thu, 9 Aug 2012 09:20:30 +0200
> From: mb@meetingmasters.de
> To: user@struts.apache.org
> Subject: Re: Open session in view (JPA, struts2, Spring 3, Hibernate4)
> 
> Am 7/26/2012 4:39 PM, schrieb Maurizio Cucchiara:
> > Did you try to extend the url mapping to the whole root?
> >
> > f.e.:  <url-pattern>/*</url-**pattern>
> >
> >
> > Twitter     :http://www.twitter.com/m_cucchiara
> > G+          :https://plus.google.com/107903711540963855921
> > Linkedin    :http://www.linkedin.com/in/mauriziocucchiara
> >
> > Maurizio Cucchiara
> >
> >
> > On 26 July 2012 15:36, Łukasz Lenart <lu...@googlemail.com> wrote:
> >
> >> Could you show the whole web.xml ?
> >>
> >>
> >> Regards
> >> --
> >> Łukasz
> >> mobile +48 606 323 122 http://www.lenart.org.pl/
> >> Warszawa JUG conference - Confitura http://confitura.pl/
> >>
> >> ---------------------------------------------------------------------
> >> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> >> For additional commands, e-mail: user-help@struts.apache.org
> >>
> >>
> >
> 
> Łukasz, Maurizio
> 
> sorry for the late response. web.xml ---> http://pastebin.com/XPiQ3ZJB
> 
> I guess the problem might be that the transaction boundary starts and ends within the controller class I wrote to manage entities. The action itself doesn't participate in the transaction. If my understanding is correct then the OSIV pattern needs an active transaction. Should I let spring manage the action in order to propagate the transaction to it?
> 
> Mounir
> 
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
> 
 		 	   		  

Re: Open session in view (JPA, struts2, Spring 3, Hibernate4)

Posted by Mounir Benzid <mb...@meetingmasters.de>.
Am 7/26/2012 4:39 PM, schrieb Maurizio Cucchiara:
> Did you try to extend the url mapping to the whole root?
>
> f.e.:  <url-pattern>/*</url-**pattern>
>
>
> Twitter     :http://www.twitter.com/m_cucchiara
> G+          :https://plus.google.com/107903711540963855921
> Linkedin    :http://www.linkedin.com/in/mauriziocucchiara
>
> Maurizio Cucchiara
>
>
> On 26 July 2012 15:36, Łukasz Lenart <lu...@googlemail.com> wrote:
>
>> Could you show the whole web.xml ?
>>
>>
>> Regards
>> --
>> Łukasz
>> mobile +48 606 323 122 http://www.lenart.org.pl/
>> Warszawa JUG conference - Confitura http://confitura.pl/
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>> For additional commands, e-mail: user-help@struts.apache.org
>>
>>
>

Łukasz, Maurizio

sorry for the late response. web.xml ---> http://pastebin.com/XPiQ3ZJB

I guess the problem might be that the transaction boundary starts and ends within the controller class I wrote to manage entities. The action itself doesn't participate in the transaction. If my understanding is correct then the OSIV pattern needs an active transaction. Should I let spring manage the action in order to propagate the transaction to it?

Mounir




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


Re: Open session in view (JPA, struts2, Spring 3, Hibernate4)

Posted by Maurizio Cucchiara <mc...@apache.org>.
Did you try to extend the url mapping to the whole root?

f.e.:  <url-pattern>/*</url-**pattern>


Twitter     :http://www.twitter.com/m_cucchiara
G+          :https://plus.google.com/107903711540963855921
Linkedin    :http://www.linkedin.com/in/mauriziocucchiara

Maurizio Cucchiara


On 26 July 2012 15:36, Łukasz Lenart <lu...@googlemail.com> wrote:

> Could you show the whole web.xml ?
>
>
> Regards
> --
> Łukasz
> mobile +48 606 323 122 http://www.lenart.org.pl/
> Warszawa JUG conference - Confitura http://confitura.pl/
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>

Re: Open session in view (JPA, struts2, Spring 3, Hibernate4)

Posted by Łukasz Lenart <lu...@googlemail.com>.
Could you show the whole web.xml ?


Regards
-- 
Łukasz
mobile +48 606 323 122 http://www.lenart.org.pl/
Warszawa JUG conference - Confitura http://confitura.pl/

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


Open session in view (JPA, struts2, Spring 3, Hibernate4)

Posted by Mounir Benzid <mb...@meetingmasters.de>.
Because of a "limitation" (which is rather a bug) of Hibernate 4, I need 
to switch to an open-session-in-view pattern in struts2


I was hoping that setting the right spring configuration in my web.xml 
would do the trick (see below)

The session still dies before the view layer (ognl) has any chance to 
lazy fetch from the database.

Is there anything struts-specific to make OSIV work?


I use Spring 3 and the EntityManager is from my JPA provider (hibernate).

- Mounir



   <filter>
<filter-name>OpenEntityManagerInViewFilter</filter-name>
         <filter-class>
org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter
         </filter-class>
         <init-param>
<param-name>entityManagerFactory</param-name>
<param-value>entityManagerFactory</param-value>
         </init-param>
     </filter>
     <filter-mapping>
<filter-name>OpenEntityManagerInViewFilter</filter-name>
         <url-pattern>/public/*</url-pattern>
     </filter-mapping>

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


Re: struts-tags Iterator usage

Posted by Justin Chin <ju...@justinchin.com>.
Understood.  I was not thinking about the conversion to HTML on the 
client side.  Let me work with the suggestion to get my HTML syntax 
correct for submission.

Thank you.

On 7/25/2012 4:59 PM, Dave Newton wrote:
> On Wed, Jul 25, 2012 at 4:56 PM, Justin Chin wrote:
>
>> I am not sure I understand.  The jsp does the job of displaying the data
>> correctly, but I must do additional work to submit the data by adding
>> additional markup?:
>>
> How would a framework know that an arbitrary number of fields named
> "firstname", "lastname", and "number" should coalesce into a list of
> specific object types?
>
> All you submit in HTTP is strings: everything else is smoke and mirrors,
> and relies completely on convention (e.g., form field names) and conversion
> based on names and property information.
>
> Dave
>


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


Re: struts-tags Iterator usage

Posted by Dave Newton <da...@gmail.com>.
On Wed, Jul 25, 2012 at 4:56 PM, Justin Chin wrote:

> I am not sure I understand.  The jsp does the job of displaying the data
> correctly, but I must do additional work to submit the data by adding
> additional markup?:
>

How would a framework know that an arbitrary number of fields named
"firstname", "lastname", and "number" should coalesce into a list of
specific object types?

All you submit in HTTP is strings: everything else is smoke and mirrors,
and relies completely on convention (e.g., form field names) and conversion
based on names and property information.

Dave

Re: struts-tags Iterator usage

Posted by Justin Chin <ju...@justinchin.com>.
Apologies.  I understand now that text only is allowed.

I am not sure I understand.  The jsp does the job of displaying the data 
correctly, but I must do additional work to submit the data by adding 
additional markup?:

         <s:form action="method2test">
             <s:iterator value="peopleList">
                 <tr>
                     <td><s:property value="firstname" /></td>
                     <td><s:property value="lastname" /></td>
                     <td><s:property value="number" /></td>
                     <s:hidden value="dn"/>
                 </tr>
             </s:iterator>
             <s:submit/>
         </s:form>

Thanks.

On 7/25/2012 4:37 PM, Dave Newton wrote:
> Totally illegible.
>
> In any case, if you want to "submit a list", you need to name your form
> fields using array notation, or at the very least, using object.property
> notation, e.g., peopleList.firstName.
>
> Otherwise you just have a bunch of fields named "firstName"--at best you'd
> get separate lists of names and numbers.
>
> Dave
>
> On Wed, Jul 25, 2012 at 4:33 PM, Justin Chin <ju...@justinchin.com> wrote:
>
>> I am trying to update a List of Person Objects via jsp.  I have one Action
>> Class used in the DispatchAction pattern.
>>
>> I begin the exercise with method1 which creates a new peopleList and
>> populates each Person object in the list with a firstname.
>>
>> *public*String method1() *throws*Exception {
>>
>> System./out/.println("Just Called method1 method");
>>
>> peopleList= *new*ArrayList<Person>();
>>
>> *for*(*int*i = 0; i < 10; i++) {
>>
>> Person thisOne = *new*Person();
>>
>> thisOne.setFirstname("**FirstName"+ i);
>>
>> peopleList.add(thisOne);
>>
>> }
>>
>> *return*/SUCCESS/;
>>
>> }
>>
>>
>> I direct to JSP1.jsp and it displays fine:
>>
>> <%@page language=/"java"/ contentType=/"text/html; charset=UTF-8"/
>> pageEncoding=/"UTF-8"/%>
>>
>> <%@taglib prefix=/"s"/ uri=/"/struts-tags"/%>
>>
>> <html>
>>
>> <head>
>>
>> <title>JSP1</title>
>>
>> </head>
>>
>> <body>
>>
>> <tableborder=/"1"/ "width=/"100%"/>
>>
>> <tr>
>>
>> <td>_firstname_</td>
>>
>> <td>_lastname_</td>
>>
>> <td>number</td>
>>
>> </tr>
>>
>> <tr>
>>
>> <s:formaction=/"method2test"/>
>>
>> <s:iteratorvalue=/"peopleList"**/>
>>
>> <tr>
>>
>> <td><s:propertyvalue=/"**firstname"/ /></td>
>>
>> <td><s:propertyvalue=/"**lastname"/ /></td>
>>
>> <td><s:propertyvalue=/"number"**/ /></td>
>>
>> <s:hiddenvalue=/"dn"//>
>>
>> </tr>
>>
>> </s:iterator>
>>
>> <s:submit/>
>>
>> </s:form>
>>
>> </tr>
>>
>> </table>
>>
>> </body>
>>
>> </html>
>>
>> I submit from this JSP1.jsp and call method2.  Here peopleList is now
>> empty. _Why doesn't JSP2.jsp submit peopleList to method2?_
>>
>>
>>
>> Below are some of the important code snippets I believe are needed to help
>> resolve my misunderstanding.
>>
>> struts.xml below:
>>
>> <?xmlversion=/"1.0"/ encoding=/"UTF-8"/ ?>
>>
>> <!DOCTYPEstruts PUBLIC
>>
>> "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
>>
>> "http://struts.apache.org/**dtds/struts-2.3.dtd<http://struts.apache.org/dtds/struts-2.3.dtd>
>> ">
>>
>> <struts>
>>
>> <constantname=/"struts.enable.**DynamicMethodInvocation"/ value=/"false"/
>> />
>>
>> <constantname=/"struts.**devMode"/ value=/"false"/ />
>>
>> <constantname=/"struts.ui.**theme"/ value=/"simple"/ />
>>
>> <packagename=/"default"/ namespace=/"/"/ extends=/"struts-default"/>
>>
>> <default-action-refname=/"**index"/ />
>>
>> <global-results>
>>
>> <resultname=/"error"/>/error._**jsp_</result>
>>
>> </global-results>
>>
>> <global-exception-mappings>
>>
>> <exception-mappingexception=/"**java.lang.Exception"/ result=/"error"//>
>>
>> </global-exception-mappings>
>>
>> <actionname=/"index"/>
>>
>> <resulttype=/"redirectAction"/**>
>>
>> <paramname=/"actionName"/>**method1test</param>
>>
>> <paramname=/"namespace"/>/**example</param>
>>
>> </result>
>>
>> </action>
>>
>> </package>
>>
>> <includefile=/"example.xml"//>
>>
>> <!-- Add packages here -->
>>
>> </struts>
>>
>>
>> example.xml below:
>>
>> <?xmlversion=/"1.0"/ encoding=/"UTF-8"/ ?>
>>
>> <!DOCTYPEstruts PUBLIC
>>
>> "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
>>
>> "http://struts.apache.org/**dtds/struts-2.0.dtd<http://struts.apache.org/dtds/struts-2.0.dtd>
>> ">
>>
>> <struts>
>>
>> <packagename=/"example"/ namespace=/"/"/ extends=/"default"/>
>>
>> <actionname=/"method1test"/ class=/"example.Action1"/ method=/"method1"/>
>>
>> <result>/example/JSP1._jsp_</**result>
>>
>> </action>
>>
>> <actionname=/"method2test"/ class=/"example.Action1"/**method=/"method2"/>
>>
>> <resultname=/"input"/>/**example/JSP1._jsp_</result>
>>
>> <result>/example/JSP2._jsp_</**result>
>>
>> </action>
>>
>> <actionname=/"method3test"/ class=/"example.Action1"/**method=/"method3"/>
>>
>> <resultname=/"input"/>/**example/JSP2._jsp_</result>
>>
>> <result>/example/JSP3._jsp_</**result>
>>
>> </action>
>>
>> </package>
>>
>> </struts>
>>
>> Action1.java
>>
>> *package*example;
>>
>> *import*java.util.ArrayList;
>>
>> *import*java.util.List;
>>
>> *import*com.opensymphony.**xwork2.ActionSupport;
>>
>> *public**class*Action1 *extends*ActionSupport {
>>
>> *private**static**final**long***/serialVersionUID/= -8528061245644461238L;
>>
>> *private*List<Person> peopleList;
>>
>> *public*List<Person> getPeopleList() {
>>
>> *return*peopleList;
>>
>> }
>>
>> *public**void*setPeopleList(**List<Person> peopleList) {
>>
>> *this*.peopleList= peopleList;
>>
>> }
>>
>> *public*String method1() *throws*Exception {
>>
>> System./out/.println("Just Called method1 method");
>>
>> peopleList= *new*ArrayList<Person>();
>>
>> *for*(*int*i = 0; i < 10; i++) {
>>
>> Person thisOne = *new*Person();
>>
>> thisOne.setFirstname("**FirstName"+ i);
>>
>> peopleList.add(thisOne);
>>
>> }
>>
>> *return*/SUCCESS/;
>>
>> }
>>
>> *public*String method2() *throws*Exception {
>>
>> System./out/.println("Just Called method2 method");
>>
>> System./out/.println("Is peopleList null?"+ peopleList== *null*);
>>
>> *if*(peopleList!= *null*) {
>>
>> List<Person> newPeopleList = *new*ArrayList<Person>();
>>
>> *for*(*int*i = 0; peopleList!= *null*&& i < peopleList.size(); i++) {
>>
>> Person thisOne = peopleList.get(i);
>>
>> thisOne.setLastname("LastName"**+ i);
>>
>> newPeopleList.add(thisOne);
>>
>> }
>>
>> peopleList.clear();
>>
>> peopleList.addAll(**newPeopleList);
>>
>> }
>>
>> *return*/SUCCESS/;
>>
>> }
>>
>> }
>>
>> Person.java:
>>
>> *package*example;
>>
>> *public**class*Person {
>>
>> String firstname, lastname, number, dn;
>>
>> *public*String getDn() {
>>
>> *return*dn;
>>
>> }
>>
>> *public**void*setDn(String dn) {
>>
>> *this*.dn= dn;
>>
>> }
>>
>> *public*String getFirstname() {
>>
>> *return*firstname;
>>
>> }
>>
>> *public**void*setFirstname(**String firstname) {
>>
>> *this*.firstname= firstname;
>>
>> }
>>
>> *public*String getLastname() {
>>
>> *return*lastname;
>>
>> }
>>
>> *public**void*setLastname(**String lastname) {
>>
>> *this*.lastname= lastname;
>>
>> }
>>
>> *public*String getNumber() {
>>
>> *return*number;
>>
>> }
>>
>> *public**void*setNumber(String number) {
>>
>> *this*.number= number;
>>
>> }
>>
>> }
>>
>>
>>
>> JSP1.jsp and JSP2.jsp are identical except for
>> <s:formaction=/"method2test"/>**, which simple calls method1test and
>> method2test respectively:
>>
>> <%@page language=/"java"/ contentType=/"text/html; charset=UTF-8"/
>> pageEncoding=/"UTF-8"/%>
>>
>> <%@taglib prefix=/"s"/ uri=/"/struts-tags"/%>
>>
>> <html>
>>
>> <head>
>>
>> <title>JSP1</title>
>>
>> </head>
>>
>> <body>
>>
>> <tableborder=/"1"/ "width=/"100%"/>
>>
>> <tr>
>>
>> <td>firstname</td>
>>
>> <td>lastname</td>
>>
>> <td>number</td>
>>
>> </tr>
>>
>> <tr>
>>
>> <s:formaction=/"method2test"/>
>>
>> <s:iteratorvalue=/"peopleList"**/>
>>
>> <tr>
>>
>> <td><s:propertyvalue=/"**firstname"/ /></td>
>>
>> <td><s:propertyvalue=/"**lastname"/ /></td>
>>
>> <td><s:propertyvalue=/"number"**/ /></td>
>>
>> <s:hiddenvalue=/"dn"//>
>>
>> </tr>
>>
>> </s:iterator>
>>
>> <s:submit/>
>>
>> </s:form>
>>
>> </tr>
>>
>> </table>
>>
>> </body>
>>
>> </html>
>>
>>
>>
>>
>>
>>
>>


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


Re: struts-tags Iterator usage

Posted by Dave Newton <da...@gmail.com>.
Totally illegible.

In any case, if you want to "submit a list", you need to name your form
fields using array notation, or at the very least, using object.property
notation, e.g., peopleList.firstName.

Otherwise you just have a bunch of fields named "firstName"--at best you'd
get separate lists of names and numbers.

Dave

On Wed, Jul 25, 2012 at 4:33 PM, Justin Chin <ju...@justinchin.com> wrote:

> I am trying to update a List of Person Objects via jsp.  I have one Action
> Class used in the DispatchAction pattern.
>
> I begin the exercise with method1 which creates a new peopleList and
> populates each Person object in the list with a firstname.
>
> *public*String method1() *throws*Exception {
>
> System./out/.println("Just Called method1 method");
>
> peopleList= *new*ArrayList<Person>();
>
> *for*(*int*i = 0; i < 10; i++) {
>
> Person thisOne = *new*Person();
>
> thisOne.setFirstname("**FirstName"+ i);
>
> peopleList.add(thisOne);
>
> }
>
> *return*/SUCCESS/;
>
> }
>
>
> I direct to JSP1.jsp and it displays fine:
>
> <%@page language=/"java"/ contentType=/"text/html; charset=UTF-8"/
> pageEncoding=/"UTF-8"/%>
>
> <%@taglib prefix=/"s"/ uri=/"/struts-tags"/%>
>
> <html>
>
> <head>
>
> <title>JSP1</title>
>
> </head>
>
> <body>
>
> <tableborder=/"1"/ "width=/"100%"/>
>
> <tr>
>
> <td>_firstname_</td>
>
> <td>_lastname_</td>
>
> <td>number</td>
>
> </tr>
>
> <tr>
>
> <s:formaction=/"method2test"/>
>
> <s:iteratorvalue=/"peopleList"**/>
>
> <tr>
>
> <td><s:propertyvalue=/"**firstname"/ /></td>
>
> <td><s:propertyvalue=/"**lastname"/ /></td>
>
> <td><s:propertyvalue=/"number"**/ /></td>
>
> <s:hiddenvalue=/"dn"//>
>
> </tr>
>
> </s:iterator>
>
> <s:submit/>
>
> </s:form>
>
> </tr>
>
> </table>
>
> </body>
>
> </html>
>
> I submit from this JSP1.jsp and call method2.  Here peopleList is now
> empty. _Why doesn't JSP2.jsp submit peopleList to method2?_
>
>
>
> Below are some of the important code snippets I believe are needed to help
> resolve my misunderstanding.
>
> struts.xml below:
>
> <?xmlversion=/"1.0"/ encoding=/"UTF-8"/ ?>
>
> <!DOCTYPEstruts PUBLIC
>
> "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
>
> "http://struts.apache.org/**dtds/struts-2.3.dtd<http://struts.apache.org/dtds/struts-2.3.dtd>
> ">
>
> <struts>
>
> <constantname=/"struts.enable.**DynamicMethodInvocation"/ value=/"false"/
> />
>
> <constantname=/"struts.**devMode"/ value=/"false"/ />
>
> <constantname=/"struts.ui.**theme"/ value=/"simple"/ />
>
> <packagename=/"default"/ namespace=/"/"/ extends=/"struts-default"/>
>
> <default-action-refname=/"**index"/ />
>
> <global-results>
>
> <resultname=/"error"/>/error._**jsp_</result>
>
> </global-results>
>
> <global-exception-mappings>
>
> <exception-mappingexception=/"**java.lang.Exception"/ result=/"error"//>
>
> </global-exception-mappings>
>
> <actionname=/"index"/>
>
> <resulttype=/"redirectAction"/**>
>
> <paramname=/"actionName"/>**method1test</param>
>
> <paramname=/"namespace"/>/**example</param>
>
> </result>
>
> </action>
>
> </package>
>
> <includefile=/"example.xml"//>
>
> <!-- Add packages here -->
>
> </struts>
>
>
> example.xml below:
>
> <?xmlversion=/"1.0"/ encoding=/"UTF-8"/ ?>
>
> <!DOCTYPEstruts PUBLIC
>
> "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
>
> "http://struts.apache.org/**dtds/struts-2.0.dtd<http://struts.apache.org/dtds/struts-2.0.dtd>
> ">
>
> <struts>
>
> <packagename=/"example"/ namespace=/"/"/ extends=/"default"/>
>
> <actionname=/"method1test"/ class=/"example.Action1"/ method=/"method1"/>
>
> <result>/example/JSP1._jsp_</**result>
>
> </action>
>
> <actionname=/"method2test"/ class=/"example.Action1"/**method=/"method2"/>
>
> <resultname=/"input"/>/**example/JSP1._jsp_</result>
>
> <result>/example/JSP2._jsp_</**result>
>
> </action>
>
> <actionname=/"method3test"/ class=/"example.Action1"/**method=/"method3"/>
>
> <resultname=/"input"/>/**example/JSP2._jsp_</result>
>
> <result>/example/JSP3._jsp_</**result>
>
> </action>
>
> </package>
>
> </struts>
>
> Action1.java
>
> *package*example;
>
> *import*java.util.ArrayList;
>
> *import*java.util.List;
>
> *import*com.opensymphony.**xwork2.ActionSupport;
>
> *public**class*Action1 *extends*ActionSupport {
>
> *private**static**final**long***/serialVersionUID/= -8528061245644461238L;
>
> *private*List<Person> peopleList;
>
> *public*List<Person> getPeopleList() {
>
> *return*peopleList;
>
> }
>
> *public**void*setPeopleList(**List<Person> peopleList) {
>
> *this*.peopleList= peopleList;
>
> }
>
> *public*String method1() *throws*Exception {
>
> System./out/.println("Just Called method1 method");
>
> peopleList= *new*ArrayList<Person>();
>
> *for*(*int*i = 0; i < 10; i++) {
>
> Person thisOne = *new*Person();
>
> thisOne.setFirstname("**FirstName"+ i);
>
> peopleList.add(thisOne);
>
> }
>
> *return*/SUCCESS/;
>
> }
>
> *public*String method2() *throws*Exception {
>
> System./out/.println("Just Called method2 method");
>
> System./out/.println("Is peopleList null?"+ peopleList== *null*);
>
> *if*(peopleList!= *null*) {
>
> List<Person> newPeopleList = *new*ArrayList<Person>();
>
> *for*(*int*i = 0; peopleList!= *null*&& i < peopleList.size(); i++) {
>
> Person thisOne = peopleList.get(i);
>
> thisOne.setLastname("LastName"**+ i);
>
> newPeopleList.add(thisOne);
>
> }
>
> peopleList.clear();
>
> peopleList.addAll(**newPeopleList);
>
> }
>
> *return*/SUCCESS/;
>
> }
>
> }
>
> Person.java:
>
> *package*example;
>
> *public**class*Person {
>
> String firstname, lastname, number, dn;
>
> *public*String getDn() {
>
> *return*dn;
>
> }
>
> *public**void*setDn(String dn) {
>
> *this*.dn= dn;
>
> }
>
> *public*String getFirstname() {
>
> *return*firstname;
>
> }
>
> *public**void*setFirstname(**String firstname) {
>
> *this*.firstname= firstname;
>
> }
>
> *public*String getLastname() {
>
> *return*lastname;
>
> }
>
> *public**void*setLastname(**String lastname) {
>
> *this*.lastname= lastname;
>
> }
>
> *public*String getNumber() {
>
> *return*number;
>
> }
>
> *public**void*setNumber(String number) {
>
> *this*.number= number;
>
> }
>
> }
>
>
>
> JSP1.jsp and JSP2.jsp are identical except for
> <s:formaction=/"method2test"/>**, which simple calls method1test and
> method2test respectively:
>
> <%@page language=/"java"/ contentType=/"text/html; charset=UTF-8"/
> pageEncoding=/"UTF-8"/%>
>
> <%@taglib prefix=/"s"/ uri=/"/struts-tags"/%>
>
> <html>
>
> <head>
>
> <title>JSP1</title>
>
> </head>
>
> <body>
>
> <tableborder=/"1"/ "width=/"100%"/>
>
> <tr>
>
> <td>firstname</td>
>
> <td>lastname</td>
>
> <td>number</td>
>
> </tr>
>
> <tr>
>
> <s:formaction=/"method2test"/>
>
> <s:iteratorvalue=/"peopleList"**/>
>
> <tr>
>
> <td><s:propertyvalue=/"**firstname"/ /></td>
>
> <td><s:propertyvalue=/"**lastname"/ /></td>
>
> <td><s:propertyvalue=/"number"**/ /></td>
>
> <s:hiddenvalue=/"dn"//>
>
> </tr>
>
> </s:iterator>
>
> <s:submit/>
>
> </s:form>
>
> </tr>
>
> </table>
>
> </body>
>
> </html>
>
>
>
>
>
>
>