当我用不同的角色更新我的用户时,我有两个错误:
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}" />
答案 0 :(得分:1)
HTML表单提交以字符(字符串)形式发送数据,即使对于<g:select
标记也是如此。并且String
和params.secRole
都获得params.secUser
。
但SecUserSecRole
需要SecRole
和SecUser
个实例。你无法为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