Struts 2 Generator Tag Example


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.


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;

2. Generator Tag Example

Write JSP with generator tag to display the list of values.

<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<title>Generator Tag Example</title>

<h2>Example of Generator Tag</h2>
<h3>List of languages:</h3>

<s:generator val="%{'Java, Groovy, Scala, Ceylon, C++'}" count="5"
      <s:property /><br/>


3. Struts.xml configurations

Write struts.xml configuration file.

<?xml version="1.0" encoding="UTF-8"?>
   "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
	<constant name="struts.devMode" value="true" />
	<package name="tags" extends="struts-default">
		<action name="struts2example" class=""
					<result name="success">/Generator.jsp</result>

4. Run the application

If you access the application http://localhost:8080/Struts2App/struts2example.action. You would see the following output in your screen.

Struts 2 Generator Example



About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Speak Your Mind


Please support the site
By clicking any of these buttons you help our site to get better