A Groovy Template Engine to produce html stream from text files written in the Asciidoctor syntax.
Using Groovlets - a nice explanation HOW-TO
<!-- setup the GroovyServlet in your web.xml
and bind the script file extension -->
<servlet>
<servlet-name>groovlets</servlet-name>
<servlet-class>groovy.servlet.GroovyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>groovlets</servlet-name>
<url-pattern>*.groovy</url-pattern>
</servlet-mapping>
apply plugin: 'groovy' // java is ok too
apply plugin: 'war'
repositories {
mavenCentral()
mavenLocal()
}
dependencies {
providedCompile 'javax.servlet:servlet-api:2.5'
compile 'org.codehaus.groovy:groovy-all:2.4.5'
}
Output HTML on GET request is pretty easy using the built-in MarkupBuilder
// if present use name parameter from
// query string (eg. ?name=Francesco)
// else use "World"
def name = params.name ?: "World"
// output some html
html.html {
head {
title "Hello from Groovlet!"
}
body {
h1 "Hello, ${name}!"
}
}
In the example above, you see also Groovy native template engine: (G)String concatenation.
With built-in variables bound from the Servlet request via the GrailsServlet, you can also perform more complicated operations like handling some JSON in POST request.
response.contentType = 'application/json'
// Access headers, with context bound variable
if (headers['Content-Type'] != "application/json") {
throw new RuntimeException("Please use 'application/json' header")
}
// Get content from POST body
def jsonContent = request.reader.text
// Parse JSON to Map
def content = null
if (jsonContent) {
content = new JsonSlurper()
.parseText(jsonContent)
}
// build JSON output
json."echo" {
"original" content
}
It delegates work to a groovy.text.TemplateEngine implementation processing HTTP requests.
helloworld.html is a headless HTML-like template
<html>
<body>
<% 3.times { %>
Hello World!
<% } %>
<br>
</body>
</html>
<web-app>
<servlet>
<servlet-name>template</servlet-name>
<servlet-class>groovy.servlet.TemplateServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>template</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
</web-app>
By default, the TemplateServlet uses the SimpleTemplateEngine which interprets JSP-like templates. The init parameter template.engine defines the fully qualified class name of the template to use:
-
template.engine = [empty] - equals groovy.text.SimpleTemplateEngine
-
template.engine = groovy.text.SimpleTemplateEngine
-
template.engine = groovy.text.GStringTemplateEngine
-
template.engine = groovy.text.XmlTemplateEngine
-
template.engine = groovy.text.DoctorTemplateEngine
<servlet>
<servlet-name>DoctorTemplateServlet</servlet-name>
<servlet-class>groovyx.caelyf.CaelyfTemplateServlet</servlet-class>
<init-param>
<param-name>template.engine</param-name>
<param-value>groovy.text.DoctorTemplateEngine</param-value>
</init-param>
<init-param>
<!-- Remove the default "generated by" messages from the templates -->
<param-name>generated.by</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>verbose</param-name>
<!-- Output generation time in the HTML, see source page -->
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DoctorTemplateServlet</servlet-name>
<url-pattern>*.adoc</url-pattern>
</servlet-mapping>
Tip
|
Set <load-on-startup> parameter to zero to improve initial performance or if asciidoctor files are not used immediately |