Subset tag example in struts2 framework

Subset tag example in struts2 framework

Previous Home Next

 

Struts2 framework provide subset components to print all list element , selected element and only one element we want to print on the web browser.
<s:subset source="" start="" count="">
   <s:iterator>
      	<s:property />
   </s:iterator>
</s:subset>

The struts2 framework provide following subset tag example,
<s:subset source="namelist" start="1" count="5">
   <s:iterator>
      	<li><s:property /></li>
   </s:iterator>
</s:subset>
 
Directory Structure of <s:subset> tag Example in Struts 2.0 Using MyEclipse IDE



index.jsp

<%@taglib uri="/struts-tags" prefix="s"%>
<h1>Struts 2 Subset tag example</h1>
 
1. Fetch all name through subset tag.
<ol>
<s:subset source="namelist">
   <s:iterator>
      	<li><s:property /></li>
   </s:iterator>
</s:subset>
</ol>

2. Fetch one to five name through subset tag
<ol>
<s:subset source="namelist" start="1" count="5">
   <s:iterator>
      	<li><s:property /></li>
   </s:iterator>
</s:subset>
</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.SubsetTagAction">
<result name="success" type="dispatcher">/index.jsp</result>
</action>
</package>
</struts> 

SubsetTagAction.java

package org.r4r;

import java.util.ArrayList;
import java.util.List;

public class SubsetTagAction {
	List<String> namelist=new ArrayList<String>();
	public String execute(){
		namelist.add("Mukund");
		namelist.add("Harsh");
		namelist.add("Somaru");
		namelist.add("Pramod");
		namelist.add("Sushant");
		namelist.add("Gaurav");
		namelist.add("Sumit");
		namelist.add("Shashi");
		
		return "success";
	}
	public List<String> getNamelist() {
		return namelist;
	}
	public void setNamelist(List<String> namelist) {
		this.namelist = namelist;
	}
}

Output






Previous Home Next