自动重新加载模板文件 [英] Automatically Reload Template Files

查看:84
本文介绍了自动重新加载模板文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常标准的2.0.3 Grails应用程序,我执行了 grails install-templates ,它将文件list.gsp,edit.gsp等放入src / templates / scaffolding /目录。这些文件在进行更改时不会自动重新加载。有没有一种方法可以让这些自动重新加载,所以我不必每次停止/启动应用程序时都会进行更改?我试着看watchedResources,但似乎是与插件开发相关的。

你是对的, 机制只适用于插件。正确的解决方法是修改核心 ScaffoldingGrailsPlugin.groovy 以添加

  def watchedResources =file:./ src / templates / scaffolding / *

为此可能值得提交 JIRA 。与此同时,您可以通过编写一个简单的插件来将其注入脚手架插件,从而实现它的工作。执行 grails create-plugin watch-scaffolding ,然后使用下面的插件描述符:

  import org.codehaus.groovy.grails.plugins.GrailsPlugin 
$ b $ class WatchScaffoldingGrailsPlugin {
def version =0.1
def grailsVersion =2.0> *
def dependsOn = [:]
def pluginExcludes = [grails-app / views / error.gsp]

def title =观看脚手架插件
def author =Your name
def authorEmail =
def description ='''\
观察脚手架模板的变化并重新加载动态脚手架
控制器和观点。
'''
//插件文档的URL
def documentation =http://grails.org/plugin/watch-scaffolding

//观察对脚手架模板的更改...
def watchedResources =file:./ src / templates / scaffolding / *

// ...并在脚手架插件更改时
def onChange = {event - >
event.manager.getGrailsPlugin('scaffolding')。notifyOfEvent(
GrailsPlugin.EVENT_ON_CHANGE,null)
}

//其余的插件选项是无操作的
def onConfigChange = {event - > }
def doWithWebDescriptor = {xml - > }
def doWithSpring = {}
def doWithDynamicMethods = {ctx - > }
def doWithApplicationContext = {applicationContext - > }
def onShutdown = {event - > }
}

现在在您的应用程序的 BuildConfig.groovy add

  grails.plugin.location.'watch-scaffolding'='../watch-scaffolding' 

(或者是从应用根目录到插件根目录的适当相对路径)您的脚手架模板更改应自动重新加载。



(这是在Grails 2.1上测试的,我最初尝试使用影响,但它没有任何效果,但强制 onChange 事件在脚手架插件有要求的结果。)


I have a pretty standard 2.0.3 Grails app and I've executed grails install-templates which places files list.gsp, edit.gsp, etc. in src/templates/scaffolding/ directory. These files are not automatically reloaded when a change is made to them. Is there a way I can get these to be automatically reloaded so I don't have to stop/start the app every time I make a change? I've tried looking at watchedResources but that seems to be plugin development related.

解决方案

You are correct that the "watched-resources" mechanism only applies to plugins. The correct fix for this would be to modify the core ScaffoldingGrailsPlugin.groovy to add

def watchedResources = "file:./src/templates/scaffolding/*"

and it's probably worth submitting a JIRA to that effect. In the mean time, you may be able to get it working by writing a simple plugin of your own to "inject" this behaviour into the scaffolding plugin. Do grails create-plugin watch-scaffolding and then use the following for the plugin descriptor:

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 -> }
}

Now in your application's BuildConfig.groovy add

grails.plugin.location.'watch-scaffolding' = '../watch-scaffolding'

(or whatever is the appropriate relative path from the root of your app to the root of the plugin) and your scaffolding template changes should start to reload automatically.

(This is tested on Grails 2.1, I initially tried using influences but it didn't have any effect, however forcing an onChange event in the scaffolding plugin had the required result.)

这篇关于自动重新加载模板文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆