shuttsy
shuttsy

Reputation: 1645

Grails: change the default folder a controller uses for gsp discovery

Is there a way to configure the starting folder that a controller uses for finding it's GSP files?

I have a single Grails project (v2.0.1) and I want to be able to organise my controller and views into packages that reflect my functions.

i.e. my controllers:

uk.co.myCompany.function1.DashboardController.groovy
uk.co.myCompany.function2.DashboardController.groovy

my views folder structure:

views -> function1 -> dashboard -> index.gsp
views -> function2 -> dashboard -> index.gsp

Whenever I call render (view: 'index') in DashboardController, it looks for views/dashboard/index.gsp.

I can change to call render (view: '../function1/dashboard/index') instead but this seems a bit unnecessary. Plus I obviously I have numerous other actions/pages in these controllers and don't want to enter this in every action.

A simple annotation on the controller class to specify the default folder would be ideal. Any ideas?

Upvotes: 1

Views: 1036

Answers (1)

user800014
user800014

Reputation:

If you want to organize your controllers by functionality, and they can have the same name, I suggest you to use the new namespaces support of Grails.

Instead of having one package for each functionality, you can create plugins, making your controller name unique by plugin.

Generating new versions of your plugins to test your app can be massive, but to avoid that you can specify your plugin location as dependency, using grails.plugin.location.

With that structure I think you can follow Grails conventions, but at the same time organize your code in functions.


Example

Function1GrailsPlugin

  • grails-app/controllers/DashboardController
  • grails-app/views/dashboard/*

Function2GrailsPlugin

  • grails-app/controllers/DashboardController
  • grails-app/views/dashboard/*

MainApp - change UrlMappings

static mappings = {
    //requests to /function1 will be handled by the
    //DashboardController provided by the Function1 plugin
    "/function1" {
        controller = 'dashboard'
        plugin = 'function1'
    }

    //requests to /function2 will be handled by the
    //DashboardController provided by the Function2 plugin
    "/function2" {
        controller = 'dashboard'
        plugin = 'function2'
    }
}

Upvotes: 1

Related Questions