• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

Struts 2 Generator Tag Example

December 13, 2013 //  by Krishna Srinivasan//  Leave a Comment

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.

Struts 2 Generator Example

Category: StrutsTag: Struts 2 Tutorials

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.

Previous Post: « Struts 2 Merge Tag Example
Next Post: Struts 2 Control Tags »

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

np.zeros

A Complete Guide To NumPy Functions in Python For Beginners

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact