s:sort:
The s:sort tag is used to sort a List by using java.util.Comparator.
Syntax:
<s:sort comparator=”#comparatorName” source=”fieldName”>
Example:
index.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>Struts 2 s:sort control tag example</title> <META HTTP-EQUIV="Refresh" CONTENT="0;URL=Test.action"> </head> <body> </body> </html> |
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>struts2</filter-name> <filter-class> org.apache.struts2.dispatcher.ng. filter.StrutsPrepareAndExecuteFilter </filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app> |
struts.xml
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="default" extends="struts-default"> <action name="Test" class="com.w3schools.action.Test"> <result name="success">/test.jsp</result> </action> </package> </struts> |
Test.java
import java.util.ArrayList; import java.util.List; import com.opensymphony.xwork2.ActionSupport; /** * This class is used as an action class. * @author w3schools */ public class Test extends ActionSupport{ //data members private List<Subject> subjectList; //business logic public String execute(){ subjectList = new ArrayList<Subject>(); subjectList.add(new Subject("Java","1")); subjectList.add(new Subject("Networking","2")); subjectList.add(new Subject("Compiler","3")); subjectList.add(new Subject("DBMS","4")); return SUCCESS; } //getter setters public List<Subject> getSubjectList() { return subjectList; } public void setSubjectList(List<Subject> subjectList) { this.subjectList = subjectList; } } |
SubjectNameComparator.java
import java.util.Comparator; /** * This class is used to compare the Subject * objects based on the subject name. * @author w3schools */ public class SubjectNameComparator implements Comparator<Subject>{ public int compare(Subject subject1, Subject subject2) { return subject1.getSubjectName(). compareTo(subject2.getSubjectName()); } } |
welcome.jsp
<%@ taglib uri="/struts-tags" prefix="s"%> <html> <head> <title>Struts 2 s:sort control tag example</title> </head> <body> <h3>This is a s:sort control tag example.</h3> <s:bean name="com.w3schools.action.SubjectNameComparator" var="subjectNameComparator" /> <h4>Iterator values</h4> <s:iterator value="subjectList"> Subject Name: <s:property value="subjectName"/> , Subject Id: <s:property value="subjectId"/><br/> </s:iterator> <s:sort comparator="#subjectNameComparator" source="subjectList"> <h4>Iterator values after sorting by subject name</h4> <s:iterator> Subject Name: <s:property value="subjectName"/> , Subject Id: <s:property value="subjectId"/><br/> </s:iterator> </s:sort> </body> </html> |
Output:
Download this example.
Next Topic: Struts 2 data tags with example.
Previous Topic: Struts 2 s:generator control tag with example.