【问题标题】:Struts 2 String to Double Conversion on ValueStackStruts 2 String 到 ValueStack 上的双重转换
【发布时间】: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
用户名 = Saurabh

x = 21.0

【问题讨论】:

    标签: struts2


    【解决方案1】:

    因为这本书很老,而且只涉及 Struts 2.0。

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

    后来的版本提高了它们通过自省在类型之间自动转换的能力。

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

    【讨论】:

    • 如果可能的话,请把书推荐给我
    • @user3817863 there was an answer(那个 romanc 有删除的冲动),解释那本书(和其他)的哪些部分仍然被覆盖,哪些部分现在完全无法使用。不幸的是,你需要 10k rep 才能看到它,因为它已被删除:/
    • @AndreaLigios 公平地说,我的书涵盖了注释并且没有讨论 Dojo 标签 ;)
    猜你喜欢
    • 2016-02-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-08
    • 2013-11-10
    相关资源
    最近更新 更多