如何映射对象列表以进行编辑和保存

发布于 2024-12-28 18:23:43 字数 2386 浏览 0 评论 0 原文

我正在尝试编辑实体列表,然后更新或创建它们。 在我的控制器中,我有:

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

有没有办法将我的对象映射回列表?

I am trying to edit a list of entities and update or create them afterwards.
In my controller I have:

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

and my editTargets template:

  #{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>
          ...

The problem I have is that I do not seem to be able to map back the edited targets in the saveTargets controller method.

The controller will map the fields back to a String array for each field (i.e. target.jan --> String[]) instead of a List<Targets>.

Is there a way to map my object back to a list?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

饮惑 2025-01-04 18:23:43

您似乎正在循环查看视图中的目标,因此您的字段标记应包含它们位于列表/数组中的事实。有趣的是,target.jan 以某种方式作为字符串数组出现。我认为您的字段标记应该如下所示:

%{ fieldName = "target[${i}].jan" }%
#{field "${fieldName}"}
    ...
#{/field}

上面是针对“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:

%{ fieldName = "target[${i}].jan" }%
#{field "${fieldName}"}
    ...
#{/field}

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文