S:iterator Usage and examples

Source: Internet
Author: User

Introduction to the properties of S:iterator
Property name Whether you must Default value Type Description
Status False False String This property is not specified by default, and if specified, an object of the Iteratorstatus class is created, which is the value of this property and is described in more detail below
Value False String A collection of loops, including map, List, set, array
Var False String Each element in the collection, placed in the value stack, accesses the elements in the traversed collection,
Begin False 0 Int Index to begin traversing
End Similar to begin

*ineratorstatus Object Description:

Objects of this class encapsulate information about the loop, obtained by accessing its properties, and the main attributes are:

Index: The indexes of the collection that are currently looping to

Count: Number of cycles already

First: Whether it is a cycle

Last: Whether it is the final loop

Odd: Whether the current position is an odd number

Even: whether the current position is an even number

eg

Create a new class Action1.java with the following code:

 Packagemy.test;ImportCom.opensymphony.xwork2.ActionSupport; Public classAction1extendsactionsupport{PrivateString[] Arr=NewString[5]; //Be sure to add the Get/set method, or a null pointer exception will appear     Publicstring[] Getarr () {returnarr; }     Public voidSetarr (string[] arr) { This. arr =arr; }     PublicString Execute () {intI=101; //The assignment for the following for loop does not work         for(String A:arr) {a= "" +i++; } arr[3]= "This is 3";//this verifies that the above for loop assignment to Arr is not working, which is determined by the character of the string array and string, and the results of the operation can be        returnSUCCESS; }}

New JSP file/iterator.jsp, remember to introduce struts tags

<%@ Page Language="Java"ContentType="text/html; Charset=utf-8"pageencoding="UTF-8"%><%@ taglib Prefix="s"URI="/struts-tags"%><!DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd "><HTML><Head><Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8"><title>Insert Title here</title></Head><Body>    <S:debug></S:debug>    <S:iteratorvar= "One"value= "Arr"begin= "2"Status= "STA">        <!--because the loop-to-value is saved to the value stack, the following values are obtained through the s:property tag -the values to loop to are:<S:propertyvalue= "One"/><BR>the number of cycles is:<S:propertyvalue= "#sta. Count"/><BR>the index of the loop is:<S:propertyvalue= "#sta. Index"/><BR>is an odd number:<S:propertyvalue= "#sta. Odd"/><HR>    </S:iterator></Body></HTML>

Last configuration file to one:

<?XML version= "1.0" encoding= "UTF-8"?><!DOCTYPE struts Public "-//apache software foundation//dtd struts Configuration 2.3//en" "Http://struts.apache.or G/dtds/struts-2.3.dtd "><Struts>    <constantname= "Struts.enable.DynamicMethodInvocation"value= "false" />    <constantname= "Struts.devmode"value= "true" />        < Packagename= "Test"namespace="/"extends= "Struts-default">        <Actionname= "iterator"class= "My.test.Action1">            <result>/iterator.jsp</result>        </Action>        <Actionname= "Index">            <resulttype= "Redirectaction">                <paramname= "ActionName">HelloWorld</param>                <paramname= "namespace">/example</param>            </result>        </Action>    </ Package>    <!--ADD Packages here -</Struts>

The author uses the STRUTS2 2.3.30 version, different versions of the configuration file are not the same drip

Run One:

The above debug if in doubt, self-baidu ~

S:iterator Usage and examples

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.