Thursday, November 16, 2006

Poop Back and Forth

With form validation complete, the next step was to take a complete article and show it to the end user for previewing before final submission. One longstanding complaint among morons.org readers is that previewing capability is missing for just about everything. This is mainly because I'm lazy, but also because I didn't have a nice framework for managing this stuff... any previewing is a kludge in the JSP code.

Fortunately, Wicket makes this easy. Surprised?

I created a new HTML page called PreviewBeforeSubmit.html and of course its corresponding PreviewBeforeSubmit.java. I created all the appropriate labels and a tiny form with just two submit buttons. Then I ran into a head-scratcher: I can set my response page easily enough from the creation form with setResponsePage, but how does my completed ArticleBean model get over there?

The answer lies in the use of a WebSession which we create by overriding public ISessionFactory getSessionFactory() in the WebApplication. I'm beginning to form an appreciation for simple and small anonymous inner classes now, despite the hate I professed for them earlier. I think as long as they're kept tiny they're probably not too evil. In this case, here's the entire definition:

public ISessionFactory getSessionFactory() {
return new ISessionFactory() {

public Session newSession() {
return new ArticleSession(ArticleCreateApp.this);
}
};
}

(In this case, the anonymous inner class is an interface implementation. Since it's anonymous there's absolutely no chance of being retarded and sticking the suffix impl on the end of the class name.)

ArticleSession is quite simple. We get Eclipse to generate constructors from the super class (WebSession) that just call super and beyond that it's just a simple bean that holds an ArticleBean with the corresponding accessor methods.

Then, rather than constructing a new ArticleBean in the creation form, we get it from the ArticleSession (which instantiates it in the declaration). In the preview page, we simple retrieve this same object from the ArticleSession. Wicket takes care of getting the ArticleSession into and out of the low-level session object, in this case HttpSession. Note that the session is associated with the web application, not any particular page.

The first time I tried this strategy, I got a big nasty exception that was rooted in a ClassCastException. This turned out to be because I still had my old session sitting around on disk, which contained a default implementation of a WebSession. The solution is to just delete Tomcat's sessions file (after stopping Tomcat) so that a new session is built.

Managing the two buttons on the small form on the preview page is easy, because the Button object has an onSubmit method you can override, which of course can execute setResponsePage to move you anywhere you'd like. With the buttons in place, going back and forth between the form and the preview is trivial, and pretty quick-- even with the HTML tidying in the form validation, it takes less than half a second (usually around 0.3s) to get between pages.

The other tidbit from tonight's work is that if you want HTML to pass through to the page literally, it is necessary to setEscapeModelStrings(false) on the associated Label.

Comments: Post a Comment





<< Home

This page is powered by Blogger. Isn't yours?

Subscribe to Posts [Atom]