Java – post a set of custom objects to struts 2 actions

How to publish a set of custom objects to struts 2 operations in Java?

For example, if I have the following Java objects:

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 do the following:

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 want to do the following 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>

Do you have a tip?

Solution

What do you have that looks good If you publish or get the set value, it has no effect on struts 2

Use the same savepersons class, except that I added a public list < person > getpersons () method This is a necessary condition to make the solution effective

And it uses basically the same form, except that I prefer to use the S2 tag to write my form, which is meaningful (what makes some people break away from the form tag is the default S2 theme. You can set the theme globally. It is simple, and the tag attribute will not work, but the UI tag will work like the HTML element behavior you expect):

<%@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 required and is the default

This is the page used to display 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>

It works well

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>