如何在spring mvc中将嵌套列表元素传递给控制器
Posted
技术标签:
【中文标题】如何在spring mvc中将嵌套列表元素传递给控制器【英文标题】:How to pass nested list element to the controller in spring mvc 【发布时间】:2016-08-22 00:48:44 【问题描述】:在我的 jsp 页面中有一个带有嵌套列表值的表,我想将该表值发送到容器,外部表值已发送但内部表值未发送到容器,我是新来的,请让我知道如何克服这种情况,
我的jsp
<script>
function rowAdded(rowElement)
//clear the imput fields for the row
$(rowElement).find("input").val('');
//may want to reset <select> options etc
//in fact you may want to submit the form
saveNeeded();
function rowRemoved(rowElement)
saveNeeded();
function saveNeeded()
$('#submit').css('color','red');
$('#submit').css('font-weight','bold');
if( $('#submit').val().indexOf('!') != 0 )
$('#submit').val( '!' + $('#submit').val() );
function beforeSubmit()
alert('script Working');
return true;
$(document).ready( function()
var config =
rowClass : 'rule',
addRowId : 'addRule',
removeRowClass : 'removeRule',
formId : 'ruleListForm',
rowContainerId : 'ruleListContainer',
indexedPropertyName : 'ruleList',
indexedPropertyMemberNames : 'id,ruleName,parameterName,overwriteValue',
rowAddedListener : rowAdded,
rowRemovedListener : rowRemoved,
beforeSubmit : beforeSubmit
;
new DynamicListHelper(config);
);
</script>
<html>
<form:form action="/update" method="post" id="ruleListForm" modelAttribute="ruleListContainer">
<table border="1">
<thead>
<h3 align="center">Selected Rule</h3>
<tr>
<th data-field="id" >ID </th>
<th data-field="details" >RuleName </th>
<th data-field="parameter" >Parameter </th>
</tr>
</thead>
<tbody id="ruleListContainer">
<c:forEach items="$List2" var="as">
<tr class="rule">
<td><input type="hidden" name="ruleList[].id" value="$as.rule.id" /> $as.rule.id</td>
<td><input type="hidden" name="ruleList[].ruleName" value="$as.rule.ruleName" /> $as.rule.ruleName</td>
<td> <input id="one" class="datepicker" type="text" name="ruleList[].startDate" size="11" ></td>
<td> <input id="two" class="datepicker" type="text" name="ruleList[].endDate" size="11" ></td>
<td>
<table border="1">
<c:forEach items="$as.ruleAssignmentParameter" var="asss">
<tr>
<td><input type="hidden" name="ruleList[].parameterName"value="$asss.parameterName" > $asss.parameterName</td>
<td><input type="hidden" name="ruleList[].overwriteValue" value="$asss.overwriteValue" /> $asss.overwriteValue</td>
</tr>
</c:forEach>
</table>
</td>
</tr>
</c:forEach>
</tbody>
</table>
<br>
<input type="submit" value="Update">
</form:form>
</html>
这是我的模型类
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import com.demo.app.model.RuleAssignmentParameter;
public class RuleAssUI
private int id;
private String ruleName;
public int getId()
return id;
public void setId(int id)
this.id = id;
public String getRuleName()
return ruleName;
public void setRuleName(String ruleName)
this.ruleName = ruleName;
private List<RuleAssignmentParameter> ruleAssignmentParameter = new LinkedList<RuleAssignmentParameter>();
public List<RuleAssignmentParameter> getRuleAssignmentParameter()
return ruleAssignmentParameter;
public void setRuleAssignmentParameter(List<RuleAssignmentParameter> ruleAssignmentParameter)
this.ruleAssignmentParameter = ruleAssignmentParameter;
public RuleAssUI()
public RuleAssUI(int id,String ruleName)
this.id=id;
this.ruleName=ruleName;
我存储列表值的容器
import java.util.LinkedList;
import java.util.List;
public class RuleListContainer
private List<RuleAssUI> ruleList = new LinkedList<RuleAssUI>();
public RuleListContainer()
public RuleListContainer(List<RuleAssUI> ruleList)
this.ruleList = ruleList;
public List<RuleAssUI> getRuleList()
return ruleList;
public void setRuleList(List<RuleAssUI> ruleList)
this.ruleList = ruleList;
控制器
@RequestMapping(value = "/update", method = RequestMethod.POST)
public String update(@ModelAttribute("SpringWeb") RuleListContainer ruleListContainer, HttpSession session, ModelMap model)
ruleListContainer.getRuleList().size();
for (RuleAssUI rul1 : ruleListContainer.getRuleList())
System.out.println("Id: " + rul1.getId());
System.out.println("RuleName: " + rul1.getRuleName());
for (RuleAssignmentParameter rul2 : rul1.getRuleAssignmentParameter())
System.out.println("ParameterName: " + rul2.getParameterName());
System.out.println("ParameterValue: " + rul2.getOverwriteValue());
session.setAttribute("ruleListContainer", ruleListContainer);
return "hello";
我尝试了很多次但无法解决问题,也浏览了但没有得到任何适当的帮助,所以请帮忙做项目
我需要帮助,请有人帮助我...!
提前谢谢你
【问题讨论】:
这个列表来自哪里?我认为问题出在您的 jsp 文件中。 您需要设置每个列表元素的索引。
<c:forEach items="$List2" var="as" varStatus="vs">
<tr class="rule">
<td><input type="hidden" name="ruleList[$vs.index].id" value="$as.rule.id" /> $as.rule.id</td>
<td><input type="hidden" name="ruleList[$vs.index].ruleName" value="$as.rule.ruleName" /> $as.rule.ruleName</td>
<td> <input id="one" class="datepicker" type="text" name="ruleList[$vs.index].startDate" size="11" ></td>
<td> <input id="two" class="datepicker" type="text" name="ruleList[$vs.index].endDate" size="11" ></td>
<td>
<table border="1">
<c:forEach items="$as.ruleAssignmentParameter" var="asss" varStatus="assignments">
<tr>
<td><input type="hidden" name="ruleList[$vs.index].ruleAssignmentParameter[$assignments.index].parameterName" value="$asss.parameterName" > $asss.parameterName</td>
<td><input type="hidden" name="ruleList[$vs.index].ruleAssignmentParameter[$assignments.index].overwriteValue" value="$asss.overwriteValue" /> $asss.overwriteValue</td>
</tr>
</c:forEach>
</table>
</td>
</tr>
</c:forEach>
同样在您的控制器(POST 方法)中,您试图获取由“SpringWeb”标识的对象,但应该是“ruleListContainer”,与表单标签中的名称相同
问题是您在 RuleAssUI 中有一个列表,但您没有正确访问,您需要 2 个循环和索引,每个列表一个。
这里是关键:
ruleList[$vs.index].ruleAssignmentParameter[$assignments.index].parameterName
【讨论】:
@cralfaro,还是不行,parameterName & overwriteValue 实际上没有传递给容器 @cralfaro,添加索引后什么都没有发生,仍然没有将值传递给容器,先生,我该怎么办。?我尝试了数千次,但仍然无法解决问题 @Emad 还要确保您在表单标签中使用的对象的名称与您的 POST 方法中的 ModelAttribute 相同 @cralfaro,是的,它是一样的 id 和 ruleName 传给容器,我可以看到列表值,通过在 update servlet 中设置调试点,但是 parameterName 和 Overwritevalue 为空,以上是关于如何在spring mvc中将嵌套列表元素传递给控制器的主要内容,如果未能解决你的问题,请参考以下文章
如何在 Spring MVC (JPA) 中将实体对象列表转换为页面对象?
在 ASP.Net MVC (LINQ) 中将嵌套数据从控制器传递到视图