Grails 方法的签名不适用于参数类型:()值:[]可能的解决方案:(java.lang.Object),(java.lang.Object)
我有一个脚本Grails 方法的签名不适用于参数类型:()值:[]可能的解决方案:(java.lang.Object),(java.lang.Object),grails,groovy,Grails,Groovy,我有一个脚本User.groovy和UserController.groovy。当我运行它时,我会 Caught: No signature of method: com.vasco.gs.User.addToUserRoles() is applicable for argument types: (com.vasco.gs.Role) values: [nurse] Possible solutions: addToUserRoles(java.lang.Object), addToUser
User.groovy
和UserController.groovy
。当我运行它时,我会
Caught: No signature of method: com.vasco.gs.User.addToUserRoles() is applicable for argument types: (com.vasco.gs.Role) values: [nurse] Possible solutions: addToUserRoles(java.lang.Object), addToUserRoles(java.lang.Object), getUserRoles()
我的域类
package com.vasco.gs
import com.vasco.gs.audit.UserAudit
class User {
static constraints = {
userName blank: false,size:2..25, unique: true
firstName blank:false, size:2..60,matches:"[a-zA-Z1-9_]+"
lastName blank:false, size:2..60,matches:"[a-zA-Z1-9_]+"
middleName nullable:false, blank:false, size:2..60,matches:"[a-zA-Z1-9_]+"
gender blank : true, nullable:true
emailId blank : false
mobileNumber blank : true, nullable:true
password nullable: false,blank: false,size:2..256,password:true
confirmPassword nullable: true, blank: false, size:2..256
activeStatus inList:['Y', 'N']
}
String userName
String password
String confirmPassword
String firstName
String lastName
String middleName
String emailId
String mobileNumber
Gender gender
String activeStatus = 'Y'
static hasMany = [userRoles:UserRole, userLocations: UserLocation]
static transients = [
'confirmPassword',
'activeUsers'
]
static List getActiveUsers(){
return User.findAllByActiveStatus('Y')
}
def beforeInsert() {
password = password.encodeAsSHA()
}
def activate(){
this.activeStatus = 'Y'
}
def inactivate(){
this.activeStatus = 'N'
}
def roles() {
return userRoles.collect { it.role }
}
def locations() {
return userLocations.collect { it.location }
}
List addToUserRoles(role){
UserRole.link this, role
return roles()
}
List removeFromUserRoles(role){
UserRole.unLink this, role
return roles()
}
List addToUserLocations(location){
UserLocation.link this, location
return locations()
}
List removeFromUserLocations(location){
UserLocation.unLink this, location
return locations()
}
String toString() {
"$firstName"
}
}
我的控制器
class UserController {
static allowedMethods = [save: "POST", update: "POST", delete: "POST"]
def UserService
def index() {
redirect(action: "list", params: params)
}
def list(Integer max) {
params.max = Math.max(max ?: User.count(), 1)
[userInstanceList: User.list(params), userInstanceTotal: User.count()]
}
def create() {
[roleList: Role.list(),locationList: Location.list(),userInstance: new User(params)]
}
def save() {
def userInstance = new User(params)
def userRole = params.userRoles
userRole.each {
def userrole = Role.get(it)
println userrole
def userRoles = userInstance.addToUserRoles(userrole)
}
if(UserService.validatePassword(userInstance,params.confirmPassword)){
if (!userInstance.save(flush: true)) {
render(view: "create", model: [userInstance: userInstance, userRoles:userRoles])
return
}
flash.message = message(code: 'default.created.message', args: [
message(code: 'user.label', default: 'User'),
userInstance.id
])
redirect(action: "list")
}
else
render(view: "create", model: [userInstance: userInstance])
}
我补充说
def userRole = params.userRoles
userRole.each {
def userrole = Role.get(it)
println userrole
def userRoles = userInstance.addToUserRoles(userrole)
}
我的用户控制器中的这段代码…我怀疑您的显式
addToUserRoles
方法采用对象
参数,而隐式方法采用GORM为支持hasMany
声明而添加的UserRole
参数,两者之间存在某种冲突。例如,尝试为显式方法使用不同的名称,以避免干扰GORM
List addRole(role){
UserRole.link this, role
return roles()
}
List removeRole(role){
UserRole.unLink this, role
return roles()
}
List addLocation(location){
UserLocation.link this, location
return locations()
}
List removeLocation(location){
UserLocation.unLink this, location
return locations()
}
我也有类似的问题。确保对象两侧的关系都正确编码,并将其修复。这里有一个指向GORM文档的链接,描述了1:m、m:m和1:1编码设置
可能您的域与您的数据库不同,请尝试更改Datasource.groovy以再次创建drop以重新创建表。您为什么要编写自己的
列表addToUserRoles(角色)
?如果不按预期的方式使用spring安全插件,您试图实现什么?而不是列表addToUserRoles(角色)(duck键入)请尝试将List addToUserRoles(角色)(显式键入)作为签名,以避免与重载混淆。很难判断出哪里出了问题,但这可能会帮助你…对不起,伊恩,你能给我解释一下吗?我在这里做的是,我有两个域类user和role,它们之间有很多关系,对于很多关系,将生成一个中间类UserRole..在user类中,我有“static hasMany=[userRoles:UserRole,userLocations:UserLocation]”,在Role类中有“static hasMany=[userRoles:UserRole]”,在UserRole类中有static belongsTo=[user:user,Role:Role]。。在用户类中,我将角色类对象添加到用户类中,以便执行此操作,我有addToUserRoles(角色),因此在controller save()中也必须有相同的方法我可以理解这两个方法相互冲突,抱歉,我的英语不是很好