views:

249

answers:

2

I am using a MVC framework which is a bit like struts. So, say I have a "Edit Store" link in my application this is how the url would look like:

http://localhost:9080/test?action=editStore&storeNum=10

Now, action determines my Action (analogous to Struts Action) to be run. The corresponding action here is: EditStoreAction. Clicking this would open a pop up with different attributes of a store for edit.

My question here is: How do I write my actions? Do I write two actions in this context?

  1. EditStoreAction which will render edit.jsp
  2. StoreSaveAction which will invoked when user presses accept on the rendered response of edit.jsp.

OR Do I just write one action? EditStoreAction and submit form to the same action, I would know that the user has pressed the accept button for changes on submission. So, I would execute a different flow in the Action, which would save updates to database and redirect to a diff page.

What is the best pratice here? Create as many actions as possible coz it keeps the code modular? OR just write one action to handle everthing in a jsp?

I know this question sounds a bit trivial, however, sometimes you just want to get everything right. Hence, the question. Appreciate your help.

+1  A: 

I like to use struts DispatchAction class because I could define more than one method in the action class (the "execute") method. Behind the hood, all it does is to find the method that it has to execute (submitted in the form or passed in the URL), find the method using reflection, invoke it with the right set of arguments (the method must have the same signature of the "execute" method), gets it result and pass it along. The DispatchAction simply overrides the "execute" method of the Action class to implement that behavior.

That being said, in your case, I would define only one class - let's say "DispatchStoreAction", and I would define two methods, probably "prepare" and "save". I like doing it that way because I still have a good class abstraction (and you don't put the "action" you're executing in the class name), because your methods can clearly identify what they are supposed to do, and also because, by definition, action classes tend to be small. You will probably have a "StoreLogic" or "StoreBusiness" defined somewhere, and this class will handle the business logic related to the entity that you're working on. I personally think that it's nice if you have one "StoreAction" and then one "StoreLogic", one "UserAction" and one "UserLogic" and so on - the relationship doesn't need to be 1 to 1 but I think it helps maintaining the code.

Check the source code of the DispatchAction class for ideas on how to do this, but the implementation should be trivial.

Ravi Wallau
+2  A: 

The idea is, similar to Spring MVC, to map your actions to the methods of a specific class, say it controller.

So, in your case, these two actions will be mapped on two different methods of the same class. You can call the class StoreFormController and two methods, editStore() and saveStore().

Better still if you make two controllers for each entity. May be one for all GET requests and another is for POST requests. So, in your case there would be two controllers StoreController for all other requests and StoreFormController for all form submissions, namely post requests. Now, your first action being GET will go to editStore() method of StoreController, whereas the second being POST request will go to saveStore() method of StoreFormController. You can define as many methods as needed in any of these two classes based on the request type.

You can easily see, where I am coming from, if you know Spring MVC API.

Adeel Ansari