Hello Nitya,
I don't know if this is the "right design approach" for Freemarker,
but we're using it without problems to build HTML and WebDAV XML
representations of some of our resources:
@Get("html")
public Representation toHTML() throws ResourceException {
if (!SUSecurityManager.getInstance().isAdminPassSet()) {
String serverURL =
getRequest().getResourceRef().getHostIdentifier();
String via = getHttpHeader("Via"); // NOI18N
if (via != null) {
via = via.split("1.[\\d] ", 1)[0]; // NOI18N
boolean isHTTPS = serverURL.startsWith("https://"); // NOI18N
serverURL = String.format("http%s://%s", isHTTPS ? "s"
: "", via.substring(4)); // NOI18N
}
String redirectURL =
String.format("%s/res/html/adminpass.html", serverURL); // NOI18N
redirectTemporary(redirectURL);
return null;
}
try {
// Create FreeMarker data object
Map<String, Object> pageData = new HashMap<String, Object>();
Map<String, Object> userData = new HashMap<String, Object>();
userData.put("name", user.getName());
userData.put("email", user.getEmail());
userData.put("isSA", user.isSysAdmin);
userData.put("lang", user.getPreferredLang());
pageData.put("user", userData);
pageData.put("workspaces", workspacesAsJson());
pageData.put("version", Constants.CCO_VERSION);
// Get FreeMarker page template
Template thtml =
FreeMarkerUtil.getInstance().getTemplate("workspaces-html.ftl");
// Return the HTML representation
TemplateRepresentation rep = new
TemplateRepresentation(thtml, pageData, MediaType.TEXT_HTML);
rep.setCharacterSet(CharacterSet.UTF_8);
return rep;
} catch (Exception e) {
String msg = "Cannot get HTML representation of workspace list";
throw new ResourceException(Status.SERVER_ERROR_INTERNAL, msg, e);
} finally {
long lstop = System.currentTimeMillis();
getLogger().info(String.format("GET [HTML] request
processed in %2.3f sec.", (lstop - rstart) / 1000.0));
}
}
The above is real, working, code. I'm not including the ftl file
because it's too big, you can take a look at it at:
http://trac.calenco.com/browser/trunk/src/com/calenco/staticres/workspaces-html.ftl
The above piece of code is from:
http://trac.calenco.com/browser/trunk/src/com/calenco/resource/system/WorkspacesResource.java
Hope this helps.
On Tue, Jun 8, 2010 at 12:57 PM, Nitya <[email protected]> wrote:
> Hi,
>
> Does anyone have (and can share) a simple sample of how Freemarker templates
> are
> configured and used in the Restlet 2 platform? Most web pages on this topic
> refer to the Javadoc or have samples for 1.1 which may not fit the bill
>
> Any pointers would be deeply appreciated, particularly if they showcase the
> right design approach for supporting Freemarker-based views within standard
> Restlet apps
>
> Best
> Nitya
>
> ------------------------------------------------------
> http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=2618514
>
--
Fabián Mandelbaum
IS Engineer
------------------------------------------------------
http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=2618830