Wednesday, March 12, 2008

In Struts, one page means one action

In our application, we have this one page where people can edit content. This page has links to edit other aspects as well. When you navigate to another page, we save the content before navigating. It makes sense – forcing people to save just to edit another aspect of the content sucks.

Where this comes tricky is error handling. We use Struts. And for this editing page, we have several simple Actions that back all this interaction. One action to save, one to load the content editing page, others to handle the other operations. We connect these actions through redirects, which seems like a freaking bad idea to me. Because when an error happens, you can no longer redirect, because we want to add all kinds of error messages to response object. And if you redirect, poof, no more errors!

So I have a rule, each page is backed by a single action. Any operation rolls through that action. This allows you to simply call one of the other functions rather than configure a redirect mechanism. Your API is cleaner, the logic flow can be read in a single file, and hey, if you have errors, you can report them to the same location!

No comments: