The Action

I l @ ve RuBoard

The Action

Because there's no validation, control always is handed over to the accountAction class, shown in Listing 16.4.

Listing 16.4 accountAction.java
 package com.bfg.struts; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.ServletException; import org.apache.struts.action.ActionForm; import org.apache.struts.action.Action; import org.apache.struts.action.ActionMapping; import org.apache.struts.action.ActionForward; import org.apache.struts.util.PropertyUtils; import com.bfg.customer.Address; import com.bfg.customer.Customer; import com.bfg.customer.CreditCard; import java.io.*; public class accountAction extends Action {     public ActionForward perform(ActionMapping mapping,                                ActionForm form,                                HttpServletRequest request,                                HttpServletResponse response)      throws IOException, ServletException {      accountForm af = (accountForm) form;      Customer cust = ((Customer)request.getSession().getAttribute("customer"));      if (af.getAction().equals("newaddr")) {          request.setAttribute("addressForm", new addressForm());          return mapping.findForward("address");      }      if (af.getAction().equals("newcard")) {          request.setAttribute("creditCardForm", new creditcardForm());          return mapping.findForward("creditcard");      }      if (af.getAction().equals("editaddr")) {          try {              Address addr =                  (Address) cust.getAddressBook().get(new Integer(af.getAddressIndex()));              if (addr == null) {                  return mapping.findForward("myaccount");              }              addressForm ad_form = new addressForm();              PropertyUtils.copyProperties(ad_form, addr);              request.setAttribute("addressForm", ad_form);              return mapping.findForward("address");          } catch (Exception ex) {              ex.printStackTrace();          }      }      if (af.getAction().equals("editcard")) {          try {              CreditCard card =                  (CreditCard) cust.getWallet().get(new Integer(af.get CardIndex()));              if (card == null) {                  return mapping.findForward("myaccount");              }              creditcardForm cc_form = new creditcardForm();              PropertyUtils.copyProperties(cc_form, card);              request.setAttribute("creditcardForm", cc_form);              return mapping.findForward("creditcard");          } catch (Exception ex) {              ex.printStackTrace();          }      }      if (af.getAction().equals("deleteaddr")) {          try {              Address addr =                  (Address) cust.getAddressBook().get(new Integer(af.getAddressIndex()));           if (addr == null) {               return mapping.findForward("myaccount");           }           cust.getAddressBook().remove(new Integer(addr.getAddressID()));           cust.deleteAddress(addr);           addr.deleteAddress();           return mapping.findForward("myaccount");       } catch (Exception ex) {           ex.printStackTrace();       }      }      if (af.getAction().equals("deletecard")) {          try {           CreditCard card =               (CreditCard) cust.getWallet().get(new Integer(af.get CardIndex()));           if (card == null) {               return mapping.findForward("myaccount");           }           cust.getWallet().remove(new Integer(card.getCardID()));           card.deleteCreditCard();           return mapping.findForward("myaccount");          }  catch (Exception ex) {           ex.printStackTrace();          }      }      return mapping.findForward("myaccount");     } } 

You first cast the ActionForm argument into the appropriate subclass so that you can access the properties. An Action must accept a generic ActionForm as an argument to match the calling signature from Struts, but you need to get the properties specific to the accountForm , so you need to cast it.

If the action requested was a new address, create a new addressForm object (which has placeholders for all the address properties) and return a forwarding request to go to the address entry form. You do the same thing for a new credit card request. You really don't need to create a new blank form because this will be done automatically by Struts; the ActionForm s are associated with the JSP pages by the structs-conf.xml file.

If the user requested to edit an existing card, the code needs to get the address out of the address book. If it isn't found (somehow the user requested one that he doesn't own), you just have Struts throw him back to the myAccount page. Otherwise, the method creates a new addressForm and uses the copyProperties method to move all the properties from the address object to the model held in the addressForm bean. (Luckily, you were careful to use the same property names throughout.) Then the method returns a forwarding request to the address edit form. Again, the same logic holds for the credit card editing code. If the method needs to delete an address or credit card, it does so and returns to the myAccount page.

I l @ ve RuBoard


MySQL and JSP Web Applications. Data-Driven Programming Using Tomcat and MySQL
MySQL and JSP Web Applications: Data-Driven Programming Using Tomcat and MySQL
ISBN: 0672323095
EAN: 2147483647
Year: 2002
Pages: 203
Authors: James Turner

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