有没有一种方法可以配置 Controller 用来查找其GSP文件的起始文件夹?
我只有一个Grails项目(v2.0.1),并且希望能够将 Controller 和 View 组织到能够反射(reflect)我的功能的软件包中。
即我的 Controller :
uk.co.myCompany.function1.DashboardController.groovy
uk.co.myCompany.function2.DashboardController.groovy
我的views文件夹结构:
views -> function1 -> dashboard -> index.gsp
views -> function2 -> dashboard -> index.gsp
每当我在
render (view: 'index')
中调用DashboardController
时,它都会寻找views/dashboard/index.gsp
。我可以改为调用
render (view: '../function1/dashboard/index')
,但这似乎没有必要。另外,我显然在这些 Controller 中还有许多其他操作/页面,并且不想在每个操作中都输入它。在 Controller 类上指定一个默认文件夹的简单注释将是理想的。有任何想法吗?
最佳答案
如果要按功能组织 Controller ,并且它们可以使用相同的名称,建议您使用新的namespaces support of Grails。
您可以创建插件,而不是为每种功能使用一个软件包,而是通过插件使 Controller 名称唯一。
生成大量新版本的插件来测试您的应用程序可能会非常繁琐,但要避免这种情况,您可以使用grails.plugin.location将插件位置指定为依赖项。
通过这种结构,我认为您可以遵循Grails约定,但同时可以在函数中组织代码。
例
功能1Grails插件
功能2Grails插件
MainApp-更改
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'
}
}
关于grails - Grails:更改 Controller 用于gsp发现的默认文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16717296/