英文:
Grails3: The return type of java.lang.Boolean hasErrors() in AuthRoleController is incompatible with boolean in grails.artefact.Controller
问题
// AuthUserController.groovy
import grails.plugin.springsecurity.SpringSecurityService
import grails.transaction.Transactional
import org.niport.com.ComTrainingCenter
import org.niport.com.ComTrainingCenterService
import org.niport.com.FileService
import org.niport.com.TrackerService
import org.springframework.security.core.context.SecurityContextHolder
import javax.imageio.ImageIO
import java.awt.image.BufferedImage
import static org.springframework.http.HttpStatus.CREATED
import static org.springframework.http.HttpStatus.OK
@Transactional(readOnly = true)
class AuthUserController {
private static final allowedImageType = ['image/png', 'image/jpeg']
SpringSecurityService springSecurityService
AuthUserService authUserService
TrackerService trackerService
FileService fileService
def dateParseFormat, currentDate
AuthUser currentUser
@Transactional
save(AuthUser userInstance) {
if (userInstance == null) {
flash.error = message(code: "default.message.noRecordFound.label", default: "Error-Save-AuthUser-L21 : Record not found...!")
redirect(action: "create")
return
}
userInstance.avatarType = params.attachment ? fileService.uploadFile("auth", "auth_user", userInstance?.username, "any", 1, 5000000, params.attachment) : null
userInstance.createdBy = currentUser?.id
userInstance.properties["hdCode"] = trackerService.getHdCode(params.password)
userInstance.validate()
if (userInstance.hasErrors()) {
flash.error = "Error-Save-AuthUser-L27 : " + userInstance.errors
redirect(action: "create", userInstance: userInstance)
return
}
try {
userInstance.save(failOnError: true)
} catch (Exception e) {
println "ex-user-sa-L112 : " + e
flash.error = "Error-Save-AuthUser-L113 : " + e
redirect(action: "create", userInstance: userInstance)
return
}
request.withFormat {
form multipartForm {
flash.success = message(code: "default.message.created", args: [message(code: "user.pageTitle.label", default: "AuthUser"), userInstance.id])
redirect userInstance
}
'*' { respond userInstance, [status: CREATED] }
}
}
}
// AuthRoleController.groovy
import grails.transaction.Transactional
import grails.validation.Validateable
import static org.springframework.http.HttpStatus.CREATED
import static org.springframework.http.HttpStatus.OK
class AuthRoleController implements Validateable {
static allowedMethods = [save: "POST", update: "PUT"]
def springSecurityService, dateFormat, currentDate, currentUser
def beforeInterceptor = {
currentDate = new Date()
currentUser = springSecurityService.getCurrentUser()
dateFormat = grailsApplication.config.format.dtp.date
}
@Transactional
def save(AuthRole roleInstance) {
roleInstance.createdBy = (AuthUser) currentUser
roleInstance.validate()
if (roleInstance == null) {
flash.error = message(code: "default.message.noRecordFound.label", default: "Error-Save-AuthRole-L21 : Record not found...!")
redirect(action: "create")
return
}
log.info "${roleInstance}"
if (roleInstance.hasErrors()) {
flash.error = "Error-Save-AuthRole-L27 : " + roleInstance.errors
redirect(action: "create", roleInstance: roleInstance)
return
}
try {
roleInstance.save failOnError: true
} catch (Exception e) {
flash.error = "Error-Save-AuthRole-L36 : " + e
redirect(action: "create", roleInstance: roleInstance)
return
}
request.withFormat {
form multipartForm {
flash.success = message(code: "default.message.created", args: [message(code: "role.pageTitle.label", default: "AuthRole"), roleInstance.id])
redirect roleInstance
}
'*' { respond roleInstance, [status: CREATED] }
}
}
}
英文:
I have upgrade my app from grails 2.5 to grails 3.3.11.I have followed all the step and everything seem to be fine,but when I run the app. I am having this error :
startup failed:
/home/server-dev/Documents/dev_repo/tms/grails-app/controllers/org/niport/auth/AuthRoleController.groovy: -1: The return type of java.lang.Boolean hasErrors() in org.niport.auth.AuthRoleController is incompatible with boolean in grails.artefact.Controller
. At [-1:-1] @ line -1, column -1.
1 error
FAILURE: Build failed with an exception.
- What went wrong:
Execution failed for task ':compileGroovy'.
> Compilation failed; see the compiler error output for details.
Any idea or assistance will be helpfull, thanks a lot
------------------------AuthUserController.groovy ---------------------
import grails.plugin.springsecurity.SpringSecurityService
import grails.transaction.Transactional
import org.niport.com.ComTrainingCenter
import org.niport.com.ComTrainingCenterService
import org.niport.com.FileService
import org.niport.com.TrackerService
import org.springframework.security.core.context.SecurityContextHolder
import javax.imageio.ImageIO
import java.awt.image.BufferedImage
import static org.springframework.http.HttpStatus.CREATED
import static org.springframework.http.HttpStatus.OK
@Transactional(readOnly = true)
class AuthUserController {
private static final allowedImageType = ['image/png', 'image/jpeg']
SpringSecurityService springSecurityService
AuthUserService authUserService
TrackerService trackerService
FileService fileService
def dateParseFormat,
currentDate
AuthUser currentUser
@Transactional
save(AuthUser userInstance) {
if (userInstance == null) {
flash.error = message(code: "default.message.noRecordFound.label", default: "Error-Save-AuthUser-L21 : Record not found...!")
redirect(action: "create")
return
}
userInstance.avatarType = params.attachment ? fileService.uploadFile("auth", "auth_user", userInstance?.username, "any", 1, 5000000, params.attachment) : null
userInstance.createdBy = currentUser?.id
userInstance.properties["hdCode"] = trackerService.getHdCode(params.password)
userInstance.validate()
if (userInstance.hasErrors()) {
flash.error = "Error-Save-AuthUser-L27 : " + userInstance.errors
redirect(action: "create", userInstance: userInstance)
return
}
try {
userInstance.save(failOnError: true)
}
catch (Exception e) {
println "ex-user-sa-L112 : " + e
flash.error = "Error-Save-AuthUser-L113 : " + e
redirect(action: "create", userInstance: userInstance)
return
}
request.withFormat {
form multipartForm {
flash.success = message(code: "default.message.created", args: [message(code: "user.pageTitle.label", default: "AuthUser"), userInstance.id])
redirect userInstance
}
'*' { respond userInstance, [status: CREATED] }
}
}}
--------------------------AuthRoleController.groovy ----------------------
mport grails.transaction.Transactional
import grails.validation.Validateable
//import grails.artefact.Controller
//import java.lang.*
import static org.springframework.http.HttpStatus.CREATED
import static org.springframework.http.HttpStatus.OK
class AuthRoleController implements Validateable {
static allowedMethods = [save: "POST", update: "PUT"]
def springSecurityService, dateFormat, currentDate, currentUser
def beforeInterceptor = {
currentDate = new Date()
currentUser = springSecurityService.getCurrentUser()
dateFormat = grailsApplication.config.format.dtp.date
}
@Transactional
def save(AuthRole roleInstance) {
roleInstance.createdBy=(AuthUser)currentUser
roleInstance.validate()
if (roleInstance == null) {
flash.error = message(code: "default.message.noRecordFound.label", default: "Error-Save-AuthRole-L21 : Record not found...!")
redirect(action: "create")
return
}
log.info "${roleInstance}";
if (roleInstance.hasErrors()) {
flash.error = "Error-Save-AuthRole-L27 : " + roleInstance.errors
redirect(action: "create", roleInstance: roleInstance)
return
}
try {
roleInstance.save failOnError: true
}
catch (Exception e) {
flash.error = "Error-Save-AuthRole-L36 : " + e
redirect(action: "create", roleInstance: roleInstance)
return
}
request.withFormat {
form multipartForm {
flash.success = message(code: "default.message.created", args: [message(code: "role.pageTitle.label", default: "AuthRole"), roleInstance.id])
redirect roleInstance
}
'*' { respond roleInstance, [status: CREATED] }
}
}
}
}
答案1
得分: 1
> class AuthRoleController implements Validateable
没有充分的理由让控制器实现 `Validateable` 特性,这样做会导致问题。
问题在于你将会得到两个名为 `hasErrors` 的方法,一个来自 https://github.com/grails/grails-core/blob/1979b62d0ef9ebbaa547c8744263376557a10459/grails-plugin-validation/src/main/groovy/grails/validation/Validateable.groovy#L58,另一个来自 https://github.com/grails/grails-core/blob/1979b62d0ef9ebbaa547c8744263376557a10459/grails-plugin-controllers/src/main/groovy/grails/artefact/Controller.groovy#L123,而且它们具有不同的返回类型。
英文:
> class AuthRoleController implements Validateable
There is no good reason to have a controller implement the Validateable
trait, and doing so will cause problems.
The issue is you are going to get 2 methods named hasErrors
, one from https://github.com/grails/grails-core/blob/1979b62d0ef9ebbaa547c8744263376557a10459/grails-plugin-validation/src/main/groovy/grails/validation/Validateable.groovy#L58 and one from https://github.com/grails/grails-core/blob/1979b62d0ef9ebbaa547c8744263376557a10459/grails-plugin-controllers/src/main/groovy/grails/artefact/Controller.groovy#L123, and they have different return types.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论