How to retrieve the elements of HashSet using Struts2 tag without using s:iterator

1k views Asked by At

I am using Struts2. Please help me in understanding how to retrieve the elements of HashSet using Struts2 tag without using Struts iterator tag.

struts.xml

<struts>
<constant name="struts.devMode" value="true" />

<package name="bundle" extends="struts-default" namespace="/">

  <action name="fetchPage">
        <interceptor-ref name="defaultStack" />
        <result name="success">/jsp/page.jsp</result>
  </action>

  <action name="process" 
        class="sample.action.Process" 
        method="execute">
        <interceptor-ref name="defaultStack" />
        <result name="success">/jsp/result.jsp</result>
  </action>

</package>  
</struts>

Process.java (Action Class)

package sample.action;

import java.util.HashSet;
import java.util.Set;

import sample.pojo.Customer;

import com.opensymphony.xwork2.ActionSupport;

public class Process extends ActionSupport 
{
    private Set<Customer> result = new HashSet<Customer>();

    public String execute() 
    {
        Customer cust1 = new Customer();
        cust1.setAge(59);
        cust1.setName("Subramanian");
        result.add(cust1);

        return SUCCESS;
    }

    public Set<Customer> getResult() {
        return result;
    }

    public void setResult(Set<Customer> result) {
        this.result = result;
    }
}

Cutomer.java - Pojo class

package sample.pojo;
public class Customer{

    String name;
    int age;

    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;
    }   
}

result.jsp - View

<!DOCTYPE html>
<html>
<head>
<%@ taglib prefix="s" uri="/struts-tags"%>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta http-equiv="X-UA-Compatible" content="IE=8;" />
<title>Welcome Page</title>
</head>
<body>
Welcome!
 <s:textfield id="custName" value="%{result[0].name}"/>
</body>
</html>

With above code, I am not able to read HashSet object value in result.jsp page.

3

There are 3 answers

1
Aleksandr M On BEST ANSWER

How to retrieve the elements of HashSet using Struts2 tag without using <s:iterator>?

By using some OGNL magic called projection.

<s:textfield id="custName" value="%{result.{name}[0]}" />

The result.{name} will create a list from all name values in the result and the [0] will retrieve first element of that list.

Note that because you are using HashSet iteration order is not guaranteed. Use LinkedHashSet to achieve predictable iteration order.

2
Roman C On

You can use () notation instead of []. The last is for List and array, or Map. So it's not applicable to Set.

<s:textfield id="custName" value="%{result(0).name}"/>

You should add id property to the element's type. The id is a default key to map through a collection.

public class Customer{
    Integer id;
    String name;
    int age;
//getter and setter
}

Initialize the id property along with others

Customer cust1 = new Customer();

cust1.setId(0);
cust1.setAge(59);
cust1.setName("Subramanian");
result.add(cust1);

You can learn more about type conversion in Indexing a collection by a property of that collection.

Also OGNL development guide for understanding Indexing.

0
Alireza Fattahi On

If you want to keep the Set you can use

<s:property value="%{result.iterator.next.name}"/>

Or

Change the Set to List, the List has a get method which the set does not .

After that both below will work

<s:property value="%{result[0].name}"/>
<s:property value="%{result.get(0).name}"/>