Struts2.2.1 subset Tag Example
Posted on: January 3, 2011 at 12:00 AM
The subset tag is a generic tag is used to output a subset or portion of an iterator elements.

Struts2.2.1 subset Tag Example

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;

import com.opensymphony.xwork2.ActionSupport;
import java.util.*;

public class SubsetTag extends ActionSupport {
private List<String> list1 = new ArrayList<String>();

public String execute() throws Exception {
list1.add("C");
list1.add("C++");
list1.add("C#");
list1.add("VC#");
list1.add("JAVA");
list1.add(".NET");
list1.add("PHP");
list1.add("PERL");
list1.add("IPHONE");
list1.add("ORACLE");
list1.add("MYSQL");
return SUCCESS;
}
public List<String> getList1() {
return list1;
}
}

This Program produces output on the basis of the Subset tag evaluation, This  give the output as-

Output:-

Download Select Source Code


Related Tags for Struts2.2.1 subset Tag Example:

Advertisements

Ads

 
Advertisement null

Ads