Home Struts Struts Struts2.2.1 Tags Struts2.2.1 sort Tag Example

Ask Questions?

View Latest Questions


 
 

Struts2.2.1 sort Tag Example
Posted on: January 19, 2011 at 12:00 AM
Advertisement
In this tutorial, we will discuss about sort tag.The sort tag is used to sort a List using a java.util.Comparator.

Struts2.2.1 sort Tag Example

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.

<%@ 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>Example Sort Tag</title>

</head>

<body>

The UnSorted Values.<br>

<ol> <s:iterator value="list">

<LI><s:property value="id" /> ,

<s:property value="name" /> ,

<s:property value="age" /></LI>

</s:iterator> </ol>

<table> <tr>

<td bgcolor="#ffffcc">The Values Sorted by ID.<br>

<s:bean name="roseindia.IdComparator" var="MyId" />

<s:sort comparator="#MyId" source="list">

<ol> <s:iterator>

<LI><s:property value="id" /> ,

<s:property value="name" /> ,

<s:property value="age" /></LI>

</s:iterator> </ol>

</s:sort></td>

<td></td>

<td bgcolor="pink">The Values Sorted by Name.<br>

<s:bean name="roseindia.NameComparator" var="MyName" />

<s:sort comparator="#MyName" source="list">

<ol> <s:iterator>

<LI><s:property value="id" /> ,

<s:property value="name" /> ,

<s:property value="age" /></LI>

</s:iterator></ol>

</s:sort></td>

</tr> </table>

</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="SortTag" class="roseindia.SortTagAction">

<result name="success">/Sort.jsp</result>

</action>

</package>

</struts>

Here is the index.jsp

<%@ 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">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Example Sort Tag</title>

</head>

<body>

<h1>Example Sort Tag</h1>

<hr>

<a href="SortTag.action">Example Sort Tag</a>

</body>

</html>

The action class SortTagAction.java is as follows.

package roseindia;

import com.opensymphony.xwork2.ActionSupport;

import java.util.*;

public class SortTagAction extends ActionSupport {

private List<SortTag> list = new ArrayList<SortTag>();

public String execute() throws Exception {

SortTag list1 = new SortTag(1, "Gyan", 25);

SortTag list2 = new SortTag(2, "Sumit", 26);

SortTag list3 = new SortTag(5, "Hemant", 24);

SortTag list4 = new SortTag(6, "Rahul", 28);

SortTag list5 = new SortTag(4, "Arun", 27);

SortTag list6 = new SortTag(3, "Rohit", 26);

SortTag list7 = new SortTag(7, "Anand", 27);

list.add(list1);

list.add(list2);

list.add(list3);

list.add(list4);

list.add(list5);

list.add(list6);

list.add(list7);

return SUCCESS;

}

public List<SortTag> getList() {

return list;

}

public void setList(List<SortTag> list) {

this.list = list;

}

}

Here is the SortTag.java

package roseindia;

public class SortTag {

private int id;

private String name;

private int age;

public SortTag(int id, String name, int age) {

this.id = id;

this.name = name;

this.age = age;

}

public int getId() {

return id;

}

public void setId(int id) {

this.id = id;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public int getAge() {

return age;

}

public void setAge(int age) {

this.age = age;

}

}

Here is the IdComparator.java

package roseindia;

import java.util.Comparator;

import roseindia.SortTag;

public class IdComparator implements Comparator<SortTag> {

public int compare(SortTag list1, SortTag list2) {

return list1.getId() - list2.getId();

}

}

Here is the NameComparator.java

package roseindia;

import java.util.Comparator;

import roseindia.SortTag;

public class NameComparator implements Comparator<SortTag> {

public int compare(SortTag list1, SortTag list2) {

String s1 = list1.getName().toUpperCase();

String s2 = list2.getName().toUpperCase();

return s1.compareTo(s2);

}

}

This Program produces output on the basis of the Sort Tag evaluation, This  give the output as-

Output:-

 

Download Select Source Code

Related Tags for Struts2.2.1 sort Tag Example:


DMCA.com