public String show() {
this.setUsername("HZP");
this.setPassword("HZP123");
ActionContext.getContext().put("age", 80);
List<User> users=new ArrayList<>();
users.add(new User("HZP1", "p1"));
users.add(new User("HZP2", "p2"));
users.add(new User("HZP3", "p3"));
users.add(new User("HZP4", "p4"));
ActionContext.getContext().put("users", users);
return "success";
}
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>show</h1>
<s:if test="#age<20">
未成年
</s:if>
<s:elseif test="#age>=20 and #age<=60">
成年人
</s:elseif>
<s:else>
老年人
</s:else>
<br>
//迭代方式1:
<s:iterator value="#users">
<s:property value="id"/>----<s:property value="username"/>----<s:property value="nickname"/>
</s:iterator>
<br>
//迭代方式2:
<s:iterator value="#users" var="u" status="st">
<div <s:if test="#st.odd">style="background:#ff0"</s:if>>
<s:property value="#u.username"/>----<s:property value="#u.nickname"/>----<s:property value="#root[1].username"/><br>
</div>
</s:iterator>
<s:property value="username"/>
</body>
</html>
CompoundRoot:是一个使用list的栈结构
迭代方式1:当使用迭代标签后,会将相应的users中的每一个对象(tempUser)放到CompoundRoot中{入栈},此时id相当于#root[0].id,该对象使用完毕后,出栈,然后users中的下一个对象入栈,反复执行。
迭代方式2当定义var=“u”时,就会将users中的每一个对象存在ActionContext中,当循环完成后,ActionContext中该对象也会清空,因为在ActionContext中,所以使用#u.id等取数据(注意加#)