简体   繁体   English

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

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

I am using Struts2.我正在使用 Struts2。 Please help me in understanding how to retrieve the elements of HashSet using Struts2 tag without using Struts iterator tag.请帮助我理解如何在不使用 Struts 迭代器标签的情况下使用 Struts2 标签检索HashSet的元素。

struts.xml 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) Process.java(动作类)

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 Cutomer.java - Pojo 类

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

With above code, I am not able to read HashSet object value in result.jsp page.使用上面的代码,我无法在result.jsp页面中读取HashSet对象值。

You can use () notation instead of [] .您可以使用()表示法代替[] The last is for List and array, or Map .最后一个用于List和数组,或Map So it's not applicable to Set .所以它不适用于Set

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

You should add id property to the element's type.您应该将id属性添加到元素的类型。 The id is a default key to map through a collection. id是映射到集合的默认键。

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

Initialize the id property along with others与其他人一起初始化id属性

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 .还有用于理解索引的OGNL 开发指南。

How to retrieve the elements of HashSet using Struts2 tag without using <s:iterator> ?如何在不使用<s:iterator>情况下使用 Struts2 标签检索HashSet的元素?

By using some OGNL magic called projection.通过使用一些称为投影的 OGNL 魔法。

<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.result.{name}将创建来自所有列表name中的值result[0]将检索列表的第一个元素。

Note that because you are using HashSet iteration order is not guaranteed.请注意,因为您使用的是HashSet ,所以无法保证迭代顺序。 Use LinkedHashSet to achieve predictable iteration order.使用LinkedHashSet实现可预测的迭代顺序。

If you want to keep the Set you can use如果你想保留你可以使用的Set

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

Or或者

Change the Set to List , the List has a get method which the set does not .Set更改为List ,List 具有 set 没有的get方法。

After that both below will work之后,以下两个都将起作用

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM