Wednesday, July 16, 2008

Writing Groovy Server Pages output to a file (or to any Writer)

One thing I've never managed to do is to write a JSP page to a file or to any stream other than the http response output. That's something that can be useful for example, to generate HTML files in batch, using JSP files as templates. Due to that, I've turned to use Velocity instead of JSP in my past days.


But now, with Grails I use Groovy Server Pages (or GSP, in short). I like GSP a lot, and recently I needed to write the output of one of my GSP pages to a file. The Groovy Pages Template Engine is a Groovy Template Engine that automatically binds the http request attributes when the template result is generated. So, in order to use my GSP as a template outside Grails, I just need to use a Mock HttpRequest to pass the referenced variables. After a couple of tries, here's the code that got things done. All the needed libraries are in the dist and lib folders of the Grails installation folder:


test.gsp


<html>
<body>
<ul>
<g:each in="${theList}">
<li>${it}</li>
</g:each>
</ul>
</body>
</html>

GspToFileTest.groovy


class GspToFileTest{

static void main(args) {
MockHttpServletRequest servletRequest = new MockHttpServletRequest()
List theList = ['one','two','three']
servletRequest.setAttribute('theList', theList)
GrailsWebRequest grailsWebRequest = new GrailsWebRequest(
servletRequest,new MockHttpServletResponse(),
new MockServletContext())
grailsWebRequest.setAttribute(
GrailsApplicationAttributes.WEB_REQUEST
, grailsWebRequest, 0)
RequestContextHolder.requestAttributes = grailsWebRequest
GroovyPagesTemplateEngine engine = new GroovyPagesTemplateEngine()
Resource page = new FileSystemResource('test.gsp')
Writer writer = new PrintWriter('test.html')
engine.createTemplate(page).make().writeTo(writer);
writer.close()
}

}

And, the result HTML is...


test.html


<html>
<body>
<ul>

<li>one</li>

<li>two</li>

<li>three</li>

</ul>
</body>
</html>