|
Helping ordinary people create extraordinary websites! |
Practically Groovy: Go Server-Side Up, with GroovyBy Andrew Glover2005-05-05
What About Those GSPs? So far, I've focused on writing Groovlets. As you'll see, however, Groovy's GSP pages easily complement the Groovlets framework, much like JSPs complement the Servlet API. On the surface, GSPs look just like JSPs, but actually, they couldn't be more different because the GSP framework is really a template engine. If you're unfamiliar with template engines, you might want to quickly review last month's article. While GSPs and JSPs are fundamentally different technologies, they are similar in that GSPs are excellent candidates for embodying the view of a Web application. As you might recall from last month, a view-facilitating technology lets you separate the concerns of your application's business logic and its corresponding view. If you take a quick look back at the diagnostic Groovlet in Listing 6, you might see where it could stand to be improved with GSP code. Yep, that Groovlet is kind of ugly, isn't it? The problem is that it mixes application logic and a load of printlns to output the HTML. Fortunately, I can resolve the situation by creating a simple GSP to complement the Groovlet. An example GSP Creating GSPs is as easy as creating Groovlets. The key to GSP development is realizing that a GSP is essentially a template and, therefore, may be best served by limited logic. I'll create a simple GSP in Listing 7 to get us started: Listing 7. A simple GSP <html>Looking at the above GSP should seriously remind you of standard Groovy template development. The syntax is JSP-ish in its use of <%s, but, like the Groovlet framework, it lets you access common servlet objects, such as ServletRequest, ServletResponse, ServletContext, as well as HttpSession objects. Tutorial Pages: » On-the-fly Server-Side Programming with Groovlets and GSPs » Defining Functions in Scripts » Groovlets and GSPs » The Groovlet, Please » A Diagnostic Groovlet » What About Those GSPs? » Refactor me this ... » Conclusion » Resources First published by IBM DeveloperWorks |
|