Categories
Development

Struts and multipage form validation

Blah, multipage forms suck. 😉 Alright, I’m not making it a lot easier for myself by insisting on using DispatchActions, but hell, it should support it.

Problem 1 – One action mapping can only have one input. This doesn’t matter when everything looks fine, but suddenly you get a validation error, and Struts tries to redirect to the first inputpage, even though the validation error occurred on page 2. With two action mappings this would of course be solved.

Problem 2 – Whenever validation fails it will redirect back to the input page. This is without touching the Action. This means that any setup you do, and things you put in the request before the page is displayed is gone. Big problem with dropdown select boxes. After failed vaildation the collection used to populate it in the request is gone and the tags fail with an Exception screaming about not finding it.

Solution – Turn off validation in the mapping and call form.validate() explicitly at the beginning of your next action step. If the validation returns errors, setup the necessary content in the request, and then forward to the page again. Not too nice. I’d rather have the Action implement a setupForm method or something.


// Validate input from previous form
ActionMessages ae = form.validate(mapping, req);
if (!ae.isEmpty()) {
this.saveErrors(req, ae);
setupStep1(req);
return mapping.getInputForward();
}

Maybe there’s some features I’m overlooking, but I’ve tried to find any info. There are lots of old posts on the mailinglists about especially problem 2, but the solution I found was to turn off validation. This seems to be an old problem, and one I have encountered several times, so I’m a bit surprised it hasn’t been incorporated into Struts yet. Or maybe it is?

Drop me a line if you have got some more info.

Update: Seems Rick Reumann has encountered the same problem here. He reaches pretty much the same conclusion as me. I’m not sure I like the solution yet, but one thing it lead me to was to structure my code better by separating out the setup code for the form in each action method.

Categories
Development

EJB Unit testing tool

We’re going to do unit tests on some parts at work. This should make it easier. I’ll have to check it out later.

Via java.no.

Categories
Development

Struts and multipage forms

At work I haven been pounding out Struts based pages lately. We are building a registration system basically, with some intrisic rules for submitting data and checking with external systems. From my work with Struts at UKA I knew multipage forms were a common problem with Struts. Henning hacked his way around it some way, but I didn’t have to figure it out then.

Struts 1.2 is in the pipeline. It’s running at version 1.2.1 Beta and features several improvements, including support for multipage forms.

The biggest problem with multipage forms were actually the validation. You couldn’t validate the form partially for the first page, and the rest on the second page. This has been solved through the page=”” attribute on the field element in validation.xml. When validation is triggered it will validate the data for the current page AND all pages with a lower number. So this means that if data was validated on page one, it will be validated again together with the data from page two. This also means that if you have implemented your form reset method correctly you will need to include the fields from the previous forms as hidden fields in the current page.

The way Struts keeps track of which page you are on is through the page form attribute. This is just a normal form-field, but its name is required to be page. They should probably have chosen a less generic name for it like struts.form.page or something, but I guess it works fine. It is normally implemented through html:hidden.