A rudimentary page flow controller


In the last section you have seen how ActionMapping can be customized. Let us use the customized action mapping to build a rudimentary page flow controller. Every Action has to render the next view to the user after successful business logic invocation. This would mean that a standard mapping.findForward() in every method of yours. The rudimentary page flow controller eliminates this by providing this information in a declarative manner in struts-config.xml utilizing MyActionMapping . That information is used at runtime to automatically decide which page to forward to. The reason why the page flow controller is called rudimentary is because it has a serious limitation. If the page transitions are dynamic, then it cannot work. The controller serves as an example for customized action mapping usage. Some of the groundwork for the page flow controller is already done in Listing 10.2, in case you didn ‚ t know it already!). In particular pay attention to the two lines:

   <  set-property property="buttons"  value="nextButton,saveButton,cancelButton" />       <set-property property="forwards"                      value="page2,success,cancel" /> 

This first property ( buttons ) is a comma-separated name of all the buttons in the form. The second property ( forwards ) is a comma-separated name of the views rendered to the user when the corresponding buttons are selected. The view names refer to the forwards instead of the actual JSPs. Since the forwards is provided declaratively , the task of deciding the next view can be refactored into the base Action. This functionality has been added to the MybankBaseAction from Chapter 4. The code is shown in Listing 10.3 with the changes highlighted in bold.

Listing 10.3: The new and modified methods in MybankBaseAction
 public MybankBaseAction extends Action {   public ActionForward execute(ActionMapping mapping,             ActionForm form, HttpServletRequest request,             HttpServletResponse response) throws Exception {           ...     MybankBaseForm myForm = (MybankBaseForm) form;  MyActionMapping myMapping = (MyActionMapping) mapping;   String selectedButton =   getSelectedButton(myForm, myMapping);  preprocess(myMapping, myForm, request, response);  // Returns a null forward if the page controller is used.  ActionForward forward =          process(myMapping, myForm, request, response);   postprocess(myMapping, myForm, request, response);       ...  if (forward == null) { // For page controller only   String forwardStr = mapping.getForward(selectedButton);   forward = mapping.findForward(forwardStr);   }  return forward;   }  protected String getSelectedButton(MyActionForm form,   MyActionMapping mapping) {   String selectedButton = null;   String[] buttons = mapping.getButtons();   for (int i=0;i  <  buttons.length;i++) {   HtmlButton button = (HtmlButton)   PropertyUtils.getProperty(form, buttons[i]);   if (button.isSelected()) {   selectedButton = buttons[i];   break;   }   }   return selectedButton;   }  } 
 

First notice that the ActionMapping is cast to MyActionMapping . Also notice that the signature of the three abstract methods ‚ process() , preprocess() and postprocess() have been changed to accept MyActionMapping as the argument instead of ActionMapping . The page flow controller logic is implemented at the end of execute() method. The logic is simple: The code first checks which button has been selected. This is done in the getSelectedButton() method. It then retrieves the corresponding ActionForward and returns it. The RequestProcessor subsequently renders the view as usual. Since the code has been refactored into the base Action class, the child classes need not worry about mapping.findFoward() . They can simply return null. MybankBaseAction is now capable of automatically selecting the appropriate ActionForward.




Struts Survival Guide. Basics to Best Practices
Struts Survival Guide: Basics to Best Practices (J2ee Survival Series)
ISBN: 0974848808
EAN: 2147483647
Year: 2004
Pages: 96

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net