Struts 2 generator tag is used to generate an iterator based on the ?val? attribute provided in the page.The generator tag is a generic tag that is used to generate iterators based on different attributes passed. In this tutorials, you will use Struts 2 generator tag to do the following tasks :
The following Example will shows how to implement the generator tag in the Struts2.2.1 --
First we create a JSP file named generator.jsp as follows.
A JSP page to show the use of generator tag to create a iterator dynamically. The ?separator? attribute is required, which separating the val into the entries of the iterator. The ?converter? attribute is optional, which allow you to modify the value. the <s:generator> generates a simple iterator based on the val attribute supplied and <s:iterator> tag prints it out using the <s:property /> tag.
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <s:generator separator="," val="%{'Gyan,Arun,Ankit,Rohit'}" > <s:iterator > <s:property/><br/> </s:iterator> </s:generator> </body> </html> |
The index.jsp file is as follows- This file only contains the hiperlink only.
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> This is my jsp. <a href="GeneratorTag.action">Generator tag</a> </body> </html> |
The Struts mapping file Struts.xml is as follows-
<?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.enable.DynamicMethodInvocation" value="false" /> <constant name="struts.devMode" value="false" /> <constant name="struts.custom.i18n.resources" value="ApplicationResources" /> <package name="default" namespace="/" extends="struts-default"> <action name="GeneratorTag" class="roseindia.GeneratorTag"> <result name="success">/generator.jsp</result> </action> </package> </struts> |
The web config file web.xml is as follows-
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>GeneratorTag</display-name> <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> |
The action class GeneratorTag.java is as follows.
package roseindia; |
This Program produces output on the basis of the generator tag evaluation, This give the output as-
Output:-
Advertisements
Ads
Ads