The merge tag is a generic tag that is used to merge iterators. The successive
call to the merge iterator causes each merge iterator to have a chance to expose
its element, subsequently next call allows the next iterator to expose its element. Once the last iterator is done exposing its element,
the first iterator is allowed to do so again (unless it is exhausted of entries).
The following Example will shows how to implement the merge tag in the Struts2.2.1 --
First we create a JSP file named merge.jsp as follows.
<%@ 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>Merge Tag Example</title> </head> <body> <h1>Struts 2 Merge tag example</h1> Merge List1 and List2 into a single iterator. <s:merge id="MergeList"> <s:param value="%{list1}" /> <s:param value="%{list2}" /> </s:merge> <s:iterator value="%{#MergeList}"> <li><s:property /></li> </s:iterator> </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"> <%@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> <a href="MergeTag.action">Merge Tag Example</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="MergeTag" class="roseindia.MergeTag"> <result name="success">/merge.jsp</result> </action> </package> </struts> |
The action class MergeTag.java is as follows.
package roseindia; |
This Program produces output on the basis of the MergeTag evaluation, This give the output as-
Output:-
Advertisements
Ads
Ads