Struts tag traverse map[to]

Source: Internet
Author: User

Go to: http://hanxin0311.iteye.com/blog/1745792

Private map<string, string> strmap = new hashmap<string, string> ();

Private map<string, person> permap = new hashmap<string, person> ();

Private map<string, string[]> strarrymap = new hashmap<string, string[]> ();

Private map<string, list<person>> perlstmap = new hashmap<string, list<person>> ();

Import java.util.ArrayList;
Import Java.util.HashMap;
Import java.util.List;
Import Java.util.Map;

Import Com.expre.struts2.bean.Person;
Import Com.expre.struts2.bean.Phone;
Import Com.opensymphony.xwork2.ActionSupport;

public class Expressmapaction extends Actionsupport {
Private static final long serialversionuid = -4251480679223607716l;

Private map<string, string> strmap = new hashmap<string, string> ();

Private map<string, person> permap = new hashmap<string, person> ();

Private map<string, string[]> strarrymap = new hashmap<string, string[]> ();

Private map<string, list<person>> perlstmap = new hashmap<string, list<person>> ();

Get&set method, which is omitted here.

@Override
Public String Execute () throws Exception {

Value is a string
Strmap.put ("First", "zxx");
Strmap.put ("Second", "LSX");
Strmap.put ("Third", "WxH");

Value is JavaBean object
person person = new Person ("001", "Zxx", 22);
Person.setphone (New Phone ("Apple", 18957157189L));

Permap.put ("one", person);

person = new Person ("002", "LSX", 25);
Person.setphone (New Phone ("HTC", 18957157187L));
Permap.put ("both", person);

Array processing
Strarrymap.put ("arr1", new string[] {"1", "310000", "Hz"});
Strarrymap.put ("arr2", new string[] {"2", "310001", "XH"});
Strarrymap.put ("Arr3", new string[] {"3", "310002", "SC"});

List Object Processing
List<person> list=new arraylist<person> ();
List.add (New Person ("001", "Zxx", 22));
List.add (New Person ("002", "LSX", 25));
Perlstmap.put ("One", list);

List=new arraylist<person> ();
List.add (New Person ("003", "WLX", 26));
List.add (New person ("004", "HZX", 28));
Perlstmap.put ("Both", list);

return "result";
}
}

<body>
<ul>
<li> visit Map:<s:property value= "Strmap"/></li>
<li> accessing an element in map:
<s:property value= "Strmap.first"/> |
<s:property value= "strmap[' second ']"/> |
<s:property value= "strmap[\ ' third\ ']"/>
</li>
<li> Access all Key:<s:property value= "Strmap.keys"/></li> in the map
<li> Access all Value:<s:property value= "Strmap.values"/></li> in the map
<li> access the size of the container:
<s:property value= "strmap.size ()"/> |
<s:property value= "Strmap.size"/>//This is a special place for maps.
</li>

<li> iterative loop values, the simplest
<s:iterator value= "Strmap" id= "entry" >
<s:property value= "#entry"/>{
Key: <s:property value= "key"/>
Value:<s:property value= "Value"/>}|
</s:iterator>
</li>

<li> Iteration Loop values
<s:iterator value= "Strmap.keyset ()" id= "key" >
<s:property value= "key"/> or <s:property value= "#key"/&GT;:
<s:if test= "strmap[#key]!=null" >
<s:property value= "Strmap.get (#key)"/> |
</s:if>
</s:iterator>
</li>

<li> iterating through array values
<s:iterator value= "Strmap" id= "entry" >
<s:property value= "#entry"/>{
<s:property value= "value[0]"/>
<s:property value= "value[1]"/> |
<s:property value= "value[2]"/>}
</s:iterator>
</li>

<li> iterate through the object values, most directly:
<ul>
<s:iterator value= "Permap" id= "entry" >
<li>
<s:property value= "#entry"/> {
Id:<s:property value= "Value.id"/>
Name:<s:property value= "Value.name"/>
Age:<s:property value= "Value.age"/>}|
</li>
</s:iterator>
</ul>
</li>

<li> iterating through the object's property values
<s:iterator value= "Permap.keyset ()" id= "key" >
<s:property value= "#key"/&GT;:
<s:iterator value= "Permap.get (#key)" >
<s:property value= "ID"/>
<s:property value= "Name"/>
<s:property value= "Phone.name"/>
</s:iterator>|
</s:iterator>
</li>

<li> slightly more complex iterations
<s:iterator value= "Perlstmap" id= "entry" >
<s:set name= "Total" value= "#entry. Value.size"/>
<s:iterator value= "#entry. Value" status= "S" >
<s:if test= "#s. First" >
<s:property value= "#entry. Key"/&GT;: Total ${total} bar
</s:if> {
<s:property value= "ID"/> |
<s:property value= "Name"/> |
<s:property value= "Age"/>
};
</s:iterator>
</s:iterator>
</li>
</ul>
</body>

Struts tag traverse map[to]

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.