Struts 2 Iterator标记示例

下载它– Struts2-Iterator-tag-Example.zip

Struts 2 Iterator标记用于迭代一个值,该值可以是java.util.Collectionjava.util.Iterator中的任何一个。 在本教程中,您将创建一个列表变量,在它使用iterator标签中循环,并获得与IteratorStatus迭代状态。

1.行动

具有List属性的Action类,其中包含各种美味的“肯德基组合餐”。

IteratorKFCAction

package com.mkyong.common.action;
 
import java.util.ArrayList;
import java.util.List;

import com.opensymphony.xwork2.ActionSupport;
 
public class IteratorKFCAction extends ActionSupport{
 
	private List<String> comboMeals;
	
	public List<String> getComboMeals() {
		return comboMeals;
	}

	public void setComboMeals(List<String> comboMeals) {
		this.comboMeals = comboMeals;
	}

	public String execute() {
		
		comboMeals = new ArrayList<String>();
		comboMeals.add("Snack Plate");
		comboMeals.add("Dinner Plate");
		comboMeals.add("Colonel Chicken Rice Combo");
		comboMeals.add("Colonel Burger");
		comboMeals.add("O.R. Fillet Burger");
		comboMeals.add("Zinger Burger");
		
		return SUCCESS;
	}
}

2.迭代器示例

一个JSP页面,显示使用Iterator标记遍历“ KFC comboMeals”列表。 在Iterator标记中 ,它包含“ status ”属性,该属性用于声明IteratorStatus类的名称。

IteratorStatus类用于获取有关迭代状态的信息。 支持的属性包括索引,计数,第一个,最后一个,奇数,偶数等。请确保您访问此IteratorStatus文档以了解其更多详细信息。

<%@ taglib prefix="s" uri="/struts-tags" %>
 
<html>
<head>
</head>
 
<body>
<h1>Struts 2 Iterator tag example</h1>

<h2>Simple Iterator</h2>
<ol>
<s:iterator value="comboMeals">
  <li><s:property /></li>
</s:iterator>
</ol>

<h2>Iterator with IteratorStatus</h2>
<table>
<s:iterator value="comboMeals" status="comboMealsStatus">
  <tr>
  	<s:if test="#comboMealsStatus.even == true">
      <td style="background: #CCCCCC"><s:property/></td>
    </s:if>
    <s:elseif test="#comboMealsStatus.first == true">
      <td><s:property/> (This is first value) </td>
    </s:elseif>
    <s:else>
      <td><s:property/></td>
    </s:else>
  </tr>
</s:iterator>
</table>

</body>
</html>

3. struts.xml

链接〜

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
 
<struts>

 	<constant name="struts.devMode" value="true" />
	
	<package name="default" namespace="/" extends="struts-default">
	
		<action name="iteratorKFCAction" 
			class="com.mkyong.common.action.IteratorKFCAction" >
			<result name="success">pages/iterator.jsp</result>
		</action>
		
	</package>
		
</struts>

4.演示

http:// localhost:8080 / Struts2Example / iteratorKFCAction.action

Struts 2 Iterator tag

参考

  1. Struts 2 Iterator标记示例
  2. IteratorStatus文档

翻译自: https://mkyong.com/struts2/struts-2-iterator-tag-example/

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值