我正在使用Struts2。请帮助我理解如何在不使用Struts标记的情况下使用HashSet
标记检索Struts2元素。
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;
}
}
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 -视图
<!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>
使用上面的代码,我无法在HashSet
页面中读取result.jsp
对象值。
发布于 2016-12-27 20:07:15
如何在不使用HashSet
<s:iterator>
**?**的情况下使用Struts2标记检索的元素
通过使用一些叫做投影的OGNL魔法。
<s:textfield id="custName" value="%{result.{name}[0]}" />
result.{name}
将从result
中的所有name
值创建一个列表,[0]
将检索该列表的第一个元素。
注意,由于您使用的是HashSet
迭代顺序,所以不能保证顺序。使用LinkedHashSet
实现可预测的迭代顺序。
https://stackoverflow.com/questions/41329957
复制相似问题