Wednesday, January 8, 2014

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

This is not a important post. I just need to do some googling to achieve this and i found at: http://stackoverflow.com/questions/1577872/grails-finding-available-views-gsps-at-runtime. Thanks Burt Beckwith. I put this here to not search in google again.


import org.codehaus.groovy.grails.web.context.ServletContextHolder as SCH

def viewGsp() {
    List gspFileList = []
    if (grailsApplication.isWarDeployed()) {
        findWarGspList ('/WEB-INF/grails-app/views', gspFileList);
    } else {
        findDevGspList ('grails-app/views', gspFileList);
    }
    gspFileList.each {
        render it.toString() + "<br/>";
    }
    render "";
}
private void findDevGspList(String location, List gspFileList) {
    for (file in new File(location).listFiles()) {
        if (file.path.endsWith('.gsp')) {
            gspFileList << file.path - 'grails-app/views/';
        } else {
            findDevGspList (file.path, gspFileList);
        }
    }
}

private void findWarGspList(String location, List gspFileList) {
    def servletContext = SCH.servletContext
    for (path in servletContext.getResourcePaths(location)) {
        if (path.endsWith('.gsp')) {
            gspFileList << path - '/WEB-INF/grails-app/views/';
        } else {
            findWarGspList (path, gspFileList);
        }
    }
}

No comments:

Post a Comment