java 如何以弹簧形式将数据绑定到列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33015654/
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
how to bind data to list in spring form
提问by Amit Das
I have a spring form with having backing object for it. The form is like this-
我有一个带有支持对象的弹簧形式。表格是这样的——
<sf:form cssClass="form-horizontal" commandName="campaignModel" method="post">
<sf:input path="campaign.name" class="form-control" />
<sf:input path="landingPageModels.landingPage.url" class="form-control" />
</sf:form>
Model class(Form backing Object) -
模型类(表单支持对象)-
CampaignModel.java
运动模型.java
public class CampaignModel {
private Campaign campaign = new CampaignImpl();
private List<LandingPageModel> landingPageModels = new Arraylist<LandingPageModel>;
public Campaign getCampaign() {
return campaign;
}
public void setCampaign(Campaign campaign) {
this.campaign = campaign;
}
public List<LandingPageModel> getLandingPageModels() {
return landingPageModels;
}
public void setLandingPageModels(List<LandingPageModel> landingPageModels) {
this.landingPageModels = landingPageModels;
}
LandingPageModel.javais -
LandingPageModel.java是 -
public class LandingPageModel {
private LandingPage landingPage = new LandingPageImpl();
private List<LandingPageParameterImpl> landingPageParameters = new ArrayList<LandingPageParameterImpl>();
public LandingPage getLandingPage() {
return landingPage;
}
public void setLandingPage(LandingPage landingPage) {
this.landingPage = landingPage;
}
public List<LandingPageParameterImpl> getLandingPageParameters() {
return landingPageParameters;
}
public void setLandingPageParameters(List<LandingPageParameterImpl> landingPageParameters) {
this.landingPageParameters = landingPageParameters;
}
}
LandingPage.javais -
LandingPage.java是 -
public class LandingPageImpl extends EntityImpl implements LandingPage {
private String url;
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
} }
So i want that i can insert many objects of landingPage (having their own url property) in landingPageModels list. That means i can have mulitple input tag having url property like this -
所以我希望我可以在landingPageModels 列表中插入许多landingPage 对象(有自己的url 属性)。这意味着我可以有多个输入标签具有这样的 url 属性 -
<sf:input path="landingPageModels.landingPage.url" class="form-control" />
<sf:input path="landingPageModels.landingPage.url" class="form-control" />
<sf:input path="landingPageModels.landingPage.url" class="form-control" />
But when executing this code, spring gives me error that landingPage property of landingPageModels has not getter setter method. How to solve it and how to take multiple value like this ?
但是在执行这段代码时,spring 给我错误,landingPageModels 的 landPage 属性没有 getter setter 方法。如何解决它以及如何像这样取多个值?
采纳答案by sh0rug0ru
In order to bind a list model property to multiple input fields, you need this in the rendered form:
为了将列表模型属性绑定到多个输入字段,您需要在呈现的表单中使用它:
<input type="text" name="landingPageModels[0].landingPage.url" class="form-control" />
<input type="text" name="landingPageModels[1].landingPage.url" class="form-control" />
<input type="text" name="landingPageModels[2].landingPage.url" class="form-control" />
Which is accomplished by:
这是通过以下方式完成的:
<c:forEach items="${campaignModel.landingPageModels}" varStatus="s">
<sf:input path="landingPageModels[${s.index}].landingPage.url" class="form-control" />
</c:forEach>
回答by Lovababu
<c:forEach items="${campaignModel.landingPageModels}" var="landingPage">
<sf:input path="landingPage.url" class="form- control" />
</c:forEach>
Will the above not works for you to view data? To get them in controller you may have to use dynamic table row concept in HTML or for each single LandingPage entry add to form bean object by clicking add button and render back.
以上不适合您查看数据吗?要将它们放入控制器中,您可能必须在 HTML 中使用动态表行概念,或者通过单击添加按钮并返回来为每个单独的 LandingPage 条目添加到表单 bean 对象中。
In my case Person
command object having List<Token>
property, in order to bind the list of tokens we have designed page as attached screen shot below, clicking on Add button hits the controller and add the each token List<Token>
and render back to same view and displays added token in list view, it facilitates to add multiple token for Person
.
在我的例子中,Person
命令对象具有List<Token>
属性,为了绑定我们设计的页面作为附加屏幕截图的令牌列表,点击添加按钮点击控制器并添加每个令牌List<Token>
并渲染回相同的视图并在列表中显示添加的令牌视图,它有助于为Person
.
回答by Shailendra
The error you are getting is correct as landingPageModels
is a list.
You would need to use index access like this landingPageModels[0].landingPage.url
.
If you have dynamic number of input/url, then you can use <c:forEach>
to create dynamic input variable names
您得到的错误与landingPageModels
列表一样正确。您需要像这样使用索引访问landingPageModels[0].landingPage.url
。如果您有输入/网址的动态数量,那么您可以使用<c:forEach>
创建动态输入变量名称
回答by Mihir Chauhan
I dont know how to do it with spring form lib input but if you want to bind using simple html input than you can bind list like this
我不知道如何使用 spring 表单库输入来实现,但是如果您想使用简单的 html 输入进行绑定,那么您可以像这样绑定列表
Simple.jsp
简单的.jsp
<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
</head>
<body>
<form:form method="post" action="save.html" modelAttribute="contactForm">
<table>
<tr>
<th>First Name</th>
<th>Last Name</th>
</tr>
<tr>
<td><input name="contacts[0].firstname" /></td>
<td><input name="contacts[0].lastname" /></td>
</tr>
<tr>
<td><input name="contacts[1].firstname" /></td>
<td><input name="contacts[1].lastname" /></td>
</tr>
</table>
<br/>
<input type="submit" value="Save" />
</form:form>
</body>
</html>
@controller :
@控制器 :
@RequestMapping(value = "/save", method = RequestMethod.POST)
public ModelAndView save(@ModelAttribute("contactForm") ContactForm contactForm) {
List<Contact> contacts = contactForm.getContacts();
if(null != contacts && contacts.size() > 0) {
ContactController.contacts = contacts;
for (Contact contact : contacts) {
System.out.printf("%s \t %s \n", contact.getFirstname(), contact.getLastname());
}
}
return new ModelAndView("show_contact", "contactForm", contactForm);
}
ContactForm.java
联系表格.java
import java.util.List;
public class ContactForm {
private List<Contact> contacts;
public List<Contact> getContacts() {
return contacts;
}
public void setContacts(List<Contact> contacts) {
this.contacts = contacts;
}
}
Contact.java
联系方式
public class Contact {
private String firstname;
private String lastname;
private String email;
private String phone;
public Contact() {
}
//getters and setters
}