|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectjavax.servlet.GenericServlet
javax.servlet.http.HttpServlet
groovy.servlet.TemplateServlet
A generic servlet for templates.
It wraps a groovy.text.TemplateEngine
to process HTTP
requests. By default, it uses the
groovy.text.SimpleTemplateEngine
which interprets JSP-like (or
Canvas-like) templates.
Example HelloWorld.template
:
<html>
<body>
<% 3.times { %>
Hello World!
<% } %>
</body>
</html>
true
.
bindDefaultVariables = init("bindDefaultVariables", false);
bindRequestParameters = init("bindRequestParameters", false);
Field Summary | |
protected boolean |
bindDefaultVariables
|
protected boolean |
bindRequestParameters
|
static String |
DEFAULT_CONTENT_TYPE
|
protected TemplateEngine |
templateEngine
|
Constructor Summary | |
TemplateServlet()
|
Method Summary | |
protected Binding |
createBinding(javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response)
Creates the application context. |
protected TemplateEngine |
createTemplateEngine(javax.servlet.ServletConfig config)
Creates the template engine. |
void |
doGet(javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response)
Delegates to doRequest(HttpServletRequest, HttpServletResponse) . |
void |
doPost(javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response)
Delegates to doRequest(HttpServletRequest, HttpServletResponse) . |
protected void |
doRequest(javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response)
Processes all requests by dispatching to helper methods. |
protected void |
error(javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response,
Exception exception)
Simply sends an internal server error page (code 500). |
protected Template |
getTemplate(javax.servlet.http.HttpServletRequest request)
Gets the template by its name. |
protected Template |
getTemplate(URL templateURL)
Gets the template by its url. |
protected Template |
handleRequest(javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response,
Binding binding)
Default request handling. |
void |
init(javax.servlet.ServletConfig config)
Initializes the servlet. |
protected boolean |
init(javax.servlet.ServletConfig config,
String param,
boolean value)
Convient evaluation of boolean configuration parameters. |
protected void |
merge(Template template,
Binding binding,
javax.servlet.http.HttpServletResponse response)
Merges the template and writes response. |
protected void |
requestDone(javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response,
Binding binding)
Called one request is processed. |
protected URL |
resolveTemplateName(String templateName)
Locate template and convert its location to an URL. |
protected void |
setContentType(javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response)
Sets DEFAULT_CONTENT_TYPE . |
Methods inherited from class javax.servlet.http.HttpServlet |
doDelete, doHead, doOptions, doPut, doTrace, getLastModified, service, service |
Methods inherited from class javax.servlet.GenericServlet |
destroy, getInitParameter, getInitParameterNames, getServletConfig, getServletContext, getServletInfo, getServletName, init, log, log |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
public static final String DEFAULT_CONTENT_TYPE
protected TemplateEngine templateEngine
protected boolean bindDefaultVariables
protected boolean bindRequestParameters
Constructor Detail |
public TemplateServlet()
Method Detail |
public void init(javax.servlet.ServletConfig config)
config
- Passed by the servlet container.protected boolean init(javax.servlet.ServletConfig config, String param, boolean value)
config
- Servlet configuration passed by the servlet container.param
- Name of the paramter to look up.value
- Default value if parameter name is not set.
true
or false
.protected TemplateEngine createTemplateEngine(javax.servlet.ServletConfig config)
init(ServletConfig)
and returns just
SimpleTemplateEngine()
if the init parameter templateEngine
is not set.
config
- This serlvet configuration passed by the container.
#createTemplateEngine()
public void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException
doRequest(HttpServletRequest, HttpServletResponse)
.
javax.servlet.ServletException
IOException
public void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException
doRequest(HttpServletRequest, HttpServletResponse)
.
javax.servlet.ServletException
IOException
protected void doRequest(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException
request
- The http request.response
- The http response.
javax.servlet.ServletException
- ...
IOException
- ...protected Binding createBinding(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws Exception
bindDefaultParameters
is
true
:
binding.setVariable("request", request);
binding.setVariable("response", response);
binding.setVariable("context", servletContext);
binding.setVariable("session", request.getSession(true));
binding.setVariable("out", response.getWriter());
Binds all form parameters, too. This is, where we leave the clean MVC
pattern and Velocity behind. (...) Nobody told you to quit Velocity
anyway. :)
request
- The HTTP request.response
- The HTTP response.
Exception
- Any exception.protected void setContentType(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
DEFAULT_CONTENT_TYPE
.
request
- The HTTP request.response
- The HTTP response.protected Template handleRequest(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, Binding binding) throws Exception
request
- The HTTP request.response
- The HTTP response.binding
- The application context.
Exception
protected Template getTemplate(javax.servlet.http.HttpServletRequest request) throws Exception
Exception
- Any exception.protected URL resolveTemplateName(String templateName) throws Exception
templateName
- The name of the template.
Exception
- Any exception.protected Template getTemplate(URL templateURL) throws Exception
templateURL
- The url of the template.
Exception
- Any exception.protected void merge(Template template, Binding binding, javax.servlet.http.HttpServletResponse response) throws Exception
template
- The template that will be merged... now!binding
- The application context.response
- The HTTP response.
Exception
- Any exception.protected void error(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, Exception exception)
request
- The HTTP request.response
- The HTTP response.exception
- The cause.protected void requestDone(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, Binding binding)
request
- The HTTP request.response
- The HTTP response.binding
- The application context.
|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |