grails应用程序是否有一种简单的方法可以找到运行时可用的可用视图列表(* .gsp)。
理想情况下,解决方案适用于解压WAR的应用程序服务器和不解压WAR的应用程序服务器。
谢谢!
答案 0 :(得分:4)
不幸的是,对于开发环境以及在战争中部署时它是不同的:
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
}
}
}