17

It's hard to see how StringTemplate integrates easily (or not) with popular Java web MVC frameworks.

Which Java MVC frameworks integrate easily with StringTemplate?

A good answer:

  • mentions one solution to integrate with a framework,
  • includes a link to something useful and applicable, like:
    • a tutorial,
    • or documentation,
    • or a reference to source code:
      • free,
      • and open source or public domain.

Readers/Voters, please vote for a solution if you know it's true and great.

In the scope of this question, I am not interested in any other templating engine than StringTemplate.

Daniel Jomphe
  • 16,991
  • 5
  • 29
  • 30
  • 1
    Great question! Terrence is a genius, but he's still a bit academic. StringTemplate doesn't get the attention it deserves in large part because (afaik) no frameworks feature it. – erickson Jan 28 '09 at 22:47
  • It's incredible how out of the map StringTemplate is. It's already been 20 hours this question is up, and it's already buried - with 0 official integration solution. ...Looks like the ball is in our camp to help ST gain tractable presence in MVCs. – Daniel Jomphe Jan 29 '09 at 15:36
  • Hiya! Yeah, ST will never be widely known w/o a big framework wrapped around it. I came to loathe building sites, though, after making jguru server. Nothing I can do to improve the situation, I'm afraid. Very happy to hear people like it. It's influential because I think it does the right thing but... Because of ANTLR, ST is well known in code gen and translation circles. – Terence Parr Jun 02 '10 at 21:51
  • I would think you could fit it into most frameworks. I'm using it with slim3. What I have done is instead of forwarding to JSP files from my Servlets, I render a template. In slim3, this happens in Controller classes. I've (so far) completely eliminated the JSP from my project. – JR Lawhorne May 28 '14 at 03:01

5 Answers5

11

I've gotten StringTemplate to work with Spring. Basically, all it took was a custom view.

But first, a disclaimer: This is an experimental hack. I've never used this in production code, and it could use some improvement before that happens. I think it is adequate to answer your question about how easily StringTemplate integrates with a Web MVC framework, however.

Reference: Spring Web MVC documentation

StringTemplateView.java:

import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.antlr.stringtemplate.StringTemplate;
import org.antlr.stringtemplate.StringTemplateGroup;
import org.springframework.core.io.Resource;
import org.springframework.web.servlet.view.InternalResourceView;

public class StringTemplateView extends InternalResourceView {

    @Override
    protected void renderMergedOutputModel(Map model, HttpServletRequest request,
            HttpServletResponse response) throws Exception {

        // Provides a Spring resource descriptor referring to the .st file
        Resource templateFile = getApplicationContext().getResource(getUrl());

        // Kind of redundant...
        StringTemplateGroup group = new StringTemplateGroup("group", templateFile.getFile().getParent());
        StringTemplate template = group.getInstanceOf(getBeanName());
        template.setAttributes(model);

        // Output to client
        PrintWriter writer = response.getWriter();
        writer.print(template);
        writer.flush();
        writer.close();
    }
}

And an example view resolver definition:

<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass" value="myapp.web.view.StringTemplateView"/>
    <property name="prefix" value="/WEB-INF/st-views/"/>
    <property name="suffix" value=".st"/>
</bean>
Jeff
  • 21,744
  • 6
  • 51
  • 55
  • +1. One thing I noticed with this code is that it does not work if your templates live in a subdirectory of /WEB-INF/st-views/. What I ended up doing was reconstructing the "prefix" using what effectively is getUrl().substring(0, getUrl().indexOf(getBeanName())); and using it in the getResource() call. Also I dropped the getParent() call when instantiating StringTemplateGroup(). – Steve Kalemkiewicz Jun 22 '12 at 04:27
2

A quick search led me to this demonstration of using StringTemplate for a Spring view.

yawmark
  • 1,934
  • 14
  • 16
1

The open source Java WEB framework JPublish, works very well with ST. By following the link above, you'll find there the following:

  • a JPublish framework user guide
  • a practical demo (downloadable from Google code ~5.7MB) showing you how to use ST from JPublish. You'll realize how easy it is.

Have fun,

Florin
  • 528
  • 5
  • 14
1

There is a not bad implementation of StringTemplate as a view for Spring MVC that also supports SiteMesh integration.

https://github.com/tomcz/spring-stringtemplate/

There is an example implementation of a webapp using ST as the view technology within this project (including the sitemesh integration). It is not worth repeating it here, so the pointer to it directly is:

https://github.com/tomcz/spring-stringtemplate/tree/master/src/test/webapp/WEB-INF

The author has also provided support for encoding/escaping output for XSS prevention. This support exists for HTML, CSS, XML, JS and URLs.

If you're a Maven user, the project is available on central

<dependency>
  <groupId>com.watchitlater</groupId>
  <artifactId>spring-stringtemplate</artifactId>
  <version>1.5.1</version>
</dependency>
Bradley Dwyer
  • 8,102
  • 5
  • 32
  • 28
0

A certain Harry Karamidas shared, in December 2008, a Struts adapter on ST's official site. Direct link to the zip file.

Daniel Jomphe
  • 16,991
  • 5
  • 29
  • 30