如何映射对象列表以进行编辑和保存
我正在尝试编辑实体列表,然后更新或创建它们。 在我的控制器中,我有:
public static void editTargets(@Required @Min(2011) Integer year, @Required String type, @Required Long groupId) {
RetailRegion region = RetailRegion.findById(groupId);
notFoundIfNull(region);
TargetType tType = TargetType.valueOf(type);
notFoundIfNull(tType);
List<Target> targets = Target.findByGroupAndTypeAndYear(region, tType, year);
if (targets != null && !targets.isEmpty()) {
render(targets, year, tType, region);
}
createTargets(year, type, groupId);
}
public static void createTargets(@Required @Min(2011) Integer year, @Required String type, @Required Long groupId) {
RetailRegion region = RetailRegion.findById(groupId);
notFoundIfNull(region);
TargetType tType = TargetType.valueOf(type);
notFoundIfNull(tType);
render(region, year, tType);
}
public static void saveTargets(@Required List<Target> targets) {
notFoundIfNull(targets);
for (Target target : targets) {
if (target != null)
target.save();
}
flash.success("Targets have been saved.");
if (params.get("_save") != null) {
mgmt();
}
mgmt();
}
和我的 editTargets 模板:
#{form id:'targetsForm', method:'POST', action:@saveTargets()}
<section id="targets">
<table width="750px" id="targetsTable">
<thead>
<tr>
<th>Code</th>
<th>January</th>
</tr>
</thead>
<tbody>
%{int i = 0;}%
#{list items:targets, as:'target'}
<tr>
#{field 'target.id'}
<input id="${field.id}" type="hidden" name="${field.name}" value="${field.value}" class="${field.errorClass}" />
#{/field}
<td class="center">${targets[i].code}</td>
<td class="center">
#{field 'target.jan'}
<input id="${field.id}" type="number" name="${field.name}" value="${field.value}" class="${field.errorClass}" />
#{/field}
</td>
...
我遇到的问题是我似乎无法在 saveTargets 控制器方法中映射回已编辑的目标。
控制器会将字段映射回每个字段的字符串数组(即 target.jan --> String[]),而不是 List
有没有办法将我的对象映射回列表?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您似乎正在循环查看视图中的目标,因此您的字段标记应包含它们位于列表/数组中的事实。有趣的是,target.jan 以某种方式作为字符串数组出现。我认为您的字段标记应该如下所示:
上面是针对“jan”属性的,因此您必须对视图中的任何其他目标属性(例如 id)执行相同的操作
You seem to be looping through targets in your view so your field tags should include the fact that they're in a list/array. It's interesting that target.jan is somehow coming though as a String array though. I think your field tag should look something like this:
The above is for the 'jan' property, so you'll have to do the same for any other target properties you have in the view (e.g. id)