我有一个非常标准的 2.0.3 Grails 应用程序,我已经执行了 grails install-templates
它将文件 list.gsp、edit.gsp 等放在 src/templates/scaffolding/目录中。对这些文件进行更改时,它们不会自动重新加载。有没有办法让这些自动重新加载,这样我就不必每次进行更改时都停止/启动应用程序?我试过查看watchedResources,但这似乎与插件开发有关。
最佳答案
您是正确的,“监视资源”机制仅适用于插件。正确的解决方法是修改核心 ScaffoldingGrailsPlugin.groovy
加上
def watchedResources = "file:./src/templates/scaffolding/*"
并且可能值得提交 JIRA达到那个效果。同时,您可以通过编写自己的简单插件将这种行为“注入(inject)”到脚手架插件中来使其工作。做
grails create-plugin watch-scaffolding
然后将以下内容用于插件描述符:import org.codehaus.groovy.grails.plugins.GrailsPlugin
class WatchScaffoldingGrailsPlugin {
def version = "0.1"
def grailsVersion = "2.0 > *"
def dependsOn = [:]
def pluginExcludes = [ "grails-app/views/error.gsp" ]
def title = "Watch Scaffolding Plugin"
def author = "Your name"
def authorEmail = ""
def description = '''\
Watches for changes to scaffolding templates and reloads dynamically-scaffolded
controllers and views.
'''
// URL to the plugin's documentation
def documentation = "http://grails.org/plugin/watch-scaffolding"
// watch for changes to scaffolding templates...
def watchedResources = "file:./src/templates/scaffolding/*"
// ... and kick the scaffolding plugin when they change
def onChange = { event ->
event.manager.getGrailsPlugin('scaffolding').notifyOfEvent(
GrailsPlugin.EVENT_ON_CHANGE, null)
}
// rest of plugin options are no-op
def onConfigChange = { event -> }
def doWithWebDescriptor = { xml -> }
def doWithSpring = { }
def doWithDynamicMethods = { ctx -> }
def doWithApplicationContext = { applicationContext -> }
def onShutdown = { event -> }
}
现在在您的应用程序的
BuildConfig.groovy
添加grails.plugin.location.'watch-scaffolding' = '../watch-scaffolding'
(或者从你的应用程序的根目录到插件的根目录的任何适当的相对路径)并且你的脚手架模板更改应该开始自动重新加载。
(这是在 Grails 2.1 上测试的,我最初尝试使用 influences 但它没有任何效果,但是在脚手架插件中强制
onChange
事件具有所需的结果。)
关于grails - 自动重新加载模板文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11873534/