如何在运行时检查资源文件是否存在[grails]? [英] How to check for resource file existence at run-time [grails]?
本文介绍了如何在运行时检查资源文件是否存在[grails]?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要测试一个资源文件(例如图像)是否存在,如果不存在,我将显示另一个图像。
我的GSP视图代码如下所示:
<%if(resExists(dir:images,file: img.jpg)){%>
< img src =$ {g.resource(dir:'images',file:'img.jpg')}>
<%else {%>
< img src =$ {g.resource(dir:'images',file:'noimg.jpg')}>
<%}%>
如何测试Grails中的文件存在性?什么是方法的代码布尔resExists()
解决方案
I找到答案:
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ def $ res $
resFile?.exists()
}
或
def resExists(resPath){
def resFile = request.servletContext.getResource(resPath)
resPath
}
你可以用 resExists('images / img.jpg')来调用它
I need to test whether a resource file (an image, for instance) exists and if not, I will display another image. My GSP view code looks like:
<% if (resExists(dir: "images", file:"img.jpg")) {%>
<img src="${g.resource(dir:'images',file: 'img.jpg')}">
<% else { %>
<img src="${g.resource(dir:'images',file: 'noimg.jpg')}">
<%}%>
How can I test for a file existence in Grails? What is the code of the method boolean resExists()
解决方案
I found out the answer:
def resExists(resPath) {
def resFile = grailsApplication.parentContext.getResource(resPath)
resFile?.exists()
}
or
def resExists(resPath) {
def resFile = request.servletContext.getResource(resPath)
resPath
}
And you call it with resExists('images/img.jpg')
这篇关于如何在运行时检查资源文件是否存在[grails]?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文