Is it possible to create a controller that extends the abstract controller in the grail?

I am wondering if something like this is possible:

abstract class AbstractController { def list = { //default list action } } class MyController extends AbstractController { def show = { //show action } } 

Where AbstractController not displayed on the Internet, i.e. / app / abstract / list is not available and where MyController has list and show actions and is available on the Internet as / app / my / ....

Has anyone done something like this?

+4
source share
2 answers

Try placing AbstractController in the src/groovy folder.

Although working together on controllers may not be the best idea - itโ€™s better to move it to POGO classes or services. This question partially addresses this question: How do you use common methods in different grails controllers?

+8
source

For the latest version of grails (3.x as time of writing) it would be better to use traits instead of extending the abstract controller or using Mixin, it was later deprecated since the introduction of traits in groovy v2.3, here is an example of using a trait to add general behavior to your to the controller: 1- create your own traits in src / groovy, for example.

 import grails.web.Action trait GenericController { @Action def test(){ render "${params}" } } 

2- implement your attribute when implementing any interface:

 class PersonController implements GenericController { /*def test(){ render 'override the default action...' }*/ } 

Note. Traits can dynamically access all objects of the controller: params, response ... etc., and you can still override the action of signs.

I hope for this help.

0
source

All Articles