Task Driven vs Event Driven on Struts 2 Action
Join the DZone community and get the full member experience.Join For Free
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.
Published at DZone with permission of Ronald Daniel, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.