java 将一组自定义对象发布到 Struts 2 操作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6313790/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
POST an array of custom objects to a Struts 2 action
提问by Mark
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?
有小费吗?
回答by Quaternion
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 类,只是我添加了一个public List<Person> 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.
它工作得很好。