Class ViewBundle<T>
- java.lang.Object
-
- io.dropwizard.views.common.ViewBundle<T>
-
- All Implemented Interfaces:
ConfiguredBundle<T>
,ViewConfigurable<T>
public class ViewBundle<T> extends Object implements ConfiguredBundle<T>, ViewConfigurable<T>
AConfiguredBundle
, which by default, enables the rendering of FreeMarker & Mustache views by your application.Other instances of
ViewRenderer
can be used by initializing yourViewBundle
with aIterable
of theViewRenderer
instances to be used when configuring yourConfiguredBundle
: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 wascom.example.application.PersonView
, Freemarker or Mustache would then look for the filesrc/main/resources/com/example/application/profile.ftl
orsrc/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 filesrc/main/resources/hello.ftl
orsrc/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,
See Also: FreeMarker Manual${person.name}
callsgetPerson().getName()
, and the?html
escapes all HTML control characters in the result. Theftlvariable
comment at the top indicate to Freemarker (and your IDE) that the root object is aPerson
, allowing for better type-safety in your templates.Mustache templates look something like this:
<html> <body> <h1>Hello, {{person.name}}!</h1> </body> </html>
In this template,
See Also: Mustache Manual{{person.name}}
callsgetPerson().getName()
.
-
-
Constructor Summary
Constructors Constructor Description ViewBundle()
ViewBundle(Iterable<ViewRenderer> viewRenderers)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Map<String,Map<String,String>>
getViewConfiguration(T configuration)
void
run(T configuration, Environment environment)
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface io.dropwizard.core.ConfiguredBundle
initialize
-
-
-
-
Constructor Detail
-
ViewBundle
public ViewBundle()
-
ViewBundle
public ViewBundle(Iterable<ViewRenderer> viewRenderers)
-
-
Method Detail
-
getViewConfiguration
public Map<String,Map<String,String>> getViewConfiguration(T configuration)
- Specified by:
getViewConfiguration
in interfaceViewConfigurable<T>
-
run
public void run(T configuration, Environment environment) throws Exception
- Specified by:
run
in interfaceConfiguredBundle<T>
- Throws:
Exception
-
-