Spring MVC Wizards & Form Taglib
I've been building a JSP based website for a course module titled "Technologies for Electronic Commerce". It's finished now, but I've only just got round to writing up this post.
As part of this I was trying to make a multi-page form, using Spring's AbstractWizardFormController and spent an hour or so trying to make the form submission change the form page. My problem was that it wasn't immediately obvious how to use the form taglib with the Wizard architecture.
The docs mentioned using request parameters, which was a bit confusing as I was using a POST form submission, however all you actually need to do is add one of these parameters to the form data (usually using a button).
Finish and cancel actions can be triggered by request parameters, named PARAM_FINISH ("_finish") and PARAM_CANCEL ("_cancel"), ignoring parameter values to allow for HTML buttons. The target page for page changes can be specified by PARAM_TARGET, appending the page number to the parameter name (e.g. "_target1"). The action parameters are recognized when triggered by image buttons too (via "_finish.x", "_abort.x", or "_target1.x").
For example:
<input value="Submit" name="_target1" type="submit"/>This will request to change to page 2 of the form (remember Array base is 0).
Because of the way forms work, you can have a number of these (either as clickable images or buttons) and only the one the user eventually selects will be submitted with the request.
Labels: form, java, jsp, programming, spring framework

3 Comments:
Interesting... although I'm still having problems with the slow loading of JSP pages that use spring-forms taglib. Hope to fix it soon =/
The best way to implement a wizard form would be to use Spring Webflow, which you can mix and match with MVC. Of course, that may not satisfy your lesson objective but, given that Spring MVC (and Struts) are getting a bit long in the tooth, it might be more appropriate to teach a more advanced technology such as Webflow.
I've spent the last week or so looking into Webflow and it makes wizard implementation very simple.
Mmm, I remember looking at Webflow at the time, and it seemed to be a better engineered system for this kind of thing, however I was halfway through the project and didn't have the time to learn something new all over again.
The other gripe I had with it at the time was its unsightly mangling of URLs. Obviously this is only a question of taste, as end-users barely look at urls anyway.
Don't take my word for it though, my foray into the Webflow docs was very brief.
Post a Comment
Links to this post:
Create a Link
<< Home