Grails, finding available views (.gsp) at runtime
1 answer
Unfortunately it differs for dev environment and when deployed in 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
}
}
}
+4
source to share