ClickAider
You are currently browsing the Bogle’s Blog weblog archives for the day Friday, October 28th, 2005.

Brad Neuberg: Bookmark and Back buttons support in Ajax applications

In Alex Bosworth’s list of “Ajax Mistakes”:http://sourcelabs.com/ajb/archives/2005/05/ajax_mistakes.html, breaking the back button and breaking bookmarks are two of the worst.

There’s progress afoot. Brad Neuberg presents an “open source Javascript library”:http://www.onjava.com/pub/a/onjava/2005/10/26/ajax-handling-bookmarks-and-back-button.html that


brings bookmarking and back button support to AJAX applications. By the end of this tutorial, developers will have a solution to an AJAX problem that not even Google Maps or Gmail possesses: robust, usable bookmarking and back and forward behavior that works exactly like the rest of the Web.

Sadly, he found it impossible to find a technique that worked with Safari. What a step it would be if the browser makers got their heads together and built a reliable, unified platform for AJAX applications.

Ruby On Spring

At Jobster, we are currently developing a rapid prototyping package that integrates the JRuby dynamic language with the Spring application framework. This post provides an introduction via examples.

The goals of the project are to permit rapid innovation and iteration, full reuse of our existing Spring and Hibernate business logic and objects, and a smooth transition from prototype to production code.

We were inspired by many of the benefits and philosophy of Ruby on Rails, including avoiding configuration files and compilation. Make a change, and you’ll see it immediately. At the same time, if you’re already commited to Spring, the package doesn’t require you to learn a new framework or abandon your existing investment in Spring. The total size of the package is less than a 1000 lines, so it’s small and easily integrated into existing projects.

The key part of the system is a Spring controller that instantiates a JRuby script engine and executes a JRuby script to define the controller and the model. The view is provided by a standard JSP file. If the JRuby controller is replaced by a Java one, the view can be used as-is with no changes.

Hello World

The simplest possible example consists of a JRuby script “hello.rb” which returns a model object and a JSP view “hello.jsp” which uses that model. To try the example, I just need to drop these two files into the “rad” directory on my Tomcat server. No compilation, configuration files, or server restarts are required.

hello.rb
{ ‘greeting’ => ‘Hello world’}

hello.jsp
<html> ${command[’greeting’]} </html>
Notice that rather than defining an explicit controller class, the script simply returns a model object. This is a shortcut for rapid prototyping. Equivilantly, we could have explicitly defined a controller object as follows:

hello.rb
class DemoController
   def getModel(request)
     { ‘greeting’ => ‘Hello world’}
   end
end

DemoController.new()

Reusing Spring Beans

Here’s a more interesting example that reuses business logic from a Spring bean implemented in Java. ($context represents the Spring application context.) Thanks to the magic of JRuby, we can interact and create Java objects just as easily as Ruby ones.

location.rb
locationCalculator = $context.getBean(’locationCalculator’)
{’locations’ => locationCalculator.getPointMatches(’seattle’)}

Hibernate Queries

The package also supports Hibernate. The follow example uses Hibernate to fetch a list of company objects starting with a given letter. Note that the query returns full fledged Java objects with behavior, not just data.

hibernate.rb
s = $request.getParameter(’s’)
companies = $helper.queryHibernate(”from jobster.model.Company where name>=?”, [s])
{’companies’ => companies, ‘label’ => “Companies starting with #{s}”}
The corresponding JSP view uses standard Spring conventions and could be used without changes with a Java controller.

hibernate.jsp
<div class=”left pad”>
<div class=”bold”>Companies: ${command.label}</div>
<ol>
<c:forEach var=”company” items=”${command.companies}”>
<li> ${company.name} <br />
</li></c:forEach>
</ol>
</div>

onSubmit Handlers

Our final example is a simple calculator controller. It implements an onSubmit handler to run business logic (computing the calculation) when the user submits the form and choose a new result view.

calculator.rb
class CalculatorController
   def getModel(request)
     {’x’ => 2, ‘y’ => 2}
   end

   def onSubmit(request, response, form, errors)
     form[’total’] = form[’x'].to_i + form[’y'].to_i
     form[’viewName’] = ‘/rad/calculator_results’
   return form
   end
end

CalculatorController.new()

Download

Read More and Download

I’m switching

In my home, it’s now farewell Windows, hello Mac Mini.

I ordered the high end 80GB/512MB version on Ebay for under $470 including shipping. PC Connection seems to have a lot of good deals on nearly new Macs going these days.

My tipping point was the spyware that I spent weeks scraping off of the PC. The price/performance of the Mini is hard to beat; it’s beautiful, easy to use, Unix based, yada yada yada.

There are two perhaps more ironic factors that contribute to my switch. The first is the effectiveness of terminal server and broadband. If I’m working on a Windows PC, often as not it’s one at work through a terminal services connection.

The second is the ubiquity of Windows notebooks. If I need Windows at home, and for some reason Virtual PC for Mac isn’t enough, I can always use my notebook.

I’m looking forward to the chance to develop some Mac applications for fun; I’ll have to find more about the developer tools that people are using.