Nuxeo ECM Projects 5.4.3-SNAPSHOT

org.nuxeo.ecm.webengine.samples
Class TemplatingObject

java.lang.Object
  extended by org.nuxeo.ecm.webengine.model.impl.AbstractResource<ResourceTypeImpl>
      extended by org.nuxeo.ecm.webengine.model.impl.DefaultObject
          extended by org.nuxeo.ecm.webengine.samples.HelloObject
              extended by org.nuxeo.ecm.webengine.samples.TemplatingObject
All Implemented Interfaces:
Resource, Adaptable

public class TemplatingObject
extends HelloObject

Templates sample. This demonstrates how to use template files to build client responses. JAX-RS provides a flexible mechanism to send responses based on the mime type that the client expects. To send a response to the client you simply return the Object you want as the response. JAX-RS engines will usually know how to render common Java objects like String, InputStream, File etc. If you need to output specific objects you need to register a custom MessageBodyWriter class. In JAX-RS you are not able to modify the HttpServletResponse object directly from a resource method. (add headers, cookies etc) Anything you may want to output must be returned from the resource method back to JAX-RS engine, and the engine will output it for you. This is a very good thing, even if for some people this approach may seem strange. You may ask yourself, ok cool, The response rendering is pretty well separated from the resource logic. But how can I modify response headers? In that case you must return a javax.ws.rs.Response that may be used to customize your response headers.

WebEngine is adding a new type of response objects: templates. Templates are freemarker based templates that can be used to render your objects depending on the request context. WebEngine is adding some cool extensions to freemarker templates that let you build your web site in a modular fashion. These extensions are called blocks. Blocks are dynamic template parts that can be extended or replaced using derived blocks. Using blocks, you can write a base template that may define the site layout (using blocks containing empty or generic content) and then write final skins for your layout by extending the base template and redefining blocks you are interested in. See the skin directory for template examples.

Templates are stored in files under the skin directory. Templates are always resolved relative to the skin directory, even if you are using absolute paths. The following variables are accessible from a template when rendered at rendering time:

To render a template as a response you need to instantiate it and then return it from the resource method. The template will be processed by the corresponding MessageBodyWriter and rendered on the client stream.

Author:
Bogdan Stefanescu

Constructor Summary
TemplatingObject()
           
 
Method Summary
 Response doRedirect(String path)
          Example of using redirect.
 Object getIndex1()
          Return the template index.ftl from 'skin' directory
 Object getIndex1(String name)
          Inject the variable 'name' in the template context and then return the template.
 Object getIndex2()
          Render the index2 template
 
Methods inherited from class org.nuxeo.ecm.webengine.samples.HelloObject
doGet, doGet
 
Methods inherited from class org.nuxeo.ecm.webengine.model.impl.DefaultObject
disptachAdapter, isAdapter
 
Methods inherited from class org.nuxeo.ecm.webengine.model.impl.AbstractResource
checkGuard, dispose, getActiveAdapter, getAdapter, getContext, getFacets, getLinks, getModule, getName, getNext, getNextSegment, getPath, getPrevious, getTemplate, getTrailingPath, getType, getURL, getView, hasFacet, initialize, isInstanceOf, isRoot, newAdapter, newObject, redirect, setNext, setPrevious, setRoot, toString
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

TemplatingObject

public TemplatingObject()
Method Detail

getIndex1

public Object getIndex1()
Return the template index.ftl from 'skin' directory


getIndex1

public Object getIndex1(String name)
Inject the variable 'name' in the template context and then return the template.


getIndex2

public Object getIndex2()
Render the index2 template


doRedirect

public Response doRedirect(String path)
Example of using redirect. The redirect method inherited from DefaultModule is returning a Response object that is doing a redirect


Nuxeo ECM Projects 5.4.3-SNAPSHOT

Copyright © 2011 Nuxeo SAS. All Rights Reserved.