/java/

JEE + JAX-RS + Thymeleaf (way2)

2016-04-08 22:45:05

Controller

package pl.btbw.modules.home;

import pl.btbw.core.thymeleaf.HtmlResponse;

import javax.ws.rs.GET;
import javax.ws.rs.Path;

@Path("/home")
public class HomeController {

    @GET
    @Path("/html2")
    public HtmlResponse html2() {
        return new HtmlResponse("test2.html", new HomeView("Lorem Ipsum"));
    }

}

html - ..src/main/resources/test2.html

<!DOCTYPE html>

<html xmlns:th='"http://www.thymeleaf.org"'>
<body>
<p th:text='"${model.message}"'>...</p>
</body>
</html>

pom.xml

<?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?>

<project http:="" maven-4.0.0.xsd&quot;="" maven.apache.org="" xmlns='"http://maven.apache.org/POM/4.0.0"' xmlns:xsi='"http://www.w3.org/2001/XMLSchema-instance"' xsd="" xsi:schemalocation='"http://maven.apache.org/POM/4.0.0'>
<modelversion>4.0.0</modelversion>
<groupid>pl.btbw</groupid>
<artifactid>jeeThymeleaf</artifactid>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupid>javax</groupid>
<artifactid>javaee-api</artifactid>
<version>7.0</version>
</dependency>
<dependency>
<groupid>org.thymeleaf</groupid>
<artifactid>thymeleaf</artifactid>
<version>2.1.4.RELEASE</version>
</dependency>
</dependencies>
<build>
<finalname>jeet</finalname>
<plugins>
<plugin>
<groupid>org.apache.maven.plugins</groupid>
<artifactid>maven-compiler-plugin</artifactid>
<configuration>
<source/>1.7
                    <target>1.7</target>
</configuration>
</plugin>
<plugin>
<artifactid>maven-war-plugin</artifactid>
<configuration>
<failonmissingwebxml>false</failonmissingwebxml>
</configuration>
</plugin>
</plugins>
</build>
</project>

Config

package pl.btbw.core;

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

@ApplicationPath("/rest")
public class App extends Application {
}
package pl.btbw.core.thymeleaf;

public class HtmlResponse {

    public String templateName;
    public Object model;

    public HtmlResponse(String templateName, Object model) {
        this.templateName = templateName;
        this.model = model;
    }
}
package pl.btbw.core.thymeleaf;

import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver;

import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.ext.MessageBodyWriter;
import javax.ws.rs.ext.Provider;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;

@Provider
@Produces(MediaType.TEXT_HTML)
public class HtmlResponseMessageBodyWriter implements MessageBodyWriter

<htmlresponse> {

    private static TemplateEngine templateEngine;

    static {
        templateEngine = new TemplateEngine();
        templateEngine.setTemplateResolver(new ClassLoaderTemplateResolver());
    }

    @Override
    public void writeTo(HtmlResponse response, Class<? > type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<string, object=""> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException {

        Context context = new Context();
        context.setVariable("model", response.model);

        Writer writer = new OutputStreamWriter(entityStream);
        templateEngine.process(response.templateName, context, writer);
        writer.flush();
    }

    @Override
    public long getSize(HtmlResponse arg0, Class<? > arg1, Type arg2, Annotation[] arg3, MediaType arg4) {
        return -1;
    }

    @Override
    public boolean isWriteable(Class<? > type, Type genericType, Annotation[] annotations, MediaType mediaType) {
        return HtmlResponse.class.isAssignableFrom(type);
    }

}