Struts 2 Sort tag sample code

Struts 2 Sort tag sample code

To sort list on JSP page in struts 2 application. Struts 2 provide sort tag which sort the list using comparator. Please see example below:

  • Create maven project: Struts2Iterator
  • Final project structure:

Struts 2 Sort tag sample code

  • IteratorTagExampleAction.java
package com.javahonk.action;

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

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.opensymphony.xwork2.ActionSupport;

@Results({
        @Result(name = "error", location = "/iterator.jsp"),
        @Result(name = "input", location = "/iterator.jsp")})
public class IteratorTagExampleAction extends ActionSupport {

    private static final long serialVersionUID = 1L;
    private List<Person> personList = new ArrayList<Person>();
    
    @Override
    @Action(value = "/iteratorTagExampleAction")    
    public String execute() throws Exception {
        
        personList.add(new Person("Java", "Honk", "NY"));
        personList.add(new Person("Ramesh", "Arrepu", "NY"));
        personList.add(new Person("Igor", "Vornovitsky", "NY"));
        personList.add(new Person("Bob", "Sidebottom", "NY"));
        personList.add(new Person("Sunil", "Garg", "NY"));
        personList.add(new Person("Mike", "Lee", "NY"));
        
        return ActionSupport.INPUT;
    }

    public List<Person> getPersonList() {
        return personList;
    }

    public void setPersonList(List<Person> personList) {
        this.personList = personList;
    }   
    
}

  • Person.java
package com.javahonk.action;

import java.io.Serializable;

public class Person implements Serializable{
    
    private static final long serialVersionUID = 1L;
    private String firstName;
    private String lastName;
    private String location;    
    
    public Person(String firstName, String lastName, String location) {
        super();
        this.firstName = firstName;
        this.lastName = lastName;
        this.location = location;
    }
    public String getFirstName() {
        return firstName;
    }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    public String getLastName() {
        return lastName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
    public String getLocation() {
        return location;
    }
    public void setLocation(String location) {
        this.location = location;
    }

}
  • PersonComparator.java:
package com.javahonk.action;

import java.util.Comparator;

public class PersonComparator implements Comparator<Person>{

    public int compare(Person o1, Person o2) {
        return o1.getFirstName().compareTo(o2.getFirstName());
    }
}
  • iterator.jsp:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Struts 2 s:iterator tag sample code</title>
<s:head />
</head>

<body>
    <h2 style="color: green">
        <s:text name="label.welcome" />
    </h2>

    <h3 style="color: green">
        <s:text name="label.iterator" />
    </h3>
    <s:form action="iteratorTagExampleAction" namespace="/" method="post"
        name="strutsForm">
        <table cellspacing="0" cellpadding="0" border="1" width="500">
            <tr style="font-weight: bold">
                <td><s:text name="label.firstName" /></td>
                <td><s:text name="label.lastName" /></td>
                <td><s:text name="label.location" /></td>
            </tr>
            <s:iterator value="personList">
                <tr>
                    <td><s:property value="firstName" /></td>
                    <td><s:property value="lastName" /></td>
                    <td><s:property value="location" /></td>
                </tr>
            </s:iterator>
        </table>

        <h3 style="color: green">
            <s:text name="label.iterator.sort" />
        </h3>
        <s:bean name="com.javahonk.action.PersonComparator"
            var="personComparator" />

        <table cellspacing="0" cellpadding="0" border="1" width="500">
            <tr style="font-weight: bold">
                <td><s:text name="label.firstName" /></td>
                <td><s:text name="label.lastName" /></td>
                <td><s:text name="label.location" /></td>
            </tr>
            <s:sort comparator="personComparator" source="personList">
                <s:iterator>
                    <tr>
                        <td><s:property value="firstName" /></td>
                        <td><s:property value="lastName" /></td>
                        <td><s:property value="location" /></td>
                    </tr>
                </s:iterator>
            </s:sort>
        </table>
    </s:form>
</body>
</html>
  • Web page output:

Struts 2 Sort tag sample code

For more information please read this tutorial

download Download Project:  Struts2Iterator

Leave a Reply

Your email address will not be published. Required fields are marked *

I am not Robot *