Now on Struts

Well I’ve spent the last couple of days learning Struts, and I found the best way to solidify my understanding: re-write this site using it!

So we’re up and running again, with an even better MVC framework.

What are my thoughts so far on Struts?

Pros

  • It’s much more pleasing to have page-flows that are all connected somehow, to be tied up into an action. I also like the idea that I can re-use actions. For example, I have a login action. So far I only have that in one place, but if I needed to I could simply drop it in as a forward from another admin action, say. It’s nice to be able to see all my page-flows in an xml configuration file, rather than littered throughout lines of code.
  • The tag libraries are very useful. I’ve noticed that Struts automatically re-writes URLs with the jsessionid when it needs to. That’s one big chore taken care of. Also my JSPs look even tidier now, with logic:iterate tags - I can access collections without having to cast, get Iterators etc. Previously, I was putting an Iterator into the pageContext. And that meant casting/while loops etc. I’d like to use JSTL, I really would, but this server is not up to JSP 2.0 and even with the jar files I don’t think I’d get away with it.
  • ApplicationResources.properties - now all my error messages and labels are in a configuration file, rather than tied up in code.
  • More generally, I really like the fact that you get more of a birds-eye view of your application. I can step back a bit from lines of code and think in terms of Actions, Use-Cases really.

Cons

  • It takes a bit more thought to get going, you can’t just dive right in and knock up page-flows. You’re forced to think more about overall design. Hang on … isn’t that a pro?!
  • Maybe it’s just me, but it seems some Actions are troublesome to figure out. What if I have an Action which builds some data, then returns a forward to the input page. This page has a html form, tied up with the same action, which you want to validate. Now here’s the problem - if you turn validation on for that action, the Action will fail first time round building the data for that page because the form fields are blank! I tackle this very problem on this site when you view an article along with its comments form. The only way I could get around it was to condition my validate() method on HTTP POST only. So when you originally request the page the data gets built and you see the form. Then you hit submit and the validation happens. This works, but it feels like a workaround. My question is … why does Struts call validate() in your ActionForm on a GET? I can think of no circumstances where that would be useful, unless you’re not using the method for its intended purpose. There’s more on this here.
  • Not really a downside, but I had a big surprise when I used bean:write to write out my blog entries, which include html - the tag cleaned up all the tags so they appeared like this: etc. when the page was loaded! i hadn’t tested posts containing html before uploading my changes so imagine my surprise when I loaded benrowland.net for the first time on Struts and all the links in my articles weren’t parsed! Of course the answer is to use filter=“false” on bean:write tag.

Overall though, I’m happy with Struts. My site feels more maintainable, AND I get to have .br on my extension mappings in the address bar!