【发布时间】: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