Over a million developers have joined DZone.

Grails View as Spring MVC View

· Java Zone

What every Java engineer should know about microservices: Reactive Microservices Architecture.  Brought to you in partnership with Lightbend.

Introduction

The reference documentation of Spring 3 contains the following statements:

One of the areas in which Spring excels is in the separation of view technologies from the rest of the MVC framework. For example, deciding to use Velocity or XSLT in place of an existing JSP is primarily a matter of configuration. This chapter covers the major view technologies that work with Spring and touches briefly on how to add new ones.

The supported views are "JSP & JSTL", "Tiles", "Velocity & FreeMarker", "XSLT". Those are the major view technologies that work with Spring. The Spring MVC stack is usually : Spring MVC + JSP/JSTL + Tiles or SiteMesh. All these tools sound outdated, always usefull but outdated. On the other side there are frameworks like JSF, Grails, Play! (not exhaustive list) which come with great views more up to date.

Grails is the most logical choice because it uses Spring MVC internally. The task should be easier. To be honest this choice is not a very hard one because Grails view is elegant.

First step : web.xml

I basicaly replace the Grails dispatcher servlet with the Spring MVC dispatcher servlet. This dispatcher takes a configuration file, usualy named servlet-context.xml. There is no other specific configuration to do.

Second step : Configuring Grails

Grails has his own directories structure which is different of a standard Java Web Application. Resources resolver depend by default on this structure. Hopefully Grails supports WAR packaging so Grails can be configured to support a standard Java Web Application directories structure.
This configuration can be done through 2 files.

application.properties

This file is stored in the resources directory. It should contain :

grails.buildScope=war
grails.war.deployed=true
grails.env=development

The important line is grails.war.deployed.
Once set to true Grails resources resolver will use a WAR structure and Grails will cache GSP and not reload them. This parameter seems to be used for production mode only.

Config.groovy

This file is stored in the groovy directory. GSP reloading can be enabled with the following line :

grails.gsp.enable.reload=true

Limitations

All the Grails taglibs cannot be used or should be used with caution. This is especially true for tags which use Grails controllers : g:form, g:link...

But creating a Grails taglib is quite easy.

Test sample

The Spring MVC controller

@Controller
@RequestMapping(value = "/test")
public class TestController {

@RequestMapping(method = RequestMethod.GET, value = "/1")
public ModelAndView getCreateForm() {
Map map = new HashMap();

Account account = new Account();
account.setName("John Doe");

map.put("foo", "foo value");
map.put("account", account);

return new ModelAndView("/test", map);
}
}

The Groovy server pages

<ui:composition template="/composition">
<ui:define composition="${it}" name="menu">
<g:render template="/template" model="[testTemplate:foo]"/>
</ui:define>

<ui:define composition="${it}" name="body">
${account.name}
</ui:define>
</ui:composition>

This sample shows a Spring controller and a Groovy server pages. The GSP uses a UI composition pattern taglib I create (similar to facelet), a gsp template and a gsp layout.

Conclusion

The task is very easy. Nevertheless I lost quite some time on different things because of my poor Grails' knowledge. I created a custom view resolver and a custom context loader. All with some debugging to understand how Grails handles resources loading.

Nevertheless with these quite simple configurations you can use GSP as Spring MVC view. There are probably some other settings to do but for now this configuration seems to work perfectly.

The project is available on github.

In my opinion there is a big lack on the view side in the Spring MVC project. I'm really wondering what SpringSource is preparing.

 

Microservices for Java, explained. Revitalize your legacy systems (and your career) with Reactive Microservices Architecture, a free O'Reilly book. Brought to you in partnership with Lightbend.

Topics:

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

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

{{ parent.tldr }}

{{ parent.urlSource.name }}