Reviewing the Controller Layer of the Mini HR Application


The ActionForward Class

The ActionForward class encapsulates a forward. Forwards were introduced in Chapter 2, but this section takes a closer look at them because they are used by the ActionForward class.

Struts provides the forward as an alternative to hard-coding URLs inside your application. Forwards allow you to define logical names for URLs and then to use the names to reference the URLs. If you reference a URL by its logical name instead of referencing it directly, when the URL changes, you don't have to update each reference to the URL. For example, with forwards you can define a forward for a search page with a logical name of "search" that points to the /search.jsp page. Instead of hard-coding the search page's /search.jsp URL throughout your application, you use the forward's logical name. If the location of the search page changes, you need to make only one change to the forward definition and all places in the application that point to that forward will receive the change. Essentially, forwards are URL aliases. They abstract URLs and shield you from changes. The application knows only the alias. Where the alias points does not matter.

Forwards are defined declaratively in the Struts configuration file. There are two types of forwards that can be defined, a global forward and an action-specific forward. Global forwards are available throughout an application, whereas action-specific forwards are available only to their respective action. Following is an example of how to define a global forward in the Struts configuration file:

<global-forwards>   <forward name="searchPage" path="/search.jsp"/> </global-forwards>

Action-specific forwards are defined by nesting the forward tag inside an action tag, as shown next:

<action-mappings>   <action path="/updateUser"           type="com.jamesholmes.minihr.UpdateUserAction">     <forward name="success" path="/updateSuccess.jsp"/>   </action> </action-mappings>

Struts' ActionForward class encapsulates a forward inside an application. For example, the Action class's execute( ) method has a return type of ActionForward. After an action executes, it must return an ActionForward or null (to indicate processing is complete). ActionForwards returned from actions are used to forward to the View layer. The following snippet illustrates how the ActionForward class is used in an action:

package com.jamesholmes.minihr;     import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;     import org.apache.struts.action.Action; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping;     public class UpdateUserAction extends Action {   public ActionForward execute(ActionMapping mapping,     ActionForm form,     HttpServletRequest request,     HttpServletResponse response)     throws Exception   {     // Perform action processing.         return new ActionForward("updateSuccess");   } }

The value passed to the ActionForward class's constructor corresponds to the logical name of a forward defined in the Struts configuration file.



Struts. The Complete Reference
Struts: The Complete Reference, 2nd Edition
ISBN: 0072263865
EAN: 2147483647
Year: 2004
Pages: 165
Authors: James Holmes

Similar book on Amazon
Struts 2 in Action
Struts 2 in Action
Murach's Java Servlets and JSP, 2nd Edition
Murach's Java Servlets and JSP, 2nd Edition
Jakarta Struts For Dummies
Jakarta Struts For Dummies
Head First Servlets and JSP: Passing the Sun Certified Web Component Developer Exam
Head First Servlets and JSP: Passing the Sun Certified Web Component Developer Exam

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