Tuesday, February 15, 2011

Grails View as Spring MVC View


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.


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

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.


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


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

The Groovy server pages

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.


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.
Maybe a partnership or more ?.


  1. Nice article, but what do you mean by saying "these tools sound outdated, always usefull but outdated.", and then talking about JSF, which IS really outdated by means of approach of providing less control, complex unnecessary flow, overweight pages which are semantically wrong when HTML is produced and non-compatible implementations?

    Probably Grails is more fresh, I don't know, just starting trying, but I definitely can't name JSP/JSTL + sitemesh outdated, since they provide powerful control over end HTML, which is quite important for developers, crawlers and web-clients

  2. Some of your statement about JSF are not true. Have a look at my benchmark, Primefaces is quite optimized and it renders a clean and a lightweight HTML. JSF flow has been improved with JSF 2 and you can used Spring Webflow or Seam.

    Yes, something can be usefull and provide powerful control over end HTML. It can on the other side outdated because newer tools provide more powerful control over end HTML and ease and improve the configuration and the developement.

    Grais view, Play! view (not exhaustive list) are in my opinion this newer tools.

    I encourage you to look at Grails.

  3. MVC applied to WSGI or Java EE, is the Servlet a controller, dispatcher, or both? I think I've seen system diagrams where the controller and the dispatcher are different. Thanks for sharing your informative post.

  4. Your posts is really helpful for me.Thanks for your wonderful post. I am very happy to read your post. It is really very helpful for us and I have gathered some important information from this blog.

    JAVA J2EE Training in Chennai

  5. Is it possible to do that without Grails? I mean using GSP pages as views gluing Spring MVC framework? Thanks

  6. I have learned much from your post. I would definitely bookmark your site to be updated with your upcoming articles. Great job! So much information...
    iOS Training in Chennai | Informatica Training in Chennai

  7. The information you have given here are most worthy for me. I have implemented in my training program as well, thanks for sharing.

    Hadoop Training Chennai
    Hadoop Training in Chennai

  8. I have read your blog, it was good to read & I am getting some useful info's through your blog keep sharing... Informatica is an ETL tools helps to transform your old business leads into new vision. Learn Informatica training in chennaifrom corporate professionals with very good experience in informatica tool.
    Best Informatica Training In Chennai|Informatica training center in Chennai

  9. Thanks for splitting your comprehension with us. It’s really useful to me & I hope it helps the people who in need of this vital information.
    ccna training in Chennai|ccna institutes in Chennai|ccna courses in Chennai

  10. Thanks Admin for sharing such a useful post, I hope it’s useful to many individuals for developing their skill to get good career.
    SAP training in chennai|SAP course in chennai|SAP Training in Chennai|sap course in Chennai

  11. Really awesome blog. Your blog is really useful for me. Thanks for sharing this informative blog.

    CCNA Training in Chennai | CCNA Training Institute in Chennai | Best CCNA Training in Chennai

  12. It is amazing and wonderful to visit your site.Thanks for sharing this information,this is useful to me...
    Android Training in Chennai
    Ios Training in Chennai

  13. Excellent Post, Interesting Article.Indias Fastest Local Search Engine, you can Search Anything, From anywhere at any time CALL360

  14. Awesome article. It is so detailed and well formatted that i enjoyed reading it as well as get some new information too.
    Devops interview Questions

  15. Needed to compose you a very little word to thank you yet again regarding the nice suggestions you’ve contributed here.

    hadoop training in chennai

    hadoop training in bangalore

    hadoop online training

    hadoop training in pune

  16. Appreciating the persistence, you put into your blog and detailed information you provide.
    offshore safety course in chennai

  17. I enjoy what you guys are usually up too. This sort of clever work and coverage! Keep up the wonderful works guys I’ve added you guys to my blog roll.
    safety course in chennai

  18. Your post is really awesome. Your blog is really helpful for me to develop my skills in a right way. Thanks for sharing this unique information with us.
    - Learn Digital Academy

  19. can you offer guest writers to write content for you? I wouldn’t mind producing a post or elaborating on some the subjects you write concerning here. Again, awesome weblog!
    occupational health and safety course in chennai

  20. Nice post. Thanks for sharing! I want people to know just how good this information is in your article. It’s interesting content and Great work.
    Thanks & Regards,
    VRIT Professionals,
    No.1 Leading Web Designing Training Institute In Chennai.

    And also those who are looking for
    Web Designing Training Institute in Chennai
    SEO Training Institute in Chennai
    Photoshop Training Institute in Chennai
    PHP & Mysql Training Institute in Chennai
    Android Training Institute in Chennai

  21. I believe there are many more pleasurable opportunities ahead for individuals that looked at your site.
    Azure Training in Chennai

  22. Nice I have checked a couple of them. I figure You Should likewise consider making a rundown of Indian named a client I'm seeing great reaction from Indian individuals as well
    Contact us :- https://www.login4ites.com

  23. I really enjoyed your blog Thanks for sharing such an informative post.
    Best- Web -Designing -services- in -Noida

  24. Much obliged for the valuable data. It's progressively useful and straightforward. Please help me suggest best webdesigning in noida and best bookmarking submission list and best classified list
    Contact us :- https://www.login4ites.com