Struts 2 generator tag takes inline array or list of values and pass to the s:iterator to loop through the list of values.
1. Create Struts 2 Action
Create action class.
[code lang=”java”] package javabeat.net.struts2;public class Struts2ExampleAction{
private String lang;
public String execute() throws Exception {
return "success";
}
public String getLang() {
return lang;
}
public void setLang(String lang) {
this.lang = lang;
}
}[/code]
2. Generator Tag Example
Write JSP with generator tag to display the list of values.
[code lang=”xml”] <%@ page contentType="text/html; charset=UTF-8" %><%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Generator Tag Example</title>
</head>
<body>
<h2>Example of Generator Tag</h2>
<h3>List of languages:</h3>
<s:generator val="%{‘Java, Groovy, Scala, Ceylon, C++’}" count="5"
separator=",">
<s:iterator>
<s:property /><br/>
</s:iterator>
</s:generator>
</body>
</html>
[/code]
3. Struts.xml configurations
Write struts.xml configuration file.
[code lang=”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="tags" extends="struts-default">
<action name="struts2example" class="javabeat.net.struts2.Struts2ExampleAction"
method="execute">
<result name="success">/Generator.jsp</result>
</action>
</package>
</struts>
[/code]
4. Run the application
If you access the application http://localhost:8080/Struts2App/struts2example.action. You would see the following output in your screen.