This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Hello, I have a question about ActionForm. In struts-config.xml, I define my action mapping as following:
the page flow will go through init first, initiate a form instance, save it in session scope, go to InitFormAction, then forward to edit. Then..my question arised: May I change the form scope to request in edit? If I can, does it mean no form instance will be saved in session after summary.jsp?
This type of "action chaining" is generally frowned upon. I don't know the details but I have read comments from Struts developers stating that it was never designed to handle two action on the same request. What type of processing are you doing in your init action? I would be tempted to move this into a utility class than can be shared between actions and called directly in your execute method.
I too had gone through some articles that are against Action Chaining. But there scenarios in which we need to chain actions to complete a task. Let us consider a page where we create case.Once we submit we update the DB after applying business rules, we come back to a view which shows all the cases you had submitted. We can also get to this view menu from menus. This view is developed as common functionaity and as an View Action, and this goes against the db to build the view. No if i submit the case i call submit action and this updates the db, now i call the View Action to Load the views. So this kind of flow needs action chaining. I dont know is there any other better option to do it? People who are say action chaining is not good could tell the way to implement this.
Below is a quote from the Struts Newbie FAQ written by the Struts authors that addresses this topic:
As a rule, chaining Actions is not recommended. If your business classes are properly factored, you should be able to call whatever methods you need from any Action, without splicing them together into a cybernetic Rube Goldberg device.
For those not familiar with it, a Rube Goldberg device is clever and complicated machine that accomplishes no practical work.
I believe the important point here is elegance of code structure. The above passage is just pointing out that yes, you can chain actions, but it's not a very elegant solution.
An Action class belongs in the controller space of Model/View/Controller architecture, and as such it shouldn't contain business logic, but rather commmunicate with other objects that do contain business logic. If you find that you must use action chaining in order to make a process work, it's because you've broken this rule and put business logic in your action class.
My answer to Arulanand's challenge is that the logic to prepare for the view of a specific JSP should be placed in a Business Delegate class, rather than an Action class. Then whatever action needs to display that particular JSP should call the Business Delegate method and then forward to the JSP rather than chaining to a view action.
I do the type of thing that you (Arulanand) describe all the time. I call the SaveXAction and the success forward for that action redirects to the DisplayXListAction. This does not fall into the "action chaining" category because the forward uses redirect="true". Action Chaining is an issue when you string together multiple action calls on the same request. There is a common pattern named something like "redirect after post" that describes the first scenario.