Archive for the ‘spring’ tag
Using StringTemplate as the view engine for your Spring MVC application
I wish I never discovered Django templates because I shudder every time I have to use Freemarker or Velocity for my Spring MVC applications. Fortunately there is an alternative. You can use StringTemplate to generate your views in Spring MVC quite easily. StringTemplate enforces a strict separation of concerns, which therefore minimises the amount of logic that is allowed in the view, thus forcing you to do more in your controllers. Therefore your view remains purely for presentation purposes.
First things first is that you need to download StringTemplate and Antlr, and make those libraries available on your classpath. Next, extend Spring’s InternalResourceView.
import org.springframework.web.servlet.view.InternalResourceView;
import org.springframework.core.io.Resource;
import org.antlr.stringtemplate.StringTemplateGroup;
import org.antlr.stringtemplate.StringTemplate;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;
import java.io.PrintWriter;
public class StringTemplateView extends InternalResourceView {
@Override
protected void renderMergedOutputModel(Map model, HttpServletRequest request,
HttpServletResponse response) throws Exception {
Resource templateFile = getApplicationContext().getResource(getUrl());
StringTemplateGroup group = new StringTemplateGroup("webpages", templateFile.getFile().getParent());
StringTemplate template = group.getInstanceOf(getBeanName());
template.setAttributes(model);
PrintWriter writer = response.getWriter();
writer.print(template);
writer.flush();
writer.close();
}
}
Finally, you need to configure your Spring application context to use StringTemplate to render your views.
Now you can organise your StringTemplate files that have the “.st” suffix in /WEB-INF/templates. For example, you might want to organise your templates as follows.
WEB-INF
+ templates
+ layout
layout.st
+ partials
header.st
footer.st
feedback_form.st
contact_us.st
The layout.st template might look like the following template.
<html>
<body>
$partials/header()$
$body$
</body>
</html>
The above layout.st contains the basic structure for an HTML page on your site. You can use this one template to keep a consistent look and feel across your entire site. The layout.st template depends on the header.st and footer.st templates that exist in the partials directory.
To insert the feedback form in feedback_form.st into the $body$ placeholder attribute for your contacts page you simply create a contact_us.st template with the following line.
$layout/layout(body=partials/feedback_form())$
When your controller handles the request to display the contact us page, the controller will return a ModelAndView with the view name set to “contact_us”. The view name maps to the contact_us.st file in your templates directory. StringTemplate will render the contact_us page using the layout template with the header, footer and feedback form templates.
May Sydney Groovy Group
I am amazed with how many quality presenters we have had at the Sydney Groovy Group, and the number of project leads that have presented on their Groovy projects.
At the last meeting we got to hear about Griffon from James Williams, one of the project’s lead developers. Griffon is a Grails like framework for developing Swing based desktop applications. I was extremely impressed with Griffon, and how easy it was to create components without having to implement a bunch of event listeners or interfaces that you don’t need or care about. It was also interesting to see how the MVC pattern was applied to a Swing application. It made me wish Griffon was around 10 years ago when I was building Swing applications for Xylogy.
The May Sydney Groovy group will be meeting this Wednesday. Graeme Rocher will be giving a webinar on Grails, plus a run down on the recent GR8 Conference. So you’ll get to hear first hand on where Spring intends to take Groovy and Grails.
If you intend to make it to the meeting then please express your interest in the forums.
Spring just got a whole lot groovier
SpringSource (creators of the Spring framework) announced yesterday that they have acquired G2One (creators of Groovy and Grails). This is exciting news for all enterprise Java developers. SpringSource obviously views Groovy and Grails as strategic technologies for improving productivity with developing applications using the Spring framework.
Martin Fowler has blogged a lot on his bliki about Dependency Injection, and more recently about Domain Specific Languages (DSLs). With Groovy on Spring you get to have your cake and eat it too. Spring uses Dependency Injection which allows for a more loosely coupled architecture that is more testable and configurable. With Groovy you will be able to use the more expressive nature of the Groovy language to encapsulate your business rules in a DSL, then call them straight from your Java code. Yes that is correct, Groovy and Java play nicely together on the JVM. Groovy can also be used as a power tool for Java development: build scripts and functional tests can easily be written in Groovy, which saves you from writing XML, and tedious amounts of Selenium RC code in Java.
Warning: Gratuitous plug follows.
The next Groovy Sydney meeting will be held on Wednesday, 26 November 2008 at the ThoughtWorks Sydney office on Pitt St. More details of the meeting are located in the forums. Coincidentally it will be the last Groovy meeting in Spring as Summer is just around the corner.