POST自定义对象的数组一个Struts 2的动作 [英] POST an array of custom objects to a Struts 2 action

查看:183
本文介绍了POST自定义对象的数组一个Struts 2的动作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何使用自定义对象的数组Java中的Struts 2的动作?

How do I POST an array of custom objects to a Struts 2 action in Java?

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

For example if I have the following Java object:

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表单:

I'd like to do something like this in an HTML form:

<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>

任何提示?

推荐答案

您有什么好看。它不会有所作为,如果你发布或尽可能获得尽可能设定值Struts2的。

What you have looks good. It does not make a difference to struts2 if you post or get as far as setting values.

使用相同的SavePersons类(除了我添加了一个公开名单,LT;人&GT; getPersons()因为我想显示人的方法后)

Using the same SavePersons class (except that I added a public List<Person> getPersons() method since I wanted to display the persons later)

和使用基本相同的形式,只不过我preFER编写使用S2标签我的形式在有意义(什么使某些人关闭表单标签是默认的S2主题,可以全局设置主题,以简单,标签属性将不会工作,但UI标签将工作就像你所期望的类似HTML元素的行为):

And using essentially the same form, except I prefer to write my forms using s2 tags where it makes sense (what puts some people off the form tags is the default s2 theme, you can globally set the theme to simple, the label attribute will not work but the UI tags will work just like you'd expect similar html elements to behave):

<%@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>

请注意该方法=后是不需要的,它是默认的。

Note that method="post" is not needed, it is the default.

下面是用于显示形式数据的页面。

Here is the page used to display the form data.

<%@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>

和它的工作就好了。

这篇关于POST自定义对象的数组一个Struts 2的动作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆