GSP and JSP
The question why Grails has GSP when there is JSP is a natural one. Here are a few substantial differences:
- A GSP allows the same ${…} expressions as in a Groovy GString. JSP EL expressions are restricted to object navigation, which is generally a good idea but can also make it difficult to access anything that isn’t a getter (e.g. String length()). JSTL functions provide some help but the more general case requires creating static helper methods, registering them through a taglib descriptor, adding a taglib declaration, and finally using the function.
- Using Groovy in logical and iterative tags comes with major perks such as the safe navigation operator: ${customer.phones?.size()} or the Elvis operator: ${totalCount ?: 10}.
- The ability to invoke Grails dynamic tags as methods makes it easier to produce well-formed markup:
<!-- With a regular tag --> <a href="<g:createLink action="list" />">A dynamic link</a> <!-- As a method call --> <a href="${createLink(action:'list')}">A dynamic link</a> - GSP provides a much simpler mechanism for creating and testing custom tag libraries in Groovy that does not require tld files and taglib declarations and of course uses Groovy. Here is a sample test case for a Grails tag library:
import grails.test.* class CustomerTagLibTests extends TagLibUnitTestCase { void testRepeat() { tagLib.repeat(times: '2') { 'output<br/>' } assertEquals 'output<br/>output<br/>', tagLib.out.toString() } } - The Grails paginate tag. It is such a common need for web applications. In comparison to the Displaytag library I think the Grails paginate tag is more lightweight and more usable.
juhu said,
December 15, 2009 @ 4:59 pm
But Displaytag does more then paginate. It also groups, format and export the lists to csv, pdf, excel an xml.
Administrator said,
January 9, 2010 @ 10:49 am
You’re right juhu. Displaytag is a very feature-rich tag library (perhaps a little bit to the detriment of clarity) whereas the Grails paginate tag is more narrowly focused on pagination as its name indicates. What I had in mind was that displaytag wraps the generation of the entire table, which in turn requires you to know how to customize its style, links, query parameters, etc.