How to check for resource file existence at run-time [grails]?

自古美人都是妖i 提交于 2019-12-09 04:36:30

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')

I've done the following in GSP:

<g:set var="flag" value="${new File(grailsApplication.mainContext.servletContext.getRealPath("images/sgs-geosol/sign/${sec.username()}.gif")).exists()}"></g:set>

<g:if test="${flag}">

</g:if>

The code below returns the real path of the Grails app:

grailsApplication.mainContext.servletContext.getRealPath("")

The above answer did not work for me when used from within a plugin, packaged as a production war. I now use the following:

GrailsConventionGroovyPageLocator groovyPageLocator

def resExists(resPath) {
    def resource = groovyPageLocator.findTemplateByPath(resPath)
    resource
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!