integrating Thymeleaf with jersey

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

integrating Thymeleaf with jersey

gcameo
Hi All,
How can I use thymeleaf with jersey . I'm new to both but think the two makes a perfect marriage for a new app I'm working on and will like to replace my jsps with something forward looking.

In jersey, pages are returned like this

return Response.ok(new Viewable("/home.jsp", model)).build();

So I suspect it will be something along the lines of implementing a view technology to bind into the jersey request handling process.

Any ideas?

Thanks
Reply | Threaded
Open this post in threaded view
|

Re: integrating Thymeleaf with jersey

mrcritical
Give something like this a try:

@Provider
public class ThymeleafViewProcessor implements ViewProcessor<String> {

        @Context
        HttpServletRequest request;

        @Context
        HttpServletResponse response;

        @Context
        ServletContext servletContext;

        private TemplateEngine templateEngine;

        public ThymeleafViewProcessor() {
                TemplateResolver resolver = new ServletContextTemplateResolver();
                resolver.setPrefix("/WEB-INF/templates/");
                resolver.setSuffix(".html");
                resolver.setTemplateMode("HTML5");
                resolver.setCacheTTLMs(3600000L);
                TemplateEngine templateEngine = new TemplateEngine();
                templateEngine.setTemplateResolver(resolver);
        }

        /*
         * (non-Javadoc)
         *
         * @see com.sun.jersey.spi.template.ViewProcessor#resolve(java.lang.String)
         */
        @Override
        public String resolve(final String path) {
                return path;
        }

        /*
         * (non-Javadoc)
         *
         * @see com.sun.jersey.spi.template.ViewProcessor#writeTo(java.lang.Object,
         * com.sun.jersey.api.view.Viewable, java.io.OutputStream)
         */
        @SuppressWarnings("unchecked")
        @Override
        public void writeTo(final String resolvedPath, final Viewable viewable, final OutputStream out) throws IOException {
                // Commit the status and headers to the HttpServletResponse
                out.flush();
                WebContext context = new WebContext(request, servletContext, request.getLocale());
                Object model = viewable.getModel();
                if (Map.class.isAssignableFrom(model.getClass())) {
                        context.setVariables((Map<String, ?>) viewable.getModel());
                }
                templateEngine.process(viewable.getTemplateName(), context, response.getWriter());
        }
}
Reply | Threaded
Open this post in threaded view
|

Re: integrating Thymeleaf with jersey

liuhuan
In reply to this post by gcameo

We charge new pairs of cossack every now and then. We buy new shoes if the division changes or if we feel we do not accept a acceptable brace of shoes for some appropriate accident which we accept to appear and sometimes we just fell in adulation with a accurate brace of http://tomsleader.com/ toms women shoes shoes or shoe appearance and we cannot abide that account to own them and accordingly we buy them. But the shoe abutting Toms Shoes gives us an altogether altered acumen to buy shoes which is blue-blooded and humane. The accepted all-embracing shoe cast gives abroad a brace of new shoes to a adolescent in charge for every brace they sold. Accordingly they accord as abounding new shoes to beggared accouchement as abounding they sold.