grails - 自动重新加载模板文件

标签 grails

我有一个非常标准的 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/

相关文章:

grails 2.4 java 8 和 tomcat 7.0.55.2

java - chalice 。 Hibernate 延迟加载多个对象

debugging - GGTS 在调试期间不会进入常规代码行

hibernate - hibernate grails顺序更改结果数

grails - 循环使用GSP grails

Grails 具有资源插件和 Assets 管道

tomcat - 在 Tomcat6 上运行的 Grails 应用程序抛出的 NotSerializableException

debugging - 如何调试Grails中发送的参数

javascript - 一次 POST Grails 中的所有传入参数

Grails 3.0.1 - 如何在 application.yml 中配置 grails.gorm.default.constraints