The sort tag is used to sort a List using a java.util.Comparator. List and Comparator both passed in as the tag attribute.
The following Example will shows how to implement the sort tag in the Struts2.2.1 --
First we create a JSP file named Sort.jsp as follows.ADS_TO_REPLACE_1
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"ADS_TO_REPLACE_2 "http://www.w3.org/TR/html4/loose.dtd"> <%@taglib prefix="s" uri="/struts-tags"%> <html>ADS_TO_REPLACE_3 <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Example Sort Tag</title>ADS_TO_REPLACE_4 </head> <body> The UnSorted Values.<br>ADS_TO_REPLACE_5 <ol> <s:iterator value="list"> <LI><s:property value="id" /> , <s:property value="name" /> ,ADS_TO_REPLACE_6 <s:property value="age" /></LI> </s:iterator> </ol> <table> <tr>ADS_TO_REPLACE_7 <td bgcolor="#ffffcc">The Values Sorted by ID.<br> <s:bean name="roseindia.IdComparator" var="MyId" /> <s:sort comparator="#MyId" source="list">ADS_TO_REPLACE_8 <ol> <s:iterator> <LI><s:property value="id" /> , <s:property value="name" /> ,ADS_TO_REPLACE_9 <s:property value="age" /></LI> </s:iterator> </ol> </s:sort></td>ADS_TO_REPLACE_10 <td></td> <td bgcolor="pink">The Values Sorted by Name.<br> <s:bean name="roseindia.NameComparator" var="MyName" />ADS_TO_REPLACE_11 <s:sort comparator="#MyName" source="list"> <ol> <s:iterator> <LI><s:property value="id" /> ,ADS_TO_REPLACE_12 <s:property value="name" /> , <s:property value="age" /></LI> </s:iterator></ol>ADS_TO_REPLACE_13 </s:sort></td> </tr> </table> </body>ADS_TO_REPLACE_14 </html> |
The Struts mapping file Struts.xml is as follows-
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLICADS_TO_REPLACE_15 "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts>ADS_TO_REPLACE_16 <constant name="struts.enable.DynamicMethodInvocation" value="false" /> <constant name="struts.devMode" value="false" /> <constant name="struts.custom.i18n.resources" value="ApplicationResources" />ADS_TO_REPLACE_17 <package name="default" namespace="/" extends="struts-default"> <action name="SortTag" class="roseindia.SortTagAction"> <result name="success">/Sort.jsp</result>ADS_TO_REPLACE_18 </action> </package> </struts> |
Here is the index.jspADS_TO_REPLACE_19
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"ADS_TO_REPLACE_20 "http://www.w3.org/TR/html4/loose.dtd"> <html> <head>ADS_TO_REPLACE_21 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Example Sort Tag</title> </head>ADS_TO_REPLACE_22 <body> <h1>Example Sort Tag</h1> <hr>ADS_TO_REPLACE_23 <a href="SortTag.action">Example Sort Tag</a> </body> </html> |
The action class SortTagAction.java is as follows.ADS_TO_REPLACE_24
package roseindia; import com.opensymphony.xwork2.ActionSupport; import java.util.*;ADS_TO_REPLACE_25 public class SortTagAction extends ActionSupport { private List<SortTag> list = new ArrayList<SortTag>(); public String execute() throws Exception {ADS_TO_REPLACE_26 SortTag list1 = new SortTag(1, "Gyan", 25); SortTag list2 = new SortTag(2, "Sumit", 26); SortTag list3 = new SortTag(5, "Hemant", 24);ADS_TO_REPLACE_27 SortTag list4 = new SortTag(6, "Rahul", 28); SortTag list5 = new SortTag(4, "Arun", 27); SortTag list6 = new SortTag(3, "Rohit", 26);ADS_TO_REPLACE_28 SortTag list7 = new SortTag(7, "Anand", 27); list.add(list1); list.add(list2);ADS_TO_REPLACE_29 list.add(list3); list.add(list4); list.add(list5);ADS_TO_REPLACE_30 list.add(list6); list.add(list7); return SUCCESS;ADS_TO_REPLACE_31 } public List<SortTag> getList() { return list;ADS_TO_REPLACE_32 } public void setList(List<SortTag> list) { this.list = list;ADS_TO_REPLACE_33 } } |
Here is the SortTag.java
package roseindia;ADS_TO_REPLACE_34 public class SortTag { private int id; private String name;ADS_TO_REPLACE_35 private int age; public SortTag(int id, String name, int age) { this.id = id;ADS_TO_REPLACE_36 this.name = name; this.age = age; }ADS_TO_REPLACE_37 public int getId() { return id; }ADS_TO_REPLACE_38 public void setId(int id) { this.id = id; }ADS_TO_REPLACE_39 public String getName() { return name; }ADS_TO_REPLACE_40 public void setName(String name) { this.name = name; }ADS_TO_REPLACE_41 public int getAge() { return age; }ADS_TO_REPLACE_42 public void setAge(int age) { this.age = age; }ADS_TO_REPLACE_43 } |
Here is the IdComparator.java
package roseindia; import java.util.Comparator;ADS_TO_REPLACE_44 import roseindia.SortTag; public class IdComparator implements Comparator<SortTag> { public int compare(SortTag list1, SortTag list2) {ADS_TO_REPLACE_45 return list1.getId() - list2.getId(); } } |
Here is the NameComparator.javaADS_TO_REPLACE_46
package roseindia; import java.util.Comparator; import roseindia.SortTag; ADS_TO_REPLACE_47 public class NameComparator implements Comparator<SortTag> { public int compare(SortTag list1, SortTag list2) { String s1 = list1.getName().toUpperCase();ADS_TO_REPLACE_48 String s2 = list2.getName().toUpperCase(); return s1.compareTo(s2); }ADS_TO_REPLACE_49 } |
This Program produces output on the basis of the Sort Tag evaluation, This give the output as-
Output:-ADS_TO_REPLACE_50
Download Select Source CodeADS_TO_REPLACE_51
Advertisements
Ads
Ads