Using GroovyPagesTemplateEngine without a request?

烂漫一生 提交于 2019-12-08 04:57:29

问题


I'm trying to evaluate a GSP file without a real http request. I'm trying this:

String compileGsp(File input) {
        def text = ''
        try{
            text = groovyPagesTemplateEngine.createTemplate(input).make().toString()
        }
        catch( Exception e ){
            StackTraceUtils.sanitize(e).printStackTrace()
        }

        return text
    }

but this throws an exception and yields this:

java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.

How can I evaluate GSPs without a request? If I use SimpleTemplateEngine, I get some functionality, but I lose all of the taglibs, which include request-void tags like <g:each>


回答1:


I guess you can mock the web request. This posting is probably what you are looking for.



来源:https://stackoverflow.com/questions/7748704/using-groovypagestemplateengine-without-a-request

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!