Class ViewBundle<T>

  • All Implemented Interfaces:
    ConfiguredBundle<T>, ViewConfigurable<T>

    public class ViewBundle<T>
    extends Object
    implements ConfiguredBundle<T>, ViewConfigurable<T>
    A ConfiguredBundle, which by default, enables the rendering of FreeMarker & Mustache views by your application.

    Other instances of ViewRenderer can be used by initializing your ViewBundle with a Iterable of the ViewRenderer instances to be used when configuring your ConfiguredBundle:

    
     new ViewBundle(ImmutableList.of(myViewRenderer))
     

    A view combines a Freemarker or Mustache template with a set of Java objects:

    
     public class PersonView extends View {
         private final Person person;
    
         public PersonView(Person person) {
             super("profile.ftl"); // or super("profile.mustache"); for a Mustache template
             this.person = person;
         }
    
         public Person getPerson() {
             return person;
         }
     }
     

    The "profile.ftl[hx]" or "profile.mustache" is the path of the template relative to the class name. If this class was com.example.application.PersonView, Freemarker or Mustache would then look for the file src/main/resources/com/example/application/profile.ftl or src/main/resources/com/example/application/profile.mustache respectively. If the template path starts with a slash (e.g., "/hello.ftl" or "/hello.mustache"), Freemarker or Mustache will look for the file src/main/resources/hello.ftl or src/main/resources/hello.mustache respectively.

    A resource method with a view would looks something like this:

     @GET
     public PersonView getPerson(@PathParam("id") String id) {
         return new PersonView(dao.find(id));
     }
     

    Freemarker templates look something like this:

    
     <#-- @ftlvariable name="" type="com.example.application.PersonView" -->
     <html>
         <body>
             <h1>Hello, ${person.name?html}!</h1>
         </body>
     </html>
     

    In this template, ${person.name} calls getPerson().getName(), and the ?html escapes all HTML control characters in the result. The ftlvariable comment at the top indicate to Freemarker (and your IDE) that the root object is a Person, allowing for better type-safety in your templates.

    See Also: FreeMarker Manual

    Mustache templates look something like this:

    
     <html>
         <body>
             <h1>Hello, {{person.name}}!</h1>
         </body>
     </html>
     

    In this template, {{person.name}} calls getPerson().getName().

    See Also: Mustache Manual