Add dynamic method from plugin using traits - not working

43 views
Skip to first unread message

Yingliang Du

unread,
Jul 22, 2015, 1:55:18 PM7/22/15
to Grails Dev Discuss
I just followed the user guide to add a dynamic method to controllers using trait from plugin. 

The following are my 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'] as String[]
    }
}

---Controller---
@Transactional(readOnly = true)
class MyDomainController {

    static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]

    def testTrait() {
        render "The current date is -> " + currentDate()
    }

    ...
}

I am expecting the controller will know the currentDate() method from the trait. But I got the following error in the browser:

Caused by MissingMethodException: No signature of method: com.ylw.gorm.MyDomainController.currentDate() is applicable for argument types: () values: []
Possible solutions: create()
->>   90 | methodMissing in grails.artefact.gsp.TagLibraryInvoker$Trait$Helper
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|     12 | $tt__testTrait in com.ylw.gorm.MyDomainController
|     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 I did wrong?

Thanks for help!
Yingliang
Reply all
Reply to author
Forward
0 new messages