Testing of different types of objects in struts2 iteration

Source: Internet
Author: User

In peacetime use S:iterator this iterative label, generally we are the same type of multiple objects into a set, then, can we put different objects to iterate? Daily work may sometimes be necessary to simplify the program, I tested it today, as if it could be completely

First step:

I set up two different Pojo classes,

public class Feng {
Private String Lou;
Private String Feng;
}
public class Fengfeng {
Private String A;
Private String B;
}

Of course, they need to provide their getter and setter methods.

Step Two:

Declare a list property in the action of STRUTS2

Private List Li;

also provides setter methods

To add different types of objects to the Li attribute in the appropriate Execute method

Feng f1=new Feng ();
F1.setfeng ("Feng");
F1.setlou ("Lou");
Feng f2=new Feng ();
F2.setfeng ("feng1");
F2.setlou ("lou1");
Fengfeng a1=new Fengfeng ();
A1.seta ("a");
A1.SETB ("B");
Fengfeng a2=new Fengfeng ();
A2.seta ("A1");
A2.setb ("B2");
Li=new ArrayList ();
Li.add (F1);
Li.add (F2);
Li.add (A1);
Li.add (A2);

The test is convenient, I have manually instantiated

Step Three:

In the page iteration, which is the most important part, I have added some judgments to the s:iterator to implement iterations of different types of objects

<s:iterator value= "Li" >
<s:if test= "Feng!=null" ><s:property value= "Feng"/>!! <s:property value= "Lou"/></s:if>
<s:if test= "A!=null" ><s:property value= "a"/>!! <s:property value= "B"/></s:if>
</s:iterator>

The page displays the following effect:

feng!! Lou feng1!! LOU1 a!! b a1!! B2

Ha ha! It worked! This will enable flexible iterations, very practical Oh!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.