资源插件 - 如何将所有内容包含在目录中? [英] Resources Plugin -- How To include all contents in a directory?

查看:93
本文介绍了资源插件 - 如何将所有内容包含在目录中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个grails应用程序,它包含一系列嵌套目录中的单个javascript文件。我想通过资源插件管理它们,但不希望显式注册每个资源。



Web Dir结构

  webapp 
app
控制器
controller1.js
controller2.js
...
模型
model1.js
...
查看
view1.js

最好是在我的 AppResources.groovy 文件中声明:



资源网址:'app / ** / *。js'



不起作用 - 抛出一个空指针。我试过:

资源网址:'app / **'但没有运气



我以为我会在配置文件中放置一些代码,通过目录结构递归,但这似乎并不奏效。这是我尝试过的:

  def iterClos = {
it.eachDir(iterClos);
it.eachFile {
资源网址:$ {it.canonicalPath};




iterClos(new File($ grails.app.context / app))

不幸的是,失败了。



有没有人有任何想法可以达到这个目标吗?

解决方案

问题解决了。

出来,通过我的JavaScript目录运行代码来回避的想法的作品。我只是不正确的代码。这里是动态加载我的JavaScript文件的代码:

- AppResources.groovy

  import org.codehaus.groovy.grails.web.context.ServletContextHolder as SCH 

modules = {
core {
resource url:'/ resources / css /app.css',处置:'head'
资源url:'/resources/css/myapp.css',处置:'head'
资源url:'/ extjs / ext-all-debug .js',处置:'head'

getFilesForPath('/ app')。each {
resource url:it
}
}
}

def getFilesForPath(path){

def webFileCachePaths = []
$ b $ def servletContext = SCH.getServletContext()

//在集成模式下测试时,上下文不存在。 -jg
if(!servletContext)返回webFileCachePaths
$ b $ def realPath = servletContext.getRealPath('/')
$ b $ def appDir = new File($ realPath / $ path)

appDir.eachFileRecurse {档案档案 - >
if(file.isDirectory()|| file.isHidden())返回
webFileCachePaths<< file.path.replace(realPath,'')
}

webFileCachePaths
}

以上内容将导致Resource插件跟踪我的JavaScript文件。以下是资源处于调试模式时html的样子:

 < script src =/ myapp / extjs / ext- all-debug.js?_debugResources = y& n = 1336614540164type =text / javascript>< / script> 
< script src =/ myapp / app / controller / LogController.js?_debugResources = y& n = 1336614540164type =text / javascript>< / script>
< script src =/ myapp / app / controller / LoginController.js?_debugResources = y& n = 1336614540164type =text / javascript>< / script>
< script src =/ myapp / app / controller / ProfileController.js?_debugResources = y& n = 1336614540164type =text / javascript>< / script>

...



Grails,可以将可执行代码放在配置文件中是非常值得欢迎的事实!


I have a grails app that contains a whole mess of individual javascript files in a series of nested directories. I want to manage them via the resources plugin, but don't want to have to explicitly register each one.

Web Dir Structure

webapp
  app
    controller
      controller1.js
      controller2.js
      ...
    model
      model1.js
      ...
    view
      view1.js

what would be great is to just declare in my AppResources.groovy file:

resource url: 'app/**/*.js'

but that doesn't work -- throws a null pointer. I've tried:

resource url: 'app/**' but with no luck

I've thought that i would put some code in the config file that would recurse through the directory structure, but that doesn't seem to be working. Here's what I've tried:

def iterClos = {
        it.eachDir( iterClos );
        it.eachFile {
            resource url: ${it.canonicalPath};

        }

    }

    iterClos( new File("$grails.app.context/app") )

Unfortunately that failed as well.

Does anyone have any ideas how I could achieve this?

解决方案

problem solved.

As it turns out, the idea of running code to recuse through my javascript directories works. I just had the code incorrect. Here is the code that dynamically loads my javascript files:

--AppResources.groovy

import org.codehaus.groovy.grails.web.context.ServletContextHolder as SCH

modules = {
    core {
        resource url: '/resources/css/app.css', disposition: 'head'
        resource url: '/resources/css/myapp.css', disposition: 'head'
        resource url: '/extjs/ext-all-debug.js', dispostion: 'head'

        getFilesForPath('/app').each {
          resource url: it
        }
    }
}

def getFilesForPath(path) {

    def webFileCachePaths = []

    def servletContext = SCH.getServletContext()

    //context isn't present when testing in integration mode. -jg
    if(!servletContext) return webFileCachePaths

    def realPath = servletContext.getRealPath('/')

    def appDir = new File("$realPath/$path")

    appDir.eachFileRecurse {File file ->
        if (file.isDirectory() || file.isHidden()) return
        webFileCachePaths << file.path.replace(realPath, '')
    }

    webFileCachePaths
}

The above will cause the Resource plugin to track my javascript files. Here's what the html looks like when Resources is in debug mode:

<script src="/myapp/extjs/ext-all-debug.js?_debugResources=y&n=1336614540164" type="text/javascript" ></script>
<script src="/myapp/app/controller/LogController.js?_debugResources=y&n=1336614540164" type="text/javascript" ></script>
<script src="/myapp/app/controller/LoginController.js?_debugResources=y&n=1336614540164" type="text/javascript" ></script>
<script src="/myapp/app/controller/ProfileController.js?_debugResources=y&n=1336614540164" type="text/javascript" ></script>

...

Being new to Grails, it is a very welcome fact that one can put executable code within a config file!

这篇关于资源插件 - 如何将所有内容包含在目录中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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