Grails3: The return type of java.lang.Boolean hasErrors() in AuthRoleController is incompatible with boolean in grails.artefact.Controller

huangapple go评论98阅读模式
英文:

Grails3: The return type of java.lang.Boolean hasErrors() in AuthRoleController is incompatible with boolean in grails.artefact.Controller

问题

  1. // AuthUserController.groovy
  2. import grails.plugin.springsecurity.SpringSecurityService
  3. import grails.transaction.Transactional
  4. import org.niport.com.ComTrainingCenter
  5. import org.niport.com.ComTrainingCenterService
  6. import org.niport.com.FileService
  7. import org.niport.com.TrackerService
  8. import org.springframework.security.core.context.SecurityContextHolder
  9. import javax.imageio.ImageIO
  10. import java.awt.image.BufferedImage
  11. import static org.springframework.http.HttpStatus.CREATED
  12. import static org.springframework.http.HttpStatus.OK
  13. @Transactional(readOnly = true)
  14. class AuthUserController {
  15. private static final allowedImageType = ['image/png', 'image/jpeg']
  16. SpringSecurityService springSecurityService
  17. AuthUserService authUserService
  18. TrackerService trackerService
  19. FileService fileService
  20. def dateParseFormat, currentDate
  21. AuthUser currentUser
  22. @Transactional
  23. save(AuthUser userInstance) {
  24. if (userInstance == null) {
  25. flash.error = message(code: "default.message.noRecordFound.label", default: "Error-Save-AuthUser-L21 : Record not found...!")
  26. redirect(action: "create")
  27. return
  28. }
  29. userInstance.avatarType = params.attachment ? fileService.uploadFile("auth", "auth_user", userInstance?.username, "any", 1, 5000000, params.attachment) : null
  30. userInstance.createdBy = currentUser?.id
  31. userInstance.properties["hdCode"] = trackerService.getHdCode(params.password)
  32. userInstance.validate()
  33. if (userInstance.hasErrors()) {
  34. flash.error = "Error-Save-AuthUser-L27 : " + userInstance.errors
  35. redirect(action: "create", userInstance: userInstance)
  36. return
  37. }
  38. try {
  39. userInstance.save(failOnError: true)
  40. } catch (Exception e) {
  41. println "ex-user-sa-L112 : " + e
  42. flash.error = "Error-Save-AuthUser-L113 : " + e
  43. redirect(action: "create", userInstance: userInstance)
  44. return
  45. }
  46. request.withFormat {
  47. form multipartForm {
  48. flash.success = message(code: "default.message.created", args: [message(code: "user.pageTitle.label", default: "AuthUser"), userInstance.id])
  49. redirect userInstance
  50. }
  51. '*' { respond userInstance, [status: CREATED] }
  52. }
  53. }
  54. }
  1. // AuthRoleController.groovy
  2. import grails.transaction.Transactional
  3. import grails.validation.Validateable
  4. import static org.springframework.http.HttpStatus.CREATED
  5. import static org.springframework.http.HttpStatus.OK
  6. class AuthRoleController implements Validateable {
  7. static allowedMethods = [save: "POST", update: "PUT"]
  8. def springSecurityService, dateFormat, currentDate, currentUser
  9. def beforeInterceptor = {
  10. currentDate = new Date()
  11. currentUser = springSecurityService.getCurrentUser()
  12. dateFormat = grailsApplication.config.format.dtp.date
  13. }
  14. @Transactional
  15. def save(AuthRole roleInstance) {
  16. roleInstance.createdBy = (AuthUser) currentUser
  17. roleInstance.validate()
  18. if (roleInstance == null) {
  19. flash.error = message(code: "default.message.noRecordFound.label", default: "Error-Save-AuthRole-L21 : Record not found...!")
  20. redirect(action: "create")
  21. return
  22. }
  23. log.info "${roleInstance}"
  24. if (roleInstance.hasErrors()) {
  25. flash.error = "Error-Save-AuthRole-L27 : " + roleInstance.errors
  26. redirect(action: "create", roleInstance: roleInstance)
  27. return
  28. }
  29. try {
  30. roleInstance.save failOnError: true
  31. } catch (Exception e) {
  32. flash.error = "Error-Save-AuthRole-L36 : " + e
  33. redirect(action: "create", roleInstance: roleInstance)
  34. return
  35. }
  36. request.withFormat {
  37. form multipartForm {
  38. flash.success = message(code: "default.message.created", args: [message(code: "role.pageTitle.label", default: "AuthRole"), roleInstance.id])
  39. redirect roleInstance
  40. }
  41. '*' { respond roleInstance, [status: CREATED] }
  42. }
  43. }
  44. }
英文:

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 ---------------------

  1. import grails.plugin.springsecurity.SpringSecurityService
  2. import grails.transaction.Transactional
  3. import org.niport.com.ComTrainingCenter
  4. import org.niport.com.ComTrainingCenterService
  5. import org.niport.com.FileService
  6. import org.niport.com.TrackerService
  7. import org.springframework.security.core.context.SecurityContextHolder
  8. import javax.imageio.ImageIO
  9. import java.awt.image.BufferedImage
  10. import static org.springframework.http.HttpStatus.CREATED
  11. import static org.springframework.http.HttpStatus.OK
  12. @Transactional(readOnly = true)
  13. class AuthUserController {
  14. private static final allowedImageType = ['image/png', 'image/jpeg']
  15. SpringSecurityService springSecurityService
  16. AuthUserService authUserService
  17. TrackerService trackerService
  18. FileService fileService
  19. def dateParseFormat,
  20. currentDate
  21. AuthUser currentUser
  22. @Transactional
  23. save(AuthUser userInstance) {
  24. if (userInstance == null) {
  25. flash.error = message(code: "default.message.noRecordFound.label", default: "Error-Save-AuthUser-L21 : Record not found...!")
  26. redirect(action: "create")
  27. return
  28. }
  29. userInstance.avatarType = params.attachment ? fileService.uploadFile("auth", "auth_user", userInstance?.username, "any", 1, 5000000, params.attachment) : null
  30. userInstance.createdBy = currentUser?.id
  31. userInstance.properties["hdCode"] = trackerService.getHdCode(params.password)
  32. userInstance.validate()
  33. if (userInstance.hasErrors()) {
  34. flash.error = "Error-Save-AuthUser-L27 : " + userInstance.errors
  35. redirect(action: "create", userInstance: userInstance)
  36. return
  37. }
  38. try {
  39. userInstance.save(failOnError: true)
  40. }
  41. catch (Exception e) {
  42. println "ex-user-sa-L112 : " + e
  43. flash.error = "Error-Save-AuthUser-L113 : " + e
  44. redirect(action: "create", userInstance: userInstance)
  45. return
  46. }
  47. request.withFormat {
  48. form multipartForm {
  49. flash.success = message(code: "default.message.created", args: [message(code: "user.pageTitle.label", default: "AuthUser"), userInstance.id])
  50. redirect userInstance
  51. }
  52. '*' { respond userInstance, [status: CREATED] }
  53. }
  54. }}
  55. --------------------------AuthRoleController.groovy ----------------------
  56. mport grails.transaction.Transactional
  57. import grails.validation.Validateable
  58. //import grails.artefact.Controller
  59. //import java.lang.*
  60. import static org.springframework.http.HttpStatus.CREATED
  61. import static org.springframework.http.HttpStatus.OK
  62. class AuthRoleController implements Validateable {
  63. static allowedMethods = [save: "POST", update: "PUT"]
  64. def springSecurityService, dateFormat, currentDate, currentUser
  65. def beforeInterceptor = {
  66. currentDate = new Date()
  67. currentUser = springSecurityService.getCurrentUser()
  68. dateFormat = grailsApplication.config.format.dtp.date
  69. }
  70. @Transactional
  71. def save(AuthRole roleInstance) {
  72. roleInstance.createdBy=(AuthUser)currentUser
  73. roleInstance.validate()
  74. if (roleInstance == null) {
  75. flash.error = message(code: "default.message.noRecordFound.label", default: "Error-Save-AuthRole-L21 : Record not found...!")
  76. redirect(action: "create")
  77. return
  78. }
  79. log.info "${roleInstance}";
  80. if (roleInstance.hasErrors()) {
  81. flash.error = "Error-Save-AuthRole-L27 : " + roleInstance.errors
  82. redirect(action: "create", roleInstance: roleInstance)
  83. return
  84. }
  85. try {
  86. roleInstance.save failOnError: true
  87. }
  88. catch (Exception e) {
  89. flash.error = "Error-Save-AuthRole-L36 : " + e
  90. redirect(action: "create", roleInstance: roleInstance)
  91. return
  92. }
  93. request.withFormat {
  94. form multipartForm {
  95. flash.success = message(code: "default.message.created", args: [message(code: "role.pageTitle.label", default: "AuthRole"), roleInstance.id])
  96. redirect roleInstance
  97. }
  98. '*' { respond roleInstance, [status: CREATED] }
  99. }
  100. }
  101. }
  102. }

答案1

得分: 1

  1. > class AuthRoleController implements Validateable
  2. 没有充分的理由让控制器实现 `Validateable` 特性,这样做会导致问题。
  3. 问题在于你将会得到两个名为 `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.

huangapple
  • 本文由 发表于 2020年9月25日 20:18:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/64064003.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定