【发布时间】:2014-09-06 17:03:50
【问题描述】:
我是 Struts2 的新手,正在关注“Struts2 in Action”。我有一个问题
我的书说,如果您使用的数据类型与 String 不同,您需要告诉编译器它不再是 String 并在 List 的情况下使用 Element-weights=java.lang.Double 定义名为 DataTransferTest-conversion.properties 的属性文件(即您使用的是 Double 类型在您的列表中)。
但是,当我实际执行此操作时,我没有指定我的属性文件。仍然,它的工作。我不知道!!为什么?
我正在附加我的代码层。请看
动作类
public class ListExampleAction extends ActionSupport implements ModelDriven<ListExample> {
ListExample example = new ListExample();
private List<ListExample> listExample;
private Double x;
//Getters Setters//
@Action(value="/ListExample", results={
@Result(name="success",location="/listExampleDisplay.jsp"),
})
public String execute() throws Exception {
return "success";
}
显示动作类(只返回jsp)
public class DisplayListExampleAction extends ActionSupport{
@Action(value="/ListExampleAction", results={
@Result(name="success",location="/listExample.jsp"),
})
public String execute() throws Exception {
return "success";
}
}
列表类型的列表示例类
public class ListExample{
private String firstName;
private Double age;
//getter/setters//
}
listExample.jsp
<html>
<head></head>
<body>
<h1>Struts </h1>
<form action="ListExample">
// For List
<s:textfield name="listExample.firstName" label="User 1 Name"></s:textfield>
<s:textfield name="listExample.age" label="User 1 Age"></s:textfield>
// For x varaible in Action Class
<s:textfield name="x" label="x"></s:textfield>
<s:submit></s:submit>
</form>
</body>
</html>
listExampleDisplay.jsp
<html>
<head></head>
<body>
<h1>Struts</h1>
// For List
Users List =
<s:iterator value="listExample">
<s:property value="firstName" />
<s:property value="age" />
</s:iterator>
<br>
// For x varaible in Action Class
<s:property value="x"/>
</body>
</html>
输出:
用户年龄 =21
用户名 = Saurabhx = 21.0
【问题讨论】:
标签: struts2