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

时间:2021-12-06 17:41:17

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

如何在Java中将一组自定义对象发布到Struts 2操作?

For example if I have the following Java object:

例如,如果我有以下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;
    }   
}

And the following Action:

并执行以下操作:

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

}

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

我想在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>

Any tips?

1 个解决方案

#1


8  

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

你有什么看起来不错。如果您发布或获取设置值,它对struts2没有任何影响。

Using the same SavePersons class, except that I added a public List<Person> getPersons() method. This is required to make the solution work.

使用相同的SavePersons类,除了我添加了一个公共List getPersons()方法。这是使解决方案有效的必要条件。

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

并且使用基本相同的形式,除了我更喜欢使用s2标签编写我的表单,它有意义(什么使一些人脱离表单标签是默认的s2主题,你可以全局设置主题简单,标签属性不会工作,但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>

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

请注意,不需要method =“post”,它是默认值。

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>

And it worked just fine.

它运作得很好。

#1


8  

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

你有什么看起来不错。如果您发布或获取设置值,它对struts2没有任何影响。

Using the same SavePersons class, except that I added a public List<Person> getPersons() method. This is required to make the solution work.

使用相同的SavePersons类,除了我添加了一个公共List getPersons()方法。这是使解决方案有效的必要条件。

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

并且使用基本相同的形式,除了我更喜欢使用s2标签编写我的表单,它有意义(什么使一些人脱离表单标签是默认的s2主题,你可以全局设置主题简单,标签属性不会工作,但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>

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

请注意,不需要method =“post”,它是默认值。

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>

And it worked just fine.

它运作得很好。