我有一个grails应用程序,其中包含一系列嵌套目录中的各种javascript文件.我想通过资源插件管理它们,但不想显式注册每个插件.
Web目录结构
webapp
app
controller
controller1.js
controller2.js
...
model
model1.js
...
view
view1.js
Run Code Online (Sandbox Code Playgroud)
什么是伟大的只是在我的AppResources.groovy文件中声明:
resource url: 'app/**/*.js'
但这不起作用 - 抛出一个空指针.我试过了:
resource url: 'app/**' 但没有运气
我以为我会在配置文件中放入一些代码,这些代码将通过目录结构进行处理,但这似乎不起作用.这是我尝试过的:
def iterClos = {
it.eachDir( iterClos );
it.eachFile {
resource url: ${it.canonicalPath};
}
}
iterClos( new File("$grails.app.context/app") )
Run Code Online (Sandbox Code Playgroud)
不幸的是,这也失败了.
有没有人有任何想法我怎么能做到这一点?
Joh*_*don 19
问题解决了.
事实证明,运行代码以通过我的javascript目录回避的想法是有效的.我的代码不正确.这是动态加载我的javascript文件的代码:
--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
}
Run Code Online (Sandbox Code Playgroud)
以上将导致Resource插件跟踪我的javascript文件.这是当Resources处于调试模式时html的样子:
<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>
Run Code Online (Sandbox Code Playgroud)
...
作为Grails的新手,可以将可执行代码放在配置文件中,这是一个非常受欢迎的事实!