Grails, finding available views (.gsp's) at runtime

Tag: grails Author: mituzhifanyaya Date: 2009-09-30

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!

Best Answer

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
      }
   }
}