Ronald Daniel is a software developer, specializing in J2EE Programming technologies and frameworks. Has been in the IT World since 2008, still much to learn yet. Been a trainer for a while, specializing in training subjects: Java, J2EE, Sql-ing in Oracle, Sql Server and MySQL. Has been involved in numerous IT Software Development Projects and still wants to be better than yesterday. Feel free to drop email to rd.djunaedi@gmail.com Ronald is a DZone MVB and is not an employee of DZone and has posted 5 posts at DZone. You can read more from them at their website. View Full User Profile

Task Driven vs Event Driven on Struts 2 Action

06.01.2012
| 3185 views |
  • submit to reddit

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, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Daniel Slazer replied on Tue, 2012/06/12 - 12:13pm

You need to rewrite the whole file. Filesystems (at least the normal ones) don't support functionality that would allow you to just insert data in the beginning of the file.

Ronald Daniel replied on Mon, 2013/03/25 - 12:38pm in response to: Daniel Slazer

Hi Daniel, Many thanks for the input, but i have to be frank with you, i cant digest your thought, mind to share in more details?

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.