Struts 2字符串在ValueStack上进行双重转换

时间:2014-09-06 17:03:51

标签: struts2

我是Struts2的新手,关注" Struts2 in Action"。我有一个问题

我的书说如果你使用的数据类型不同于String,你需要告诉编译器它不再是String并且在List的情况下定义名为DataTransferTest-conversion.properties的属性文件Element-weights=java.lang.Double(即你在你的列表中使用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>

输出:

  

UserAge = 21
  用户名= Saurabh

     

x = 21.0

1 个答案:

答案 0 :(得分:0)

因为这本书很老,只涵盖了Struts 2.0。

当时许多类型 需要转换规范。

后来的版本通过内省提高了自动转换类型的能力。

IIRC,早期的书也没有涵盖一些后来的书所涵盖的注释。