java 尝试在 spring mvc 中创建和保存对象列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28947765/
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
Trying to create and save list of objects in spring mvc
提问by Sharique
This is my model class
这是我的模型课
public class DynamicRow {
private String id;
private String name;
private String email;
public DynamicRow(String id, String name, String email) {
this.id = id;
this.name = name;
this.email = email;
}
public DynamicRow() {
// TODO Auto-generated constructor stub
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "DynamicRow [id=" + id + ", name=" + name + ", email=" + email
+ "]";
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
This is the form class
这是表单类
public class DynamicRowForm {
private List<DynamicRow> dynamicRow = LazyList.decorate(new ArrayList<DynamicRow>(), FactoryUtils.instantiateFactory(DynamicRow.class));
public DynamicRowForm() {
}
public List<DynamicRow> getDynamicRow() {
return dynamicRow;
}
public void setDynamicRow(List<DynamicRow> dynamicRow) {
this.dynamicRow = dynamicRow;
}
}
Below is my controllers
下面是我的控制器
@RequestMapping(value="/createDetails")
public String list(Model model){
DynamicRowForm dynamicRowForm = new DynamicRowForm();
model.addAttribute("DynamicRowForm",dynamicRowForm);
return "test2";
}
@RequestMapping(value="/save")
public String showList(Model model,@ModelAttribute("DynamicRowForm") DynamicRowForm dynamicRowForm) {
System.out.println(dynamicRowForm.getDynamicRow());
return "success";
}
And this is my jsp page named as test2
这是我的名为 test2 的 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"%>
<!DOCTYPE html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
var rowCount = 1;
function addMoreRows(form) {
rowCount ++;
var recRow = '<p id="rowCount'+rowCount+'"><tr><td><input name="" type="text" size="17%" maxlength="120" /></td><td><input name="" type="text" maxlength="120" style="margin: 4px 5px 0 5px;"/></td><td><input name="" type="text" maxlength="120" style="margin: 4px 10px 0 0px;"/></td></tr> <a href="javascript:void(0);" onclick="removeRow('+rowCount+');">Delete</a></p>';
$('#addedRows').append(recRow);
}
function removeRow(removeNum) {
$('#rowCount'+removeNum).remove();
}
</script>
</head>
<body>
<form:form action="save" method="GET" modelAttribute="DynamicRowForm">
<input type="button" onclick="addMoreRows(this.form);" value="AddRow">
<table rules="all" style="background:#fff;">
<tr>
<td style="font-size:14px;" >Name</td>
<td style="font-size:14px;">Email</td>
<td style="font-size:14px;">Mobile</td>
<!-- <td><span style="font:normal 12px agency, arial; color:blue; text-decoration:underline; cursor:pointer;" onclick="addMoreRows(this.form);">
Add More
</span>
</td> -->
</tr>
<tr id="rowId">
<td><form:input path="${dynamicRow.id}" type="" size="17%"/></td>
<td><form:input path="${dynamicRow.name}" type="text" /></td>
<td><form:input path="${dynamicRow.email}" type="text" /></td>
</table>
<div id="addedRows"></div>
<input type="submit" value="Save">
</form:form>
</body>
I know these questions have been asked before, but as am very new to spring mvc and learning things so am trying to get some comfort in this technology..
我知道之前已经问过这些问题,但是由于我对 spring mvc 和学习东西很陌生,所以我试图在这项技术中获得一些安慰..
Anyway, what here I am trying to do is to save multiple row/list of object... but its not getting saved in the list... Please help me out on what is the mistake am doing, and correct me.
无论如何,我在这里要做的是保存对象的多行/列表......但它没有保存在列表中......请帮助我了解我在做什么,并纠正我。
EditAnd one more thing I would like to clarify, in my first controller am creating a dynamicRowForm object and adding into model so that my jsp page can access it...and in my second controller am receiving DynamicRowForm object using @ModelAttribute with same name..but here am getting different object.why?
编辑还有一件事我想澄清一下,在我的第一个控制器中,我正在创建一个 dynamicRowForm 对象并添加到模型中,以便我的 jsp 页面可以访问它……而在我的第二个控制器中,我正在使用具有相同名称的 @ModelAttribute 接收 DynamicRowForm 对象..但这里有不同的对象。为什么?
回答by Alan Hay
Your inputs need to look like this:
您的输入需要如下所示:
<form:form action="save" method="POST" modelAttribute="DynamicRowForm">
...
<td><form:input path="dynamicRow[0].id" type="text" /></td>
<td><form:input path="dynamicRow[0].name" type="text" /></td>
<td><form:input path="dynamicRow[0].email" type="text" /></td>
...
</form:form>
Which essentially states: on form submission bind the id field to the DynamicRow at index 0 in the form backing Object - DynamicRowForm.
其本质上是:在表单提交时,将 id 字段绑定到表单支持对象 - DynamicRowForm 中索引 0 处的 DynamicRow。
If you do this, then the following should print the values input:
如果你这样做,那么下面应该打印值输入:
@RequestMapping(value="/save")
public String showList(@ModelAttribute DynamicRowForm dynamicRowForm) {
System.out.println(dynamicRowForm.getDynamicRow().get(0).getId());
System.out.println(dynamicRowForm.getDynamicRow().get(0).getName());
System.out.println(dynamicRowForm.getDynamicRow().get(0).getEmail());
return "success";
}
And one more thing I would like to clarify, in my first controller am creating a dynamicRowForm object and adding into model so that my jsp page can access it...and in my second controller am receiving DynamicRowForm object using @ModelAttribute with same name..but here am getting different object.why?
还有一件事我想澄清一下,在我的第一个控制器中,我正在创建一个 dynamicRowForm 对象并添加到模型中,以便我的 jsp 页面可以访问它……而在我的第二个控制器中,我正在使用同名的 @ModelAttribute 接收 DynamicRowForm 对象。 .但这里有不同的对象。为什么?
Because it is stateless. If you want to work with the same instance you will need to store it in the session between requests. See the following useful discussion.
因为它是无状态的。如果你想使用同一个实例,你需要将它存储在请求之间的会话中。请参阅以下有用的讨论。
http://www.intertech.com/Blog/understanding-spring-mvc-model-and-session-attributes/
http://www.intertech.com/Blog/understanding-spring-mvc-model-and-session-attributes/
回答by Aninda Bhattacharyya
You are missing indexing here as you are using collection. Try...
当您使用集合时,您在这里缺少索引。尝试...
<form:input path="${DynamicRowForm.dynamicRow[0].id}" type="" size="17%"/>
and also in your addRow(), you have to use the dynamic index with row count. If it is not working with form:input
, try simple html input type.
并且在您的 addRow() 中,您必须使用带有行数的动态索引。如果它不适用于form:input
,请尝试简单的 html 输入类型。