開発者ドキュメント

Struts 2イテレータタグの例

ダウンロードする – リンク://wp-content/uploads/2010/07/Struts2-Iterator-tag-Example.zip[Struts2-Iterator-tag-Example.zip]

Struts 2

Iteratorタグは、

java.util.Collection

または

java.util.Iterator

のいずれかの値を反復処理するために使用されます。このチュートリアルでは、リスト変数を作成し、それをループするためにIteratorタグを使用し、

IteratorStatus ** を使用してイテレータのステータスを取得します。

1.アクション

Listプロパティを持つActionクラス。さまざまな美味しい「KFCコンボミール」が含まれています。

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.イテレータの例

「KFC comboMeals」リストをループするための

Iteratorタグ

の使用を示すJSPページ。

Iteratorタグ

には、

IteratorStatus

クラスの名前を宣言するために使用される ”

status

“属性が含まれています。

  • IteratorStatus ** クラスは、反復の状態に関する情報を取得するために使用されます。サポートされているプロパティは、インデックス、カウント、最初、最後、奇数、偶数などです。これを参照してくださいhttp://struts.apache.org/2.1.8/struts2-core/apidocs/org/apache/struts2/views/jsp/IteratorStatus.html[IteratorStatus documentation]を参照してください。

<%@ taglib prefix="s" uri="/struts-tags" %>

<html>
<head>
</head>

<body>
<h1>Struts 2 Iterator tag example</h1>

<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle"
     style="display:block"
     data-ad-client="ca-pub-2836379775501347"
     data-ad-slot="8821506761"
     data-ad-format="auto"
     data-ad-region="mkyongregion"></ins>
<script>
(adsbygoogle = window.adsbygoogle ||[]).push({});
</script><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.デモ

image =//wp-content/uploads/2010/07/Struts2-Iterator-tag-example.jpg[Struts 2 Iteratorタグ、title = “Struts2-Iterator-tag-example”、width = 587、height = 480]

モバイルバージョンを終了