The subset tag is a generic tag is used to output a subset or portion of an iterator elements.
The following Example will shows how to implement the subset tag in the Struts2.2.1 --
First we create a JSP file named Subset.jsp as follows.
Now create a jsp page using <s:subset> and <s:iterator> tags as shown in the SubsetTag.jsp page. The subset tag takes an iterator and outputs a subset of it. The parameter count is of integer type and it sets the number of entries to be kept in the resulting subset iterator.
|
<%@ 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>Subset Tag</title></ head>< body>The Subset of first 4 values : <br>< s:subset source="list1" count="4"> <s:iterator> <s:property /><br> </s:iterator></ s:subset>< br>The Subset of 4 values starting from 5th index : <br>< s:subset source="list1" start="4" count="4"> <s:iterator> <s:property /><br> </s:iterator></ s:subset></ 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>Subset Tag Example</title> </head> <body> <a href="SubsetTag.action">Subset 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="SubsetTag" class="roseindia.SubsetTag"> <result name="success">/Subset.jsp</result> </action></package> </struts> |
Create a list in the action class and populate it with various items as shown in the ?SubsetTag? class.
The action class SubsetTag.java is as follows.
|
package roseindia; |
This Program produces output on the basis of the Subset tag evaluation, This give the output as-
Output:-