Jtwig Web

Jtwig Web project extends Jtwig Core with and eases the integration with Java Servlet API (version 3.1). It also adds the function path, a new resource WebResource and resource resolver WebResourceResolver.

The path function

The path function can be used to output the Servlet Context Path, for more information about this value, check the Servlet API documentation. It is expecting either none of one argument. If no argument is provided, as mentioned, it will only print the servlet context path, on the other hand, if the an argument is provided (expected to be a String), then it appends such value to the servlet context path.

{{ path('/index') }}

Assuming the servlet context path is /application, then the previous example will output /application/index.

The WebResource and WebResourceResolver

This combo of WebResource and WebResourceResolver allows Jtwig to support web (WEB-INF/ rooted) resources. Users can then reference such resources from their app. Note that, if a given path is prefixed with web:, it will be interpreted by this resource resolver, such can be used to disambiguate the source of those resources.

Combining with the Servlet API

To integrate Jtwig Web with the Servlet API the JtwigRenderer concept was introduced.

public static class HelloWorldServlet extends HttpServlet {
    private final JtwigRenderer renderer = JtwigRenderer.defaultRenderer();

    @Override
    protected void service(
        HttpServletRequest request, 
        HttpServletResponse response
        ) throws ServletException, IOException {
        request.setAttribute("variable", "Hello");

        renderer.dispatcherFor("/WEB-INF/templates/index.twig.html")
                .with("name", "Jtwig")
                .render(request, response);
    }
}

The example above can be seen in jtwig-examples. It will print Hello World!, as the /WEB-INF/templates/index.twig.html template is defined as {{ variable }} {{ name }}!. As one can see in the previous example, JtwigRenderer exposes all request variable as part of the JtwigModel passed to the render stage.

Note that, this JtwigRenderer also exposes an API call to specify the Jtwig template inline, as shown in the example below. Such example will produce the same output as the example above.

public class HelloWorldServlet extends HttpServlet {
    private final JtwigRenderer renderer = JtwigRenderer.defaultRenderer();

    @Override
    protected void service(
            HttpServletRequest request,
            HttpServletResponse response
    ) throws ServletException, IOException {
        request.setAttribute("variable", "Hello");

        renderer.inlineDispatcherFor("{{ variable }} {{ name }}!")
                .with("name", "Jtwig")
                .render(request, response);
    }
}

The app variable

JtwigRenderer injects into the JtwigModel the app variable which exposes information extracted from the HttpServletRequest provided.

public class Application {
    private HttpRequest request;
}
public class HttpRequest {
    private Map<String, Object> parameter;
    private Map<String, Object> query;
    private Map<String, Object> session;
    private Map<String, Object> cookies;
}

This allows one to access:

  • GET query parameters (using app.request.query.parameterName)
  • POST parameters (using app.request.parameter.parameterName)
  • Session parameters (using app.request.session.parameterName)
  • Cookie parameters (using app.request.cookies.parameterName)

Integration

Integration of Jtwig Web in your project will depend on the dependency management mechanism being used. Also, you will need to make sure jcenter is part of your repository list. To check the most recent version, go to bintray.

Gradle

repositories {
    jcenter()
}
dependencies {
    compile 'org.jtwig:jtwig-web:1.X'
}

Maven

<repositories>
    <repository>
        <id>bintray</id>
        <url>https://jcenter.bintray.com/</url>
    </repository>
</repositories>

<dependencies>
    <dependency>
        <groupId>org.jtwig</groupId>
        <artifactId>jtwig-web</artifactId>
        <version>1.X</version>
    </dependency>
</dependencies>