public class ViewBundle extends Object implements Bundle
Bundle which enables the rendering of FreeMarker views by your service.
A view combines a Freemarker template with a set of Java objects:
public class PersonView extends View {
private final Person person;
public PersonView(Person person) {
super("profile.ftl");
this.person = person;
}
public Person getPerson() {
return person;
}
}
The "profile.ftl" is the path of the template relative to the class name. If this
class was com.example.service.PersonView, Freemarker would then look for the file
src/main/resources/com/example/service/profile.ftl. If the template path
starts with a slash (e.g., "/hello.ftl"), Freemarker will look for the file src/main/resources/hello.ftl.
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.service.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 typesafety in your templates.
| Constructor and Description |
|---|
ViewBundle() |
public void initialize(Bootstrap<?> bootstrap)
initialize in interface Bundlepublic void run(Environment environment)
Copyright © 2013. All Rights Reserved.