Over a million developers have joined DZone.

Using Mustache.java Templates With Struts 2

· Java Zone

Discover how AppDynamics steps in to upgrade your performance game and prevent your enterprise from these top 10 Java performance problems, brought to you in partnership with AppDynamics.

A friend recently recommended I take a look at the Mustache templating engine. It’s clean, simple and designer friendly, and promotes logic minimization on the template side (I don’t like the term “logic-less”, I don’t think you can get away with absolutely zero logic).

To try it out I decided to build a simple Struts 2 based web app, but I found that there was no out-of-the-box integration between the two. Thankfully both frameworks are easily extendable so I managed to get them playing along quite easily.

Mustache has been ported to a number of platforms (Javascript, Ruby, Python and Java amongst others). The Java implementation (mustache.java) is maintained as an independant project and seems to be quite active.

In order to use Mustache templates as views in my Struts app I decided that creating a new result type was the most elegant approach:

package com.ricardozuasti.mustache;
import com.github.mustachejava.*;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.util.logging.Logger;
import com.opensymphony.xwork2.util.logging.LoggerFactory;
import java.io.File;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.dispatcher.PlainTextResult;
public class MustacheResult extends PlainTextResult {
    private static final Logger LOG = LoggerFactory.getLogger(MustacheResult.class);
    protected void doExecute(String finalLocation, ActionInvocation invocation) throws Exception {
        HttpServletResponse response = (HttpServletResponse) invocation.getInvocationContext().get(HTTP_RESPONSE);
        ServletContext servletContext = (ServletContext) invocation.getInvocationContext().get(SERVLET_CONTEXT);
        PrintWriter writer = response.getWriter();
        // Open a stream to read the template passed to the action
        InputStreamReader reader = new FileReader(servletContext.getRealPath(finalLocation));
        if (reader == null) {
            LOG.warn("resource at location ["+finalLocation+"] cannot be obtained (return null) from ServletContext !!! ");
        else {
            // We need to pass the real path of the templates to the Mustache compiler, in order to support nested templates
            String resourceRoot = servletContext.getRealPath(finalLocation);
            resourceRoot = resourceRoot.substring(0, resourceRoot.lastIndexOf("/")+1);
            MustacheFactory mf = new DefaultMustacheFactory(new File(resourceRoot));
            Mustache mustache = mf.compile(reader, "mustacheResult");
            mustache.execute(writer, invocation.getAction());

I extended the PlainTextResult provided by Struts, using the Mustache compiler to pre-process the specified view resources passed onto the executed Struts action.

You can use any of the executed action bean public attributes in your templates as variables. If you want to add any other Java objects to the Mustache scope, you can modify the mustache.execute() line to receive an Object array with all you need.

After this new result type is included in our app, we must configure it in our struts.xml file and just use it in our actions configuration

    <result-type name="mustache" class="com.ricardozuasti.mustache.MustacheResult" />
<action name="index" class="com.ricardozuasti.mustache.SomeAction">
    <result type="mustache">/somepage.html</result>

I used the .html extension in the example, but you can use .mustache or whatever you prefer. I rather use .html so the templates can be opened with a regular browser outside of the running app, this facilitates working with the templates for pure HTML/CSS purposes.

Check out the mustache documentation for details on how to create your templates.



The Java Zone is brought to you in partnership with AppDynamics. AppDynamics helps you gain the fundamentals behind application performance, and implement best practices so you can proactively analyze and act on performance problems as they arise, and more specifically with your Java applications. Start a Free Trial.


Published at DZone with permission of Ricardo Zuasti, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}