【问题标题】:Passing a list of values back to controller将值列表传递回控制器
【发布时间】:2015-03-06 18:51:24
【问题描述】:

我有以下问题

  1. 我正在创建一个 List<UserProfileHelper> 的值
  2. 此列表添加到 ProfileUserCommand 以发送到视图 (profile.gsp) 进行渲染

如您所见,ProfileUserCommand 对象包含 2 个布尔值,用于控制 GSP 中复选框的状态。这部分工作完美,因为我可以看到 GSP 正确呈现,并且复选框正确标记/未标记。

当将表单提交回控制器时,我不知道如何使用来自 GSP 的更新值“重建”此列表,因此我可以获得更新数据的 List<UserProfileHelper>

这是我的课

UserProfileHelper.groovy

class UserProfileHelper {

    Long optionId
    String optionName
    boolean isSubMenu
    boolean hasAccess
    boolean canWrite

}

ProfileUserCommand.groovy

class ProfileUserCommand {

    String username
    List userProfile = [].withLazyDefault { return new UserProfileHelper() }

    static constraints = {
        username blank: false
        userProfile blank: true, nullable: true
        access blank: true, nullable: true
    }
}

profile.gsp(仅 GSP 的相关部分)

<g:each in="${command.userProfile}" var="option">
    <tr>
        <td>
            ${option.optionId}
        </td>
        <td>
            <g:if test="${!option.isSubMenu}">
                ${option.optionName}
            </g:if>
            <g:else>
                &nbsp;
            </g:else>
        </td>
        <td>
            <g:if test="${option.isSubMenu}">
                ${option.optionName}
            </g:if>
            <g:else>
                &nbsp;
            </g:else>
        </td>
        <td>
            <g:checkBox bean="${option}" name="access" value="${option.hasAccess}"/>
        </td>
        <td>
            <g:checkBox bean="${option}" name="write" value="${option.canWrite}"/>
        </td>
    </tr>
</g:each>

提前致谢!

【问题讨论】:

    标签: java grails groovy gsp


    【解决方案1】:

    您只需将命令添加到控制器方法的参数中即可。

    例如:

    class YourController {
      def submitAction(ProfileUserCommand command) {
        // do something with command
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2014-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-13
      • 1970-01-01
      • 2016-05-15
      • 1970-01-01
      相关资源
      最近更新 更多