I have the following code that grabs all controllers, sorts it, and outputs in li tags:
<g:each var="c" in="${grailsApplication.controllerClasses.sort { it.fullName } }">
<li<%= c.logicalPropertyName == controllerName ? ' class="active"' : '' %>>
<g:link controller="${c.logicalPropertyName}">${c.naturalName}</g:link>
</li>
</g:each>
I have a need to filter out controllers by package i.e. grab controller from a certain package.
For example:
com.app.module.mars.controller.HelloController
com.app.module.venus.controller.PrintController
As you can see I'm packaging controllers by modules, so mars will have its own set of controllers and venus will have its own. Then in the UI I want to use the above code (with some filter) which will show modules as main-menus and their controllers as dropdowns.
How can I apply such a filter? Or if you could guide me in the right direction would be great. Thanks.
You can use
GrailsClassUtils.isClassBelowPackage()
which takes a class and a list of packages as the arguments. So this should do the trick:Edit:
grailsApplication.controllerClasses
probably gives you a list ofGrailsClass
objects, so you'd want to usec.clazz
instead ofc.class
likeYou can use
Collection#groupBy
to group the controller classes by package name.I don't have a Grails system to make a quick test right now, but this would be a little example of grouping classes by package name:
You can then iterate through each packageName to make each menu and through each class under that package name to make each menu item. Something like...
... but with GSP-style loops :)