views:

358

answers:

3

Hello, is there a way to use groovy builders to build JSP files in a Grails application keeping things enough integrated?

To explain better: by default Grails uses gsp files that are nice but quite verbose..

<div class="clear">
  <ul id="nav">  
    <li><g:link controller="snippets" action="list">Snippets</g:link></li>
    <li><g:link controller="users" action="list">Users</g:link></li>
    <li><g:link controller="problems" action="list">Problems</g:link></li>
    <li><g:link controller="messages" action="list">Messages</g:link></li>
  </div>
<div id="content">

is there a way to use groovy.xml.MarkupBuilder tha would turn the previous piece into

div(class:'clear') {
  ul(id:'nav') {
    li { g_link(controller:'snippets', action:'list', 'Snippets') }
// and so on

Of course g_link is invented just to give the idea..

+1  A: 

I don't have a complete answer for you, but I suspect the key will be gaining access to the "view resolvers". In a normal SpringMVC app, these are configured in views.properties (or views.xml) as follows:

csv=com.example.MyCSVResolver
xml=com.example.MyXMLResolver
audio=com.example.MySpeechResolver

In a regular SpringMVC app, you return something like new ModelAndView(myModel, 'csv') from a controller action.

This would cause the CSVResolver class to be invoked passing it the data in myModel. In addition to containing the data to be rendered, myModel would likely also contain some formatting options (e.g. column widths).

Spring searches the views file for a key matching the view name. If it doesn't find a match, by default it just renders a JSP with the view name and passes it the model data.

Now back to Grails....remember that Grails is really just a Groovy API over SpringMVC and most of the features of SpringMVC can be accessed from Grails. So if you can figure out how to modify the views file, just change your controller actions to return an appropriate ModelAndView instance, and it should work as described above.

Don
+3  A: 

Do a search for builder under the web layer section of the grails user guide. There is an example in there that shows you exactly how to do this using the xml builder.

Blacktiger
A: 

GSP allows you to run arbitrary Groovy code inside <% %> brackets. So you can have something like this (borrowing example from page linked to by BlackTiger):

<%      StringWriter w = new StringWriter()
        def builder = new groovy.xml.MarkupBuilder(w)
        builder.html{
            head{
                title 'Log in'
            }
            body{
                h1 'Hello'
                builder.form{ }   
            } 
        }     
    out << w.toString() 
%>

Note that the above calls g:form tag, and you can pass additional stuff to it.

So what you are asking for is certainly possible, though I am not sure if it will end up being a win. I'd suggest you perhaps look more at TagLibs in combination with Templates and SiteMesh Layouts - can definitely simplify things tremendously.

Jean Barmash