Thursday, May 9, 2013

Using getResource() and getResourceAsStream() in Grails

Want to include some static files into your Grails application? For example some document templates in order to be used by Freemarker, XDocReport etc?

You can:

  • create a package into conf/ eg named templates
  • put your files into conf/templates
  • reference the files from a service or controller using

this.class.getClassLoader().getResourceAsStream("templates/invoiceTemplate.docx");
or
this.class.getClassLoader().getResource("templates/invoiceTemplate.docx");

info: Don't forget to restart webapp (stop and run-app) to allow Grails to redeploy (including your resources)

This works for Grails 2.2.2