Struts 2 s:generator control tag

:

The s:generator tag is used to generate an iterator based on the specified value in the val attribute.
Syntax: <s:generator val=”%{value}”>

Example:

index.jsp

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  	<title>Struts 2 s:generator control tag example</title>
	<META HTTP-EQUIV="Refresh" CONTENT="0;URL=Test.action">
  </head>  
  <body>
  </body>
</html>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 
 
 	<filter>
	  <filter-name>struts2</filter-name>
	  <filter-class>
        	org.apache.struts2.dispatcher.ng.
        	filter.StrutsPrepareAndExecuteFilter
          </filter-class>
	</filter>
 
	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
 
	<welcome-file-list>
	  <welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
 
</web-app>

struts.xml

<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
 
<struts>
 
	<package name="default" extends="struts-default">
		<action name="Test" class="com.javawithease.action.Test">
			<result name="success">/test.jsp</result>
		</action>
	</package>
 
</struts>

Test.java

import com.opensymphony.xwork2.ActionSupport;
 
/**
 * This class is used as an action class.
 * @author javawithease
 */
public class Test extends ActionSupport{
	//data members
	private String subjectString;
 
	//business logic
	public String execute(){
		subjectString = "Java,DBMS,Networking,Operating System";
 
		return SUCCESS;	
	}
 
	//getter setters
	public String getSubjectString() {
		return subjectString;
	}
 
	public void setSubjectString(String subjectString) {
		this.subjectString = subjectString;
	}	
 
}

test.jsp

<%@ taglib uri="/struts-tags" prefix="s"%>
<html>
	<head>
		<title>Struts 2 s:generator control tag example</title>
	</head>
	<body>
		<h3>This is a s:generator control tag example.</h3>
		<s:generator val="%{subjectString}" 
                             var="subjectList" separator=","/>
 
		<h4>Generated iterator values</h4>
		<s:iterator value="subjectList">
			<s:property/><br/>
		</s:iterator>
	</body>
</html>

Output:

struts 46
 
Download this example.
 
Next Topic: Struts 2 s:sort control tag with example.
Previous Topic: Struts 2 s:append control tag with example.

Related Topics:

Struts 2 If, else if and else control tag with example.
Struts 2 iterator control tag with example.
Struts 2 merge control tag with example.
Struts 2 s:append control tag with example.
Struts 2 s:generator control tag with example.
Struts 2 s:sort control tag with example.