Task Driven vs Event Driven on Struts 2 Action
I had a job to design and develop a new framework for my own development team. We made the decision that we will adopt the Struts 2 as my MVC framework.
Yes i see the benefit from Struts 2 very well, easy to implement and has so many features. Until, my team member said “Hey, i hate Struts very much, Because i have to put many exception-handling and/or debugger inside many events/methods. Can we use another framework? or just use the servlet mixed with the scriptlet?”
Well i know his concern, it would be easy to read and maintain the code with their exception-handling when your code was still small, but along with the time, you will have your own trouble to maintain the code to stay in a good shape, believe me, i have had this kind of experience before.
So, i think and try to find a better solution and lucky for me, i found the much better solution with the Task-Driven approach. Maybe for some of you this approach will look like a tricky, but no problem as long as my team could gain a benefit from it and keep the code stay in healthy shape .
Well, as a brief introduction for Struts 2. This Action code,perhaps, is the common way that almost every single Struts 2 developer will do. As you can see, you can have many methods that indicates as an “EVENT”
As you can see, i have the events/methods execute(), save(), fetch(), and delete().
and you can see on the above code, that you have to do the exception-handling four times, one each method. For me — and team — one is more than enough, easy to read and maintain the code, especially when exception occur.
Now, my approach is to convert the familiarity of the event-driven into the task-driven. So, i just have to add a new parameter that i gave name task, as a flagging parameter. In here i will have only 4 tasks to subtitute the 4 methods, “execute“, “save“, “fetch” and “delete“.
Implementing the code is easy, just put one extra attribute on the Action code, i named it task.
And on the JSP, only add a minor change, and i will give you the comparison between the event-driven and task-driven.
As you can see the difference between those 2 slices of code. I dont know how about you, but i love the task-driven Struts more than the event-driven. Maybe it’s relative, the answer could be different for another developer, but the most important for me is now my developers are happier to use Struts.
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)