Generator tag in struts2 framework

Generator tag in struts2 framework

Previous Home Next

 

Struts2 framework provide generator component to generate an iterator based "val" attribute.Create an iterator with generator tag and modify the iterator value with the “Converter” object
The struts2 framework provide following generator tag library,

<s:generator val="%{'Mukund|Amit|Praveen|Harsh'}" separator="|" 
converter="%{languageConverter}">
<ol>
<s:iterator>
  <li><s:property /></li>
</s:iterator>
</s:generator>

 
Directory Structure of <s:generator> tag Example in Struts 2.0 Using MyEclipse IDE



index.jsp

<%@taglib uri="/struts-tags" prefix="s"%>

<h2>Generator tag example in Struts2 Framework</h2>

<s:generator val="%{'Mukund|Amit|Praveen|Harsh'}" separator="|" 
converter="%{languageConverter}">
<ol>
<s:iterator>
  <li><s:property /></li>
</s:iterator>
</s:generator>
</ol>

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>f1</filter-name>
  <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
  </filter>
  <filter-mapping>
  <filter-name>f1</filter-name>
  <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration
2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="demo" extends="struts-default">
<action name="" class="org.r4r.GeneratorAction">
<result name="success" type="dispatcher">/index.jsp</result>
</action>
</package>
</struts>

GeneratorAction.java

package org.r4r;

import org.apache.struts2.util.IteratorGenerator.Converter;

public class GeneratorAction {
	public String execute(){
		return "success";
	}
	public Converter getLanguageConverter(){
		return new Converter() {
	         public Object convert(String value) throws Exception {
 
	        	 if("Mukund".equals(value)){
	        		 return "[New Name Kumar Bal Mukund] - " + value;
	        	 }
	        	 else if("Amit".equals(value)){
	        		 return "[New Name Amit Singh] - " + value;
	        	 }
	        	 else if("Praveen".equals(value)){
	        		 return "[New Name Praveen Singh] - " + value;
	        	 }
	        	 else if("Harsh".equals(value)){
	        		 return "[New Name Harsh Vardhan] - " + value;
	        	 }else{
	        		 return value;
	        	 }
 
	         }
	     };
	}

}

Output






Previous Home Next