4、普通属性
public String execute(){
username = "zhangsan";
password = "123";
}
<s:property value="username"/><br>
<s:property value="password"/><br>
5、自定义对象
public String execute(){
userInfo = new UserInfo();
userInfo.setUsername("hhh");
userInfo.setPassword("111");
}
<s:property value="userInfo.username"/><br>
<s:property value="userInfo.password"/><br>
6、List(普通属性)
public String execute(){
list = new ArrayList<String>();
list.add("aaa");
list.add("bbb");
list.add("ccc");
}
<s:iterator value="list" var="var">
<s:property value="var"/><br>
</s:iterator>
或者:
<s:iterator value="list">
<s:property/><br>
</s:iterator>
6、List(自定义对象属性)
public String execute(){
list = new ArrayList<UserInfo>();
UserInfo userInfo = new UserInfo();
userInfo.setUsername("u1");
userInfo.setPassword("p1");
userInfo.setRoleInfo(new RoleInfo("r1"));
list.add(userInfo);
userInfo = new UserInfo();
userInfo.setUsername("u2");
userInfo.setPassword("p2");
userInfo.setRoleInfo(new RoleInfo("r2"));
list.add(userInfo);
}
<s:iterator value="list" var="var">
<s:property value="#var.username"/>
<s:property value="#var.password"/>
<s:property value="#var.roleInfo.rolename"/><br>
</s:iterator>
或者:
<s:iterator value="list">
<s:property value="username"/>
<s:property value="password"/>
<s:property value="roleInfo.rolename"/><br>
</s:iterator>
7、数组(普通属性)
public String execute(){
strs = new String[2];
strs[0] = "as";
strs[1] = "asd";
}
<s:iterator value="strs">
<s:property/><br>
</s:iterator>
或者:
<s:iterator value="strs" var="var">
<s:property value="var"/><br>
</s:iterator>
8、数组(自定义对象属性)
public String execute(){
strs = new UserInfo[2];
UserInfo userInfo = new UserInfo();
userInfo.setUsername("u11");
userInfo.setPassword("11");
strs[0] = userInfo;
userInfo = new UserInfo();
userInfo.setUsername("u22");
userInfo.setPassword("22");
strs[1] = userInfo;
}
<s:iterator value="strs">
<s:property value="username"/>
<s:property value="password"/><br>
</s:iterator>
或者:
<s:iterator value="strs" var="var">
<s:property value="#var.username"/>
<s:property value="#var.password"/><br>
</s:iterator>
8、数组(自定义对象属性)
public String execute(){
strs = new UserInfo[2];
UserInfo userInfo = new UserInfo();
userInfo.setUsername("u11");
userInfo.setPassword("11");
userInfo.setRoleInfo(new RoleInfo("r11"));
strs[0] = userInfo;
userInfo = new UserInfo();
userInfo.setUsername("u22");
userInfo.setPassword("22");
userInfo.setRoleInfo(new RoleInfo("r22"));
strs[1] = userInfo;
}
<s:iterator value="strs" var="var">
<s:property value="#var.username"/>
<s:property value="#var.password"/>
<s:property value="#var.roleInfo.rolename"/><br>
</s:iterator>
或者:
<s:iterator value="strs">
<s:property value="username"/>
<s:property value="password"/>
<s:property value="roleInfo.rolename"/><br>
</s:iterator>
8、Map(普通属性)
public String execute(){
map = new HashMap<String,String>();
map.put("k1", "v1");
map.put("k2", "v2");
}
<s:iterator value="map" var="var">
<s:property value="#var.key"/>
<s:property value="#var.value"/><br>
</s:iterator>
或者:
<s:iterator value="map">
<s:property value="key"/>
<s:property value="value"/><br>
</s:iterator>
8、Map(自定义对象属性)
public String execute(){
map = new HashMap<String,UserInfo>();
UserInfo userInfo = new UserInfo();
userInfo.setUsername("u1");
userInfo.setPassword("p1");
userInfo.setRoleInfo(new RoleInfo("r1"));
map.put("k1", userInfo);
userInfo = new UserInfo();
userInfo.setUsername("u2");
userInfo.setPassword("p2");
userInfo.setRoleInfo(new RoleInfo("r2"));
map.put("k2", userInfo);
}
<s:iterator value="map" var="var">
<s:property value="#var.key"/>
<s:property value="#var.value.username"/>
<s:property value="#var.value.password"/>
<s:property value="#var.value.roleInfo.rolename"/><br>
</s:iterator>
或者
<s:iterator value="map">
<s:property value="key"/>
<s:property value="value.username"/>
<s:property value="value.password"/>
<s:property value="value.roleInfo.rolename"/><br>
</s:iterator>
8、Map嵌套Map(自定义对象属性)
public String execute(){
map = new TreeMap<String,Map<String,UserInfo>>();
Map<String,UserInfo> innerMap = new TreeMap<String,UserInfo>();
UserInfo userInfo = new UserInfo();
userInfo.setUsername("u1");
userInfo.setPassword("p1");
userInfo.setRoleInfo(new RoleInfo("r1"));
userInfo = new UserInfo();
userInfo.setUsername("u11");
userInfo.setPassword("p11");
userInfo.setRoleInfo(new RoleInfo("r11"));
innerMap.put("k1", userInfo);
innerMap.put("k11", userInfo);
map.put("key1", innerMap);
//////////////////////////
innerMap = new TreeMap<String,UserInfo>();
userInfo = new UserInfo();
userInfo.setUsername("u2");
userInfo.setPassword("p2");
userInfo.setRoleInfo(new RoleInfo("r2"));
userInfo = new UserInfo();
userInfo.setUsername("u22");
userInfo.setPassword("p22");
userInfo.setRoleInfo(new RoleInfo("r22"));
innerMap.put("k2", userInfo);
innerMap.put("k22", userInfo);
map.put("key2", innerMap);
}
<s:iterator value="map" var="var">
<s:iterator value="value">
<s:property value="#var.key"/>
<s:property value="key"/>
<s:property value="value.username"/>
<s:property value="value.password"/>
<s:property value="value.roleInfo.rolename"/><br>
</s:iterator>
</s:iterator>
var属性的含义:
1、var属性为可选,如果无, <s:property value="集合中元素的属性"/> 因为集合中的对象被置于compoundRoot栈顶
2、如果有var属性,则 <s:property value="#var.key"/> 此时集合中对象会以var为key置于Map中
Struts2的iterator各种用法
精选 转载文章标签 Struts2的iterator各种用法 文章分类 前端开发
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章