如何在不使用 s:iterator 的情况下使用 Struts2 标签检索 HashSet 的元素

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

我正在使用 Struts2。请帮助我理解如何在不使用 Struts 迭代器标记的情况下使用 Struts2 标记检索 HashSet 的元素。

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(动作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 - 波乔 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 - 查看

<!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>

使用上面的代码,我无法读取 result.jsp 页面中的 HashSet 对象值。

您可以使用 () 表示法代替 []。最后一个用于 List 和数组,或 Map。所以不适用于Set.

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

您应该将 id 属性 添加到元素的类型中。 id 是通过集合进行映射的默认键。

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

初始化 id 属性 和其他

Customer cust1 = new Customer();

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

您可以在 Indexing a collection by a property of that collection 中了解有关类型转换的更多信息。

还有OGNL开发指南,用于理解Indexing

如果你想保留 Set 你可以使用

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

Set 更改为 List,List 具有集合没有的 get 方法。

之后下面两个都可以工作

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

如何在不使用 <s:iterator> 的情况下使用 Struts2 标签检索 HashSet 的元素?

通过使用一些称为投影的 OGNL 魔法。

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

result.{name} 将从 result 中的所有 name 值创建一个列表,[0] 将检索该列表的第一个元素。

请注意,因为您使用的是 HashSet 迭代顺序无法保证。使用 LinkedHashSet 实现可预测的迭代顺序。