AbstractWizardFormController – AWFC


AWFC? – here is the link.

I have realized over time that Spring framework seriously lacks ‘quality’ documentation and competent faculty. This has in many cases lead the ‘decision makers’ to tag Spring as difficult (which i think otherwise about) and explore possibilities of using ‘easier’ frameworks like Struts. I know thats debatable.

Anyways. I will explain the problems that i faced when i hit upon AWFC for the first time. I had to go the source code to really understand and solve the problem.

Requirement:
Collect data from user in two pages. Display the data in uneditable mode in 3rd page which has 3 buttons.

btn1 – post. save the data in db. Remain on the same screen.

btn2 – pdf. Generate the pdf out of the collected data and show in a new window. Redirect the current page into another screen.

btn3 – go back to previous page.

Problem:

All problems i faced were with the last page which had three buttons.

1> After saving the data if i click on pdf button i get a npe saying that command object is null.

2> On click on pdf button i am able to open the new window but i don’t see my pdf there and my current page doesnt seem to be refreshing with new URL.

3> I have a confusion over how Spring decides on the next page to display?

Solution:

1> I was using AWFC.porcessFinish() method to process the save button click. This method cleans up the command object after processing. Thats the reason i get npe after this. processFinish() should only be called when you have decided that your wizard functionality is achieved. Ideally after this wizard you should not need to command object, but for any complicated requirements if you need it, then you should create separate object and store it in session.

For intermediate processing steps one should use the AWFC.postProcessPage() method.

2> You need to include the pdf page into the wizard so that command object is available to it. We need to take two actions on click of the pdf button. So the usual way of telling the next page to Spring will not work. ie

<input type=”submit” name=”_target3″>

We now need to take control of the submit, make two submits one after other setting the “_target”, “target=_blank/_self” and URL values properly. Putting the name=”_target?” in button will make your code complicated. I would say a hidden variable should be defined to set the “_target?” properly.

<input type=”button” value=”pdf” onClick=”document.pressed=’pdf’;pdf()”>

<script>
function pdf(){
if (document.pressed == “save”) {
document.getElementById(“targetParam”).name=”_target2″;
document.getElementById(“userForm”).submit();
} else if (document.pressed == “back”) {
document.getElementById(“targetParam”).name=”_target1″;
document.getElementById(“userForm”).submit();
} else if (document.pressed == “pdf”) {
document.getElementById(“userForm”).target=”_blank”;
document.getElementById(“targetParam”).name=”_target3″;
document.getElementById(“userForm”).submit();

document.getElementById(“userForm”).target=”_self”;
document.getElementById(“userForm”).action=”menu.htm”;
document.getElementById(“userForm”).submit();
}
}
</script>
The above code change made it work as expected. Learnings about AWFC from it are,

1> Use postProcessPage() for intermediate processing

2> Use processFinish() only for ending the wizard. If data is required after it, create another object and store it.

3> Use a hidden variable to set the ‘_target’ required by Spring to decide on the next page to display.

4> In general, AWFC does provide lot of automation for you, but also puts a few restrictions which may make it a difficult choice for many real world problems. A proper analysis should be done before choosing it for use in code.

2 comments

  1. Hello,
    Your comments and guidelines are very helpful regarding AWFC.

    I’m in the process of trying to use it, and I’m aware it suffers from serious lack of docs, and examples.

    I have a large jsp form that I’m trying to break down into “sections”, and based on the sparse docs, it appears like it would be a good choice. However I’ve been staring at the class wondering what are the next steps for some time now. At this point I’m seriously considering diving into the source code to try and obtain a little deeper understanding.

    Your comments helped.
    Thanks,
    Peter

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s