Struts 2 Generator Tag (Simple Iterator)
In this section, you will learn about the generator tag. Firstly, we will know about the generator tag. The Generator tag is based on the value of supplied val iterator.
Generator: Main thing of the generator iterator push the value into stack always on top and poped at the end of stack.
You see the following example to use generator with simple iterator:
index.jsp:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0
Transitional//EN">
<html>
<head>
<title>Struts 2 Tags Examples</title>
</head>
<body>
<table border="0" cellpadding="0" cellspacing="0"
width="400">
<tr>
<td>
<h2>Welcome to Developerhelpway.com</h2>
<h3>Struts 2 Generic Tags Example</h3>
<b>Control Tags Example</b>
<ol>
<li><a href="generatorTag.action">
Generatot With Simple Iterator</a></li>
</ol>
</td>
</tr>
</table>
</body>
</html>
|
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.enable
.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="true" />
<!-- Add your package and namespace here -->
<package name="developerhelpway" namespace="/"
extends="struts-default">
<!-- Add your actions here -->
<!-- Generic Tags -->
<action name="generatorTag"
class="developerhelpway.action.GeneratorTag">
<result>/pages/genericTags/GeneratorTag.jsp</result>
</action>
<!-- Actions end-->
</package>
</struts>
|
GeneratorTag.java:
package developerhelpway.action;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.util.IteratorGenerator.Converter;
public class GeneratorTag extends ActionSupport {
public String excute() throws Exception{
return SUCCESS;
}
}
|
GeneratorTag.jsp:
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title> Generator Tag Example </title>
</head>
<body>
<h2>Generator Tag Example</h2>
<h3>Generator With Simple Iterator</h3>
<s:generator val="%{'Struts,Hibernate,JSF,AJAX'}"
separator=",">
<s:iterator>
<s:property /><br/>
</s:iterator>
</s:generator>
</body>
</html>
|
Output: