Struts 2 Generator Tag Example

SHARE & 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.

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;
   }
}

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" %>
<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>

3. Struts.xml configurations

Write struts.xml configuration file.

<?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>

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

Comments

comments

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

*

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