Struts 2 Generator Tag (With Id Attribute)
In this section, you will learn about the generator tag (With Id attribute). You will see the following example that generate an iterator with id attribute.
Here only two attributes will display by using the id attribute of generator tag.
You see the following example to use generator with id attribute:
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="generatorTagIdAttribute.action">
Generator With Id Arrtibute</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="generatorTagIdAttribute"
class="developerhelpway.action.GeneratorTag">
<result>/pages/genericTags/
GeneratorTagIdAttribute.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;
}
}
|
GeneratorTagIdAttribute.jsp:
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@page language="java" import="java.util.*"%>
<html>
<head>
<title> Generator Tag Example! </title>
</head>
<body>
<h1>Generator Tag Example</h1>
<h3>Generator With Id Arrtibute</h3>
<s:generator val="%{'Struts,Hibernate,JSF,AJAX'}"
count="2" separator="," id="myAtt" />
<%
Iterator i = (Iterator) pageContext.getAttribute("myAtt");
while(i.hasNext()) {
String s = (String) i.next(); %>
<%=s%> <br/>
<% }
%>
</body>
</html>
|
Output: