Source Code Cross Referenced for IteratorComponent.java in  » Web-Framework » struts-2.0.11 » org » apache » struts2 » components » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Web Framework » struts 2.0.11 » org.apache.struts2.components 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * $Id: IteratorComponent.java 497654 2007-01-19 00:21:57Z rgielen $
003:         *
004:         * Licensed to the Apache Software Foundation (ASF) under one
005:         * or more contributor license agreements.  See the NOTICE file
006:         * distributed with this work for additional information
007:         * regarding copyright ownership.  The ASF licenses this file
008:         * to you under the Apache License, Version 2.0 (the
009:         * "License"); you may not use this file except in compliance
010:         * with the License.  You may obtain a copy of the License at
011:         *
012:         *  http://www.apache.org/licenses/LICENSE-2.0
013:         *
014:         * Unless required by applicable law or agreed to in writing,
015:         * software distributed under the License is distributed on an
016:         * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017:         * KIND, either express or implied.  See the License for the
018:         * specific language governing permissions and limitations
019:         * under the License.
020:         */
021:        package org.apache.struts2.components;
022:
023:        import java.io.Writer;
024:        import java.util.Iterator;
025:
026:        import org.apache.struts2.views.annotations.StrutsTag;
027:        import org.apache.struts2.views.annotations.StrutsTagAttribute;
028:        import org.apache.struts2.util.MakeIterator;
029:        import org.apache.struts2.views.jsp.IteratorStatus;
030:
031:        import com.opensymphony.xwork2.util.ValueStack;
032:
033:        /**
034:         * <!-- START SNIPPET: javadoc -->
035:         *
036:         * <p>Iterator will iterate over a value. An iterable value can be either of: java.util.Collection, java.util.Iterator,
037:         * java.util.Enumeration, java.util.Map, array.</p> <p/> <!-- END SNIPPET: javadoc -->
038:         *
039:         * <!-- START SNIPPET: params -->
040:         *
041:         * <ul>
042:         *
043:         * <li>status (String) - if specified, an instanceof IteratorStatus will be pushed into stack upon each iteration</li>
044:         *
045:         * <li>value (Object) - the source to iterate over, must be iteratable, else an the object itself will be put into a
046:         * newly created List (see MakeIterator#convert(Object)</li>
047:         *
048:         * <li>id (String) - if specified the current iteration object will be place with this id in Struts stack's context
049:         * scope</li>
050:         *
051:         * </ul>
052:         *
053:         * <!-- END SNIPPET: params -->
054:         *
055:         * <!-- START SNIPPET: example1description -->
056:         *
057:         * <p>The following example retrieves the value of the getDays() method of the current object on the value stack and
058:         * uses it to iterate over. The &lt;s:property/&gt; tag prints out the current value of the iterator.</p>
059:         *
060:         * <!-- END SNIPPET: example1description -->
061:         *
062:         * <pre>
063:         * <!-- START SNIPPET: example1code -->
064:         * &lt;s:iterator value="days"&gt;
065:         *   &lt;p&gt;day is: &lt;s:property/&gt;&lt;/p&gt;
066:         * &lt;/s:iterator&gt;
067:         * <!-- END SNIPPET: example1code -->
068:         * </pre>
069:         *
070:         *
071:         * <!-- START SNIPPET: example2description -->
072:         *
073:         * <p>The following example uses a {@link Bean} tag and places it into the ActionContext. The iterator tag will retrieve
074:         * that object from the ActionContext and then calls its getDays() method as above. The status attribute is also used to
075:         * create a {@link IteratorStatus} object, which in this example, its odd() method is used to alternate row
076:         * colours:</p>
077:         *
078:         * <!-- END SNIPPET: example2description -->
079:         *
080:         *
081:         * <pre>
082:         * <!-- START SNIPPET: example2code -->
083:         *
084:         * &lt;s:bean name="org.apache.struts2.example.IteratorExample" id="it"&gt;
085:         *   &lt;s:param name="day" value="'foo'"/&gt;
086:         *   &lt;s:param name="day" value="'bar'"/&gt;
087:         * &lt;/s:bean&gt;
088:         * <p/>
089:         * &lt;table border="0" cellspacing="0" cellpadding="1"&gt;
090:         * &lt;tr&gt;
091:         *   &lt;th&gt;Days of the week&lt;/th&gt;
092:         * &lt;/tr&gt;
093:         * <p/>
094:         * &lt;s:iterator value="#it.days" status="rowstatus"&gt;
095:         *   &lt;tr&gt;
096:         *     &lt;s:if test="#rowstatus.odd == true"&gt;
097:         *       &lt;td style="background: grey"&gt;&lt;s:property/&gt;&lt;/td&gt;
098:         *     &lt;/s:if&gt;
099:         *     &lt;s:else&gt;
100:         *       &lt;td&gt;&lt;s:property/&gt;&lt;/td&gt;
101:         *     &lt;/s:else&gt;
102:         *   &lt;/tr&gt;
103:         * &lt;/s:iterator&gt;
104:         * &lt;/table&gt;
105:         *
106:         * <!-- END SNIPPET: example2code -->
107:         * </pre>
108:         *
109:         * <!--START SNIPPET: example3description -->
110:         *
111:         * <p> The next example will further demonstrate the use of the status attribute, using a DAO obtained from the action
112:         * class through OGNL, iterating over groups and their users (in a security context). The last() method indicates if the
113:         * current object is the last available in the iteration, and if not, we need to seperate the users using a comma: </p>
114:         *
115:         * <!-- END SNIPPET: example3description -->
116:         *
117:         * <pre>
118:         * <!-- START SNIPPET: example3code -->
119:         *
120:         *  &lt;s:iterator value="groupDao.groups" status="groupStatus"&gt;
121:         *      &lt;tr class="&lt;s:if test="#groupStatus.odd == true "&gt;odd&lt;/s:if&gt;&lt;s:else&gt;even&lt;/s:else&gt;"&gt;
122:         *          &lt;td&gt;&lt;s:property value="name" /&gt;&lt;/td&gt;
123:         *          &lt;td&gt;&lt;s:property value="description" /&gt;&lt;/td&gt;
124:         *          &lt;td&gt;
125:         *              &lt;s:iterator value="users" status="userStatus"&gt;
126:         *                  &lt;s:property value="fullName" /&gt;&lt;s:if test="!#userStatus.last"&gt;,&lt;/s:if&gt;
127:         *              &lt;/s:iterator&gt;
128:         *          &lt;/td&gt;
129:         *      &lt;/tr&gt;
130:         *  &lt;/s:iterator&gt;
131:         *
132:         * <!-- END SNIPPET: example3code -->
133:         * </pre>
134:         * <p>
135:         *
136:         * <!-- START SNIPPET: example4description -->
137:         *
138:         * </p> The next example iterates over a an action collection and passes every iterator value to another action. The
139:         * trick here lies in the use of the '[0]' operator. It takes the current iterator value and passes it on to the edit
140:         * action. Using the '[0]' operator has the same effect as using &gt;s:property /&gt;. (The latter, however, does not
141:         * work from inside the param tag). </p>
142:         *
143:         * <!-- END SNIPPET: example4description -->
144:         *
145:         * <pre>
146:         * <!-- START SNIPPET: example4code -->
147:         *
148:         *      &lt;s:action name="entries" id="entries"/&gt;
149:         *      &lt;s:iterator value="#entries.entries" &gt;
150:         *          &lt;s:property value="name" /&gt;
151:         *          &lt;s:property /&gt;
152:         *          &lt;s:push value="..."&gt;
153:         *              &lt;s:action name="edit" id="edit" &gt;
154:         *                  &lt;s:param name="entry" value="[0]" /&gt;
155:         *              &lt;/s:action&gt;
156:         *          &lt;/push&gt;
157:         *      &lt;/s:iterator&gt;
158:         *
159:         * <!-- END SNIPPET: example4code -->
160:         * </pre>
161:         *
162:         * <!-- START SNIPPET: example5description -->
163:         *
164:         * </p>To simulate a simple loop with iterator tag, the following could be done.
165:         * It does the loop 5 times.
166:         *
167:         * <!-- END SNIPPET: example5description -->
168:         *
169:         * <pre>
170:         * <!-- START SNIPPET: example5code -->
171:         *
172:         * &lt;s:iterator status="stat" value="{1,2,3,4,5}" &gt;
173:         *    &lt;!-- grab the index (start with 0 ... ) --&gt;
174:         *    &lt;s:property value="#stat.index" /&gt;
175:         *
176:         *    &lt;!-- grab the top of the stack which should be the --&gt;
177:         *    &lt;!-- current iteration value (0, 1, ... 5) --&gt;
178:         *    &lt;s:property value="top" /&gt;
179:         * &lt;/s:iterator&gt;
180:         *
181:         * <!-- END SNIPPET: example5code -->
182:         * </pre>
183:         *
184:         */
185:        @StrutsTag(name="iterator",tldTagClass="org.apache.struts2.views.jsp.IteratorTag",description="Iterate over a iterable value")
186:        public class IteratorComponent extends Component {
187:            protected Iterator iterator;
188:            protected IteratorStatus status;
189:            protected Object oldStatus;
190:            protected IteratorStatus.StatusState statusState;
191:            protected String statusAttr;
192:            protected String value;
193:
194:            public IteratorComponent(ValueStack stack) {
195:                super (stack);
196:            }
197:
198:            public boolean start(Writer writer) {
199:                //Create an iterator status if the status attribute was set.
200:                if (statusAttr != null) {
201:                    statusState = new IteratorStatus.StatusState();
202:                    status = new IteratorStatus(statusState);
203:                }
204:
205:                ValueStack stack = getStack();
206:
207:                if (value == null) {
208:                    value = "top";
209:                }
210:                iterator = MakeIterator.convert(findValue(value));
211:
212:                // get the first
213:                if ((iterator != null) && iterator.hasNext()) {
214:                    Object currentValue = iterator.next();
215:                    stack.push(currentValue);
216:
217:                    String id = getId();
218:
219:                    if ((id != null) && (currentValue != null)) {
220:                        //pageContext.setAttribute(id, currentValue);
221:                        //pageContext.setAttribute(id, currentValue, PageContext.REQUEST_SCOPE);
222:                        stack.getContext().put(id, currentValue);
223:                    }
224:
225:                    // Status object
226:                    if (statusAttr != null) {
227:                        statusState.setLast(!iterator.hasNext());
228:                        oldStatus = stack.getContext().get(statusAttr);
229:                        stack.getContext().put(statusAttr, status);
230:                    }
231:
232:                    return true;
233:                } else {
234:                    super .end(writer, "");
235:                    return false;
236:                }
237:            }
238:
239:            public boolean end(Writer writer, String body) {
240:                ValueStack stack = getStack();
241:                if (iterator != null) {
242:                    stack.pop();
243:                }
244:
245:                if (iterator != null && iterator.hasNext()) {
246:                    Object currentValue = iterator.next();
247:                    stack.push(currentValue);
248:
249:                    String id = getId();
250:
251:                    if ((id != null) && (currentValue != null)) {
252:                        //pageContext.setAttribute(id, currentValue);
253:                        //pageContext.setAttribute(id, currentValue, PageContext.REQUEST_SCOPE);
254:                        stack.getContext().put(id, currentValue);
255:                    }
256:
257:                    // Update status
258:                    if (status != null) {
259:                        statusState.next(); // Increase counter
260:                        statusState.setLast(!iterator.hasNext());
261:                    }
262:
263:                    return true;
264:                } else {
265:                    // Reset status object in case someone else uses the same name in another iterator tag instance
266:                    if (status != null) {
267:                        if (oldStatus == null) {
268:                            stack.getContext().put(statusAttr, null);
269:                        } else {
270:                            stack.getContext().put(statusAttr, oldStatus);
271:                        }
272:                    }
273:                    super .end(writer, "");
274:                    return false;
275:                }
276:            }
277:
278:            @StrutsTagAttribute(description="If specified, an instanceof IteratorStatus will be pushed into stack upon each iteration",type="Boolean",defaultValue="false")
279:            public void setStatus(String status) {
280:                this .statusAttr = status;
281:            }
282:
283:            @StrutsTagAttribute(description="the iteratable source to iterate over, else an the object itself will be put into a newly created List")
284:            public void setValue(String value) {
285:                this.value = value;
286:            }
287:
288:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.