我有一个非常标准的2.0.3 Grails应用程序,我已执行grails install-templates
,它将文件list.gsp,edit.gsp等放在src / templates / scaffolding /目录中。对它们进行更改时,不会自动重新加载这些文件。有没有办法让我自动重新加载这些,所以每次做出改变时我都不必停止/启动应用程序?我试过看过watchedResources,但这似乎与插件开发有关。
答案 0 :(得分:11)
“观察资源”机制仅适用于插件,这是正确的。对此的正确修复是修改核心ScaffoldingGrailsPlugin.groovy
以添加
def watchedResources = "file:./src/templates/scaffolding/*"
并且可能值得为此提交JIRA。与此同时,您可以通过编写自己的简单插件将此行为“注入”脚手架插件来使其工作。执行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
事件具有所需的结果。)
答案 1 :(得分:3)
此代码刷新脚手架缓存。您可以为其创建特定的管理操作:
org.codehaus.groovy.grails.scaffolding.view.
ScaffoldingViewResolver.scaffoldedViews.clear()
答案 2 :(得分:2)
根据GRAILS-755,这已得到修复,但我认为没有,因为它们也没有为我重新加载。
从那个Jira,这是一个可能的解决方法:
使用控制台插件,然后运行此命令清除动态 scaffolded view cache:
def scaffoldedView = org.codehaus.groovy.grails.scaffolding.view.ScaffoldingViewResolver.scaffoldedViews.clear()
之后,下次我请求一个页面时,它找不到它 缓存,然后返回磁盘重新创建它。