public class ViewBundle<T> extends Object implements ConfiguredBundle<T>, ViewConfigurable<T>
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.
Mustache templates look something like this:
<html>
<body>
<h1>Hello, {{person.name}}!</h1>
</body>
</html>
In this template, {{person.name}}
calls getPerson().getName()
.
Constructor and Description |
---|
ViewBundle() |
ViewBundle(Iterable<ViewRenderer> viewRenderers) |
Modifier and Type | Method and Description |
---|---|
Map<String,Map<String,String>> |
getViewConfiguration(T configuration) |
void |
run(T configuration,
Environment environment)
Initializes the environment.
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
initialize
public ViewBundle()
public ViewBundle(Iterable<ViewRenderer> viewRenderers)
public Map<String,Map<String,String>> getViewConfiguration(T configuration)
getViewConfiguration
in interface ViewConfigurable<T>
public void run(T configuration, Environment environment) throws Exception
ConfiguredBundle
run
in interface ConfiguredBundle<T>
configuration
- the configuration objectenvironment
- the application's Environment
Exception
- if something goes wrongCopyright © 2020. All rights reserved.