Baratine on GitHub

Views

Introduction

Baratine supports writing to the output directly, a basic view system, and popular template engines out there like Freemarker and Mustache.

Writing Directly to the Output

You can write String data or binary data to the output using the write() methods on io.baratine.web.RequestWeb, which is a special type of io.baratine.service.Result for web contexts:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
@Get("/hello")
public void hello(RequestWeb req)
{
  // writing Strings
  req.write("hello world");
  req.ok();
}

@Get("/hello")
public void hello(RequestWeb req)
{
  // writing binary
  byte[] bytes = { 0x61, 0x62, 0x63 };

  req.write(bytes, 0, bytes.length);
  req.ok();
}

Here are the write() methods on RequestWeb:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
OutWeb<T> push(Pipe<Buffer> out);

OutWeb<T> write(Buffer buffer);
OutWeb<T> write(byte []buffer, int offset, int length);

OutWeb<T> write(String value);
OutWeb<T> write(String value, String enc);
OutWeb<T> write(char []buffer, int offset, int length);
OutWeb<T> write(char []buffer, int offset, int length, String enc);

OutWeb<T> flush();

Writer writer();
Writer writer(String enc);

OutputStream output();

Writing Your First View

When you complete the request with req.ok(object), Baratine will invoke a view to serialize the object to the output. By default, Baratine will serialize Strings as UTF-8 text (Content-Type: text/plain):

1
2
3
4
5
6
@Get("/hello")
public void hello(RequestWeb req)
{
  // outputs: hello world
  req.ok("hello world");
}

and Java objects as JSON (Content-Type: application/json):

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
static class Hello {
  private String msg = "hello world";
}

@Get("/hello")
public void hello(RequestWeb req)
{
  // outputs: {"msg": "hello world"}
  req.ok(new Hello());
}

You can create your own view to format different return types. Here is one that overrides the default String view (note the type in ViewWeb<String>):

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
public class MyBoldView implements ViewWeb<String>
{
  public boolean render(RequestWeb req, String value)
  {
    req.header("Content-Type: text/html");
    req.write("<b>" + value + "</b>");
    req.ok();

    return true;  // or return false to proceed to the next view
  }
}

And register it globally with Baratine:

1
2
3
4
5
6
7
8
public static void main(String[] args)
{
  include(MyService.class);

  view(MyBoldView.class);

  start();
}

Here is a view that handles integers:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
public class MyIntView implements ViewWeb<Integer>
{
  public boolean render(RequestWeb req, Integer value)
  {
    req.write("int: " + value);
    req.ok();

    return true;
  }
}

Templates

Freemarker

To use Freemarker, add the following Maven dependency:

<dependency>
  <groupId>org.freemarker</groupId>
  <artifactId>freemarker</artifactId>
  <version>2.3.23</version>
</dependency>

In the template file (src/main/resources/hello.ftl):

hello ${name} from hello.ftl

Finally in the service (src/main/java/example/MyService.java):

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
@Service
public class MyService()
{
  @Get("/hello")
  public void hello(RequestWeb req)
  {
    ViewAndMapBuilder map = Views.view("hello.ftl");
    map.add("name", "John");

    req.ok(map);
  }

  public static void main(String[ ]args)
  {
    // scans the autoconf files and enables the tertiary views
    scanAutoConf();
    include(MyService.class);

    // default is classpath:/templates if not set
    Web.property("view.freemarker.templates", "classpath:/");

    start();
  }
}

Mustache

To use Mustache, add the following Maven dependency:

<dependency>
  <groupId>com.github.spullara</groupId>
  <artifactId>mustache</artifactId>
  <version>0.9.1</version>
</dependency>

Then in the service:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
@Service
public class MyService
{
  @Get("/hello")
  public void hello(RequestWeb req)
  {
    ViewAndMapBuilder map = Views.view("hello.mustache");
    map.add("name", "John");

    req.ok(map);
  }

  public static void main(String[ ]args)
  {
    scanAutoConf();
    include(MyService.class);

    Web.property("view.mustache.templates", "classpath:/");

    start();
  }
}

Velocity

To use Velocity, add the following Maven dependency:

<dependency>
  <groupId>org.apache.velocity</groupId>
  <artifactId>velocity</artifactId>
  <version>1.7</version>
</dependency>

Then in the service:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
@Service
public class MyService
{
  @Get("/hello")
  public void hello(RequestWeb req)
  {
    ViewAndMapBuilder map = Views.view("hello.vm");
    map.add("name", "John");

    req.ok(map);
  }

  public static void main(String[ ]args)
  {
    scanAutoConf();
    include(MyService.class);

    Web.property("view.velocity.templates", "classpath:/");

    start();
  }
}

Thymeleaf

To use Thymeleaf, add the following Maven dependency:

<dependency>
  <groupId>org.thymeleaf</groupId>
  <artifactId>thymeleaf</artifactId>
  <version>2.1.4.RELEASE</version>
</dependency>

Then in the service:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
public class MyService
{
  @Get("/hello")
  public void hello(RequestWeb req)
  {
    ViewAndMapBuilder map = Views.view("hello.html");
    map.add("name", "John");

    req.ok(map);
  }

  public static void main(String[ ]args)
  {
    scanAutoConf();
    include(MyService.class);

    Web.property("view.thymeleaf.templates", "classpath:/");

    start();
  }
}

Jade

To use Jade, add the following Maven dependency:

<dependency>
  <groupId>de.neuland-bfi</groupId>
  <artifactId>jade4j</artifactId>
  <version>1.1.4</version>
</dependency>

Then in the service:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
public class MyService
{
  @Get("/hello")
  public void hello(RequestWeb req)
  {
    ViewAndMapBuilder map = Views.view("hello.jade");
    map.add("name", "John");

    req.ok(map);
  }

  public static void main(String[ ]args)
  {
    scanAutoConf();
    include(MyService.class);

    Web.property("view.jade.templates", "classpath:/");

    start();
  }
}