Override this method to set your variables to the Groovy binding.
All variables bound the binding are passed to the template source text,
e.g. the HTML file, when the template is merged.
The binding provided by TemplateServlet does already include some default
variables. As of this writing, they are (copied from
groovy.servlet.ServletBinding):
- "request" : HttpServletRequest
- "response" : HttpServletResponse
- "context" : ServletContext
- "application" : ServletContext
- "session" : request.getSession(false)
And via implicite hard-coded keywords:
- "out" : response.getWriter()
- "sout" : response.getOutputStream()
- "html" : new MarkupBuilder(response.getWriter())
Example binding all servlet context variables:
class Mytlet extends TemplateServlet {
protected void setVariables(ServletBinding binding) {
// Bind a simple variable
binding.setVariable("answer", new Long(42));
// Bind all servlet context attributes...
ServletContext context = (ServletContext) binding.getVariable("context");
Enumeration enumeration = context.getAttributeNames();
while (enumeration.hasMoreElements()) {
String name = (String) enumeration.nextElement();
binding.setVariable(name, context.getAttribute(name));
}
}
}