|
Helping ordinary people create extraordinary websites! |
Practically Groovy: Go Server-Side Up, with GroovyBy Andrew Glover2005-05-05
Refactor me this ... You can learn a lot from refactoring older code as your experience with a programming language or platform grows. I'd like to revisit the simple reporting application from January's column, when you were just learning learning about GroovySql. As you'll recall, I built a quick-and-dirty reporting application that could have multiple uses within an organization. As it turns out, the application has since become quite popular for studying activity on the company database. Now, nontechnical personnel want to have access to this stupendous report, but they don't want the overhead of having to install Groovy on their machines to run it. I kind of predicted this would happen, and the solution seems practically obvious: I'll Web-enable the reporting application. Lucky for me, Groovlets and GSPs will make the refactoring a snap. Refactoring the reporting application First, I'll tackle the guts of the simple application from Listing 12 of the GroovySql article. Refactoring this is easy: I simply replace all the printlns with logic that places an instance variable in the HttpRequest object using the setAttribute() method. My next step is to forward the request, using a RequestDispatcher, to a GSP that will handle the view component of the reporting application. The new report Groovlet is defined in Listing 8: Listing 8. The refactored database reporting application import groovy.sql.SqlThe code in Listing 8 should be quite familiar. I've simply replaced all the printlns from the previous application and added a forward function to handle the view portion of the report. Adding the view component My next step is to create the GSP to handle the reporting application's view. Because I'm an engineer and not an artist, my view is rather simple -- a tad of HTML with a table, as shown in Listing 9: Listing 9. The view component of the report <html><head>Running the new report should result in the output found in Listing 3. Of course, numbers will vary. Figure 3. Output from the refactored reporting application
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 |
|