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
ViewRenderercan be used by initializing yourViewBundlewith aIterableof theViewRendererinstances 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.ftlorsrc/main/resources/com/example/application/profile.mustacherespectively. 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.ftlorsrc/main/resources/hello.mustacherespectively.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?htmlescapes all HTML control characters in the result. Theftlvariablecomment 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)voidrun(T configuration, Environment environment)Initializes the 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:
getViewConfigurationin interfaceViewConfigurable<T>
-
run
public void run(T configuration, Environment environment) throws Exception
Description copied from interface:ConfiguredBundleInitializes the environment.- Specified by:
runin interfaceConfiguredBundle<T>- Parameters:
configuration- the configuration objectenvironment- the application'sEnvironment- Throws:
Exception- if something goes wrong
-
-