(6)选择标签与迭代标签

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等取数据(注意加#)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值