Sort tag example in struts2 framework

Sort tag example in struts2 framework

Previous Home Next

 

Struts2 framework provide a sort component to sorting the list and map element. if sort tag is used to sort a List and map element using a java.util.Comparator package.
<s:sort comparator="" source="">
<s:iterator>
     <s:property value="name" />
</s:iterator>
</s:sort>

The struts2 framework provide following merge tag example,
<s:sort comparator="#rollComparator" source="students">
<ol>
<s:iterator>
     <li><s:property value="name" />, 
         <s:property value="roll" />
     </li>
</s:iterator>
</ol>
</s:sort>
 
Directory Structure of <s:sort> tag Example in Struts 2.0 Using MyEclipse IDE



index.jsp

<%@taglib uri="/struts-tags" prefix="s"%>
<h2>Sort tag example in struts2 framework</h2>
<s:bean name="org.r4r.NameComparator" var="nameComparator" />
<s:bean name="org.r4r.RollComparator" var="rollComparator" />
<h3>Name wise Sorting through sort tag</h3>
<s:sort comparator="#nameComparator" source="students">
<ol>
<s:iterator>
     <li><s:property value="name" />, 
         <s:property value="roll" />
     </li>
</s:iterator>
</ol>
</s:sort>
<h3>Roll wise Sorting through sort tag</h3>
<s:sort comparator="#rollComparator" source="students">
<ol>
<s:iterator>
     <li><s:property value="name" />, 
         <s:property value="roll" />
     </li>
</s:iterator>
</ol>
</s:sort>

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.SortTagAction">
<result name="success" type="dispatcher">/index.jsp</result>
</action>
</package>
</struts>

NameComparator.java

package org.r4r;

import java.util.Comparator;

public class NameComparator implements Comparator<Student> {

	@Override
	public int compare(Student s1, Student s2) {
		String name1 = s1.getName().toUpperCase();
		String name2 = s2.getName().toUpperCase();
		return name1.compareTo(name2);
	}

}

RollComparator.java

package org.r4r;

import java.util.Comparator;

public class RollComparator implements Comparator<Student> {

	@Override
	public int compare(Student roll1, Student roll2) {
		
		return roll1.getRoll()-roll2.getRoll();
	}

}

Student.java

package org.r4r;

public class Student {
	String name;
	int roll;
	
	public Student(String name, int roll) {
		super();
		this.name = name;
		this.roll = roll;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getRoll() {
		return roll;
	}
	public void setRoll(int roll) {
		this.roll = roll;
	}
}

SortTagAction.java

package org.r4r;

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

public class SortTagAction {
	List<Student> students=new ArrayList<Student>();
	
	public String execute(){
		Student s1=new Student("Mukund",25);
		Student s2=new Student("Harsh",24);
		Student s3=new Student("Somaru",22);
		Student s4=new Student("Pramod",26);
		Student s5=new Student("Sushant",28);
		Student s6=new Student("Gaurav",35);
		Student s7=new Student("Sumit",34);
		students.add(s1);
		students.add(s2);
		students.add(s3);
		students.add(s4);
		students.add(s5);
		students.add(s6);
		students.add(s7);
		return "success";
	}

	public List<Student> getStudents() {
		return students;
	}

	public void setStudent(List<Student> students) {
		this.students = students;
	}

}

Output






Previous Home Next