【问题标题】:How to retrieve the elements of HashSet using Struts2 tag without using s:iterator如何在不使用 s:iterator 的情况下使用 Struts2 标签检索 HashSet 的元素
【发布时间】:2017-05-10 20:13:09
【问题描述】:

我正在使用 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(动作类)

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

Customer.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 - 查看

<!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 对象值。

【问题讨论】:

    标签: java jsp struts2 type-conversion ognl


    【解决方案1】:

    您可以使用() 表示法代替[]。最后一个用于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了解更多关于类型转换的信息。

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

    【讨论】:

    • 感谢罗曼的宝贵时间。我通过将 id 属性添加到 Pojo 类并设置 id 值并在 jsp 中使用 () 而不是 [] 进行了更改,如您的回答中所述。但不幸的是它不起作用。
    • 嗯,根据链接到这个答案的文件,我认为它可以工作。如果我需要添加一些内容来更新我的答案,请告诉我。此外,您可能会发现类似的answer 对同一主题很有帮助。
    【解决方案2】:

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

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

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

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

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

    【讨论】:

      【解决方案3】:

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

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

      或者

      Set改成List,List有一个get这个set没有的方法。

      之后,以下两个都可以工作

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

      【讨论】:

        猜你喜欢
        • 2015-06-02
        • 2011-07-08
        • 2020-09-18
        • 2021-04-23
        • 1970-01-01
        • 2021-04-14
        • 2022-12-05
        • 1970-01-01
        • 2021-09-15
        相关资源
        最近更新 更多