gorm - Grails 3 - Add dynamic method from plugin using traits - not working -


i followed user guide add dynamic method controllers using trait plugin.

the following code:

---trait---

package com.ylw.gorm  trait datetrait { date currentdate() {     return new date() } } 

---traitinjector---

package com.ylw.gorm  import grails.compiler.traits.traitinjector import groovy.transform.compilestatic   @compilestatic class controllertraitinjector implements traitinjector {  @override class gettrait() {     datetrait }  @override string[] getartefacttypes() {     ['controller'] string[] } } 

---controller---

@transactional(readonly = true) class mydomaincontroller {  static allowedmethods = [save: "post", update: "put", delete: "delete"]  def testtrait() {     render "the current date -> " + currentdate() }  ... } 

i expecting controller know currentdate() method trait. got following error in browser:

caused missingmethodexception: no signature of method: com.ylw.gorm.mydomaincontroller.currentdate() applicable argument types: () values: [] possible solutions: create() 90 | methodmissing in grails.artefact.gsp.taglibraryinvoker$trait$helper - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 12 | $tt__testtrait in com.ylw.gorm.envers.auditeddomaincontroller 93 | dointransaction in grails.transaction.grailstransactiontemplate$2 90 | execute in grails.transaction.grailstransactiontemplate 1145 | runworker in java.util.concurrent.threadpoolexecutor 615 | run in java.util.concurrent.threadpoolexecutor$worker 745 | run . . . in java.lang.thread

what did wrong?

thanks help! yingliang

you haven't shown enough code sure going wrong.

i have created demo app @ https://github.com/jeffbrown/traitplugin.

see following:

https://github.com/jeffbrown/traitplugin/blob/b76af469a0695a449f28c43479ce60d9081c252e/datehelper/src/main/groovy/com/ylw/gorm/controllertraitinjector.groovy

https://github.com/jeffbrown/traitplugin/blob/b76af469a0695a449f28c43479ce60d9081c252e/datehelper/src/main/groovy/com/ylw/gorm/datetrait.groovy

those copied , pasted example code.

the app has controller @ following:

https://github.com/jeffbrown/traitplugin/blob/b76af469a0695a449f28c43479ce60d9081c252e/demo/grails-app/controllers/demo/mydomaincontroller.groovy

all of appears work.

ensure app has compile time dependency on plugin.


Comments

Popular posts from this blog

javascript - Karma not able to start PhantomJS on Windows - Error: spawn UNKNOWN -

c# - Display ASPX Popup control in RowDeleteing Event (ASPX Gridview) -

Nuget pack csproj using nuspec -