将自定义对象数组POST到Struts 2操作

6

如何在Java中将自定义对象数组POST到Struts 2操作?

例如,如果我有以下Java对象:

public class Person {

    private String name;
    private String lastName;

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getLastName() {
        return lastName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }   
}

接下来的操作:

public class SavePersons extends ActionSupport {

    private List<Person> persons;

    @Override
    public String execute() throws Exception {
            // Do something
        return SUCCESS;
    }

    public void setPersons(List<Person> persons) {
        this.persons = persons;
    }

}

我希望能在HTML表单中实现以下类似功能:
<html>
<body>
<form method="POST" action="http://postHere">
    <input type="text" name="persons[0].name" value="Name1"/>
    <input type="text" name="persons[0].lastName" value="LastName1"/>
    <input type="text" name="persons[1].name" value="Name2"/>
    <input type="text" name="persons[1].lastName" value="LastName2"/>
    <input type="submit" />
</form>
</body>
</html>

有什么技巧吗?


2
"四元数已经回答了你的问题。而你的问题与 Struts2 的表格输入有关。" - lschin
1个回答

8
你的代码看起来很不错。对于设置值而言,post请求和get请求在struts2中没有区别。
使用相同的SavePersons类,只是我添加了一个public List<Person> getPersons()方法。这是必须的才能使解决方案正常运行。
并且使用基本相同的表单,只是我更喜欢在适当的情况下使用s2标签编写表单(一些人对表单标签的默认s2主题感到不满意,你可以全局设置主题为simple,label属性将不起作用,但UI标签将像你期望的html元素一样工作)。
<%@taglib prefix="s" uri="/struts-tags"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html> 
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Person Form</title>
    </head>
    <body>
        <h1>Person Form</h1>
        <s:form action="person-test" method="post">
            <s:textfield name="persons[0].name" label="fName 1"/>
            <s:textfield name="persons[0].lastName"  label="lName 1"/>
            <s:textfield name="persons[1].name" label="fName 2"/>
            <s:textfield name="persons[1].lastName" label="lName 2"/>
            <s:submit/>
        </s:form>
    </body>
</html>

请注意,method="post"并非必要,它是默认值。
以下是用于展示表单数据的页面。
<%@taglib prefix="s" uri="/struts-tags"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html> 
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1>List of People</h1>
        <s:iterator value="persons">
            <s:property value="name"/> <s:property value="lastName"/><br/>
        </s:iterator>
    </body>
</html>

它完美地运行了。


1
谢谢你的回答。我的问题是,如果我不声明一个getter“getPersons”(因为我不想在以后读取列表),它就无法工作,并且persons列表在POST时永远不会被填充。 - Mark
你能使用迭代器来创建表单吗? - manafire
@Quaternion:@Mark说得对,getPersons getter是你的解决方案必须的。实际上,setter不会设置persons,getter将用于获取persons列表,然后所有元素都将开始填充。昨天我花了4个多小时(最后一个小时和我的4个同事一起)试图弄清楚为什么它不起作用,你的答案“getPersons只能读取…”误导了我们。最终添加此getter解决了问题。 - ahmehri
1
@ahmehri,我不知道当时在想什么,设置集合时确实需要getter。如果我的早期评论会误导某些人,我会将其删除。 - Quaternion

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接