Spring安全性无法将类型[java.lang.String]的值转换为属性“secRole”所需的类型[SecRole]

时间:2014-02-24 04:17:53

标签: java grails spring-security

当我用不同的角色更新我的用户时,我有两个错误:

Cannot convert value of type [java.lang.String] to required type [rms.SecUser] for property 'secUser': no matching editors or conversion strategy found]

Cannot convert value of type [java.lang.String] to required type [rms.SecRole] for property 'secRole': no matching editors or conversion strategy found]

我正在使用grails spring安全插件。

这是我的控制器:

def updateSecUserSecRole(Long id, Long version) {
    def secUserSecRoleInstance = SecUserSecRole.get(id)
    if (!secUserSecRoleInstance) {
        flash.message = message(code: 'default.not.found.message', args: [message(code: 'secUserSecRole.label', default: 'SecUserSecRole'), id])
        redirect(action: "listSecUserSecRole")
        return
    }

    if (version != null) {
        if (secUserSecRoleInstance.version > version) {
            secUserSecRoleInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
                      [message(code: 'secUserSecRole.label', default: 'SecUserSecRole')] as Object[],
                      "Another user has updated this SecUserSecRole while you were editing")
            redirect(action: "listSecUserSecRole")
            return
        }
    }

    secUserSecRoleInstance.properties = params

    if (!secUserSecRoleInstance.save(flush: true)) {
        secUserSecRoleInstance.errors.allErrors.each{
            println  it
        } 

        redirect(action: "listSecUserSecRole")
        return
    }

    flash.message = message(code: 'default.updated.message', args: [message(code: 'secUserSecRole.label', default: 'SecUserSecRole'), secUserSecRoleInstance.id])
    redirect(action: "listSecUserSecRole")
}

以下是编辑gsp中我的表单字段的片段:

<g:select class="select2-edit" style="width:30% !important;" id="secUser.id" name="secUser" from="${rms.SecUser.list()}" optionKey="id" optionValue="username" required="" value="${secUserSecRoleInstance?.secUser?.id}" />

<g:select id="secRole.id" name="secRole" from="${rms.SecRole.list()}" optionKey="id" optionValue="authority" required="" value="${secUserSecRoleInstance?.secRole?.id}" />

2 个答案:

答案 0 :(得分:1)

HTML表单提交以字符(字符串)形式发送数据,即使对于<g:select标记也是如此。并且Stringparams.secRole都获得params.secUser

SecUserSecRole需要SecRoleSecUser个实例。你无法为secUserSecRole分配字符串(这就是你用secUserSecRoleInstance.properties = params做的事情。也是不安全的),你只需要加载实际数据并传递这些对象。

像:

secUserSecRoleInstance.secUser = SecUser.get(params.secUser)
secUserSecRoleInstance.secRole = SecRole.get(params.secRole)

答案 1 :(得分:1)

我怀疑,在您的表单中,名称应与ID匹配。所以,

 id="secUser.id" name="secUser.id" 
 id="secRole.id" name="secRole.id"

更具体地说,表单是使用name值而不是id

提交的