/java/

JEE + JAX-RS + Thymeleaf (way3)

2016-04-08 23:14:20

https://github.com/szalek/JEE-Thymeleaf

Controller

package pl.btbw.modules.home;

import pl.btbw.core.thymeleaf.HtmlResponse;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import java.util.ArrayList;
import java.util.List;

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

    @GET
    @Path("/html")
    public HtmlResponse html2() {

        List

<addressview> addresses = new ArrayList&lt;&gt;();
        addresses.add(new AddressView("Warszawa"));
        addresses.add(new AddressView("Gdańsk"));
        addresses.add(new AddressView("Kraków"));

        return new HtmlResponse("test.html")
                .add("home", new HomeView("Lorem Ipsum"))
                .add("address", new AddressView("Gdańsk", "Poland", "00-000", 12))
                .add("addresses", addresses)
                ;
    }

}

html - ..src/main/resources/test.html

<!DOCTYPE html>

<html xmlns:th='"http://www.thymeleaf.org"'>
<meta charset="utf-8"/>
<body>
<h2>Title</h2>
<p th:text='"${home.message}"'>Test paragraph</p>
<hr/>
<h2>Address</h2>
<span th:text='"${address.city}"'></span><br/>
<span th:text='"${address.country}"'></span><br/>
<span th:text='"${address.zipCode}"'></span><br/>
<span th:text='"${address.number}"'></span>
<hr/>
<h2>Addresses</h2>
<ul>
<li ${addresses}&quot;="" :="" th:each='"address'>
<span th:text='"${address.city}"'></span><br/>
</li>
</ul>
<hr/>
</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.8
                    <target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactid>maven-war-plugin</artifactid>
<configuration>
<failonmissingwebxml>false</failonmissingwebxml>
</configuration>
</plugin>
</plugins>
</build>
</project>

Configuration

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.modules.home;

public class HomeView {

    public String message;

    public HomeView(String message) {
        this.message = message;
    }
}

package pl.btbw.modules.home;

public class AddressView {

    public String city;
    public String country;
    public String zipCode;
    public int number;

    public AddressView(String city) {
        this.city = city;
    }

    public AddressView(String city, String country, String zipCode, int number) {
        this.city = city;
        this.country = country;
        this.zipCode = zipCode;
        this.number = number;
    }
}

Html Response

package pl.btbw.core.thymeleaf;

import java.util.HashMap;
import java.util.Map;

public class HtmlResponse {

    public String templateName;
    public Map<string, object=""> model = new HashMap&lt;&gt;();

    public HtmlResponse(String templateName) {
        this.templateName = templateName;
    }

    public HtmlResponse add(String modelName, Object model) {
        this.model.put(modelName, model);
        return this;
    }
}

BodyWriter

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();
        response.model.forEach((k, v) -&gt; context.setVariable(k, v));
        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);
    }

}