Is there an easy way for 开发者_高级运维a grails application to find the available list of views (*.gsp) available at runtime.
Ideally a solution will work for both application servers which unpack the WAR and those that do not unpack the WAR.
Thanks!
Unfortunately it's different for the dev environment and when deployed in a war:
import org.codehaus.groovy.grails.web.context.ServletContextHolder as SCH
def gsps = []
if (grailsApplication.isWarDeployed()) {
findWarGsps '/WEB-INF/grails-app/views', gsps
}
else {
findDevGsps 'grails-app/views', gsps
}
private void findDevGsps(current, gsps) {
for (file in new File(current).listFiles()) {
if (file.path.endsWith('.gsp')) {
gsps << file.path - 'grails-app/views/'
}
else {
findDevGsps file.path, gsps
}
}
}
private void findWarGsps(current, gsps) {
def servletContext = SCH.servletContext
for (path in servletContext.getResourcePaths(current)) {
if (path.endsWith('.gsp')) {
gsps << path - '/WEB-INF/grails-app/views/'
}
else {
findWarGsps path, gsps
}
}
}
精彩评论