gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 02:59:08 24 4
gpt4 key购买 nike

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

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 类

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>

使用上面的代码,我无法读取 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 .

关于java - 如何在不使用 s :iterator 的情况下使用 Struts2 标记检索 HashSet 的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41329957/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com