February 2005
Intermediate to advanced
528 pages
12h 53m
English
You want to use a single Action class to handle
related operations instead of having to write separate
Action classes for each operation.
Extend the
DispatchAction
with your own class. Provide methods for each operation that you wish
to be accessible as an Action. Each method should
have the same signature as the execute( ) method
except for the method name. The Action class shown
in Example 6-4 provides three related operations in
one class: create( ), update(), and delete( ).
Example 6-4. Dispatch action for related operations
package com.oreilly.strutsckbk.ch06; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.apache.struts.actions.DispatchAction; public class MyDispatchAction extends DispatchAction { public ActionForward create( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { // create data request.setAttribute("dispatchedTo","create"); return mapping.findForward("success"); } public ActionForward update( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { // update data request.setAttribute("dispatchedTo","update"); return mapping.findForward("success"); } public ActionForward delete( ActionMapping ...