gpt4 book ai didi

java - 尝试在 spring mvc 中创建和保存对象列表

转载 作者:行者123 更新时间:2023-11-30 07:02:40 24 4
gpt4 key购买 nike

这是我的模型类

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

这是表单类

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

下面是我的 Controller

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

这是我的 jsp 页面,名为 test2

<%@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>
&nbsp;
<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>

我知道以前有人问过这些问题,但由于我对 spring mvc 和学习还很陌生,所以我想在这项技术中获得一些安慰..

无论如何,我在这里要做的是保存多行/对象列表...但它没有保存在列表中...请帮我弄清楚我在做什么错误,并纠正我.

编辑还有一件事我想澄清一下,在我的第一个 Controller 中创建一个 dynamicRowForm 对象并添加到模型中,以便我的 jsp 页面可以访问它......在我的第二个 Controller 中使用同名的@ModelAttribute 接收 DynamicRowForm 对象。 .但这里得到不同的对象。为什么?

最佳答案

您的输入需要如下所示:

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

本质上说:在提交表单时,将 id 字段绑定(bind)到表单支持对象 - DynamicRowForm 中索引 0 处的 DynamicRow。

如果你这样做,那么下面应该打印值输入:

  @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?

因为它是无状态的。如果你想使用同一个实例,你需要将它存储在请求之间的 session 中。请参阅以下有用的讨论。

http://www.intertech.com/Blog/understanding-spring-mvc-model-and-session-attributes/

关于java - 尝试在 spring mvc 中创建和保存对象列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28947765/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com