The Address Editing Page

I l @ ve RuBoard

When the address page (see Listing 16.5) is brought up, it displays the fields for the address. One of the nice features of using the Struts html:text tag is that it automatically handles filling in previous values and even does null to empty string conversion. Struts will also do type conversion on the way out, turning a text field into an int , for example.

Listing 16.5 Address.jsp
 <%@ page language="java" %> <%@ taglib uri="/WEB-INF/struts-html.tld"         prefix="html" %> <%@ taglib uri="/WEB-INF/struts-bean.tld"         prefix="bean" %> <%@ include file="/jsp/cust/AutoLogin.jsp" %> <% {     Customer customer =     (Customer) pageContext.getAttribute("customer", PageContext.SESSION_SCOPE);     if (( customer == null)  (customer.getEmail() == null)) {      response.sendRedirect("Login.jsp");      return;     } } %> <html:html> <head> <title>   <bean:message key="myaddr.title"/> </title> <%@ include file="/jsp/includes/bfgheader.jsp" %> <h2 align="center"><bean:message key="myaddr.title"/></h2> <html:errors/> <html:form action="/Address"> <html:hidden property="addressID"/> <bean:message key="myaddr.field.firstname"/> <html:text property="firstName"/><BR> <bean:message key="myaddr.field.lastname"/>  <html:text property="lastName"/><BR> <bean:message key="myaddr.field.street1"/> <html:text property="street1"/><BR> <bean:message key="myaddr.field.street2"/> <html:text property="street2"/><BR> <bean:message key="myaddr.field.city"/> <html:text property="city"/> <bean:message key="myaddr.field.state"/> <html:text property="state"/> <bean:message key="myaddr.field.postal"/> <html:text property="postalCode"/><BR> <html:submit/> </html:form> </html:html> 

The html:errors tag lets you display the validation errors (which you'll define in the addressForm ActionForm ) in a JSP file.

If the customer is doing an edit, the addressForm will have already been created and validated with appropriate data in the accountAction , so this form will act as if the customer has already submitted the values and come back from validation.

The addressForm (see Listing 16.6) that holds the model for the address starts out with the properties for the address but then has one additional method.

Listing 16.6 addressForm.java
 package com.bfg.struts; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionError; import org.apache.struts.action.ActionErrors; import org.apache.struts.action.ActionMapping; public class addressForm extends ActionForm {     protected int addressID = -1;     protected String firstName;     protected String lastName;     protected String street1;     protected String street2;     protected String city;     protected String state;     protected String postalCode;     public int getAddressID() {      return addressID;     }     public void setAddressID(int id) {      addressID = id;     }     public String getFirstName() {      return firstName;     }     public void setFirstName(String name) {      firstName = name;     }     public String getLastName() {      return lastName;     }     public void setLastName(String name) {      lastName = name;     }     public String getStreet1 () {      return street1;     }     public void setStreet1(String street) {      street1 = street;     }     public String getStreet2 () {      return street2;     }     public void setStreet2(String street) {      street2 = street;     }     public String getCity () {      return city;     }     public void setCity(String c) {      city = c;     }     public String getState () {      return state;     }     public void setState(String st) {      state = st;     }     public String getPostalCode () {      return postalCode;     }     public void setPostalCode(String pc) {      postalCode = pc;     }     public ActionErrors validate(ActionMapping mapping,                                  HttpServletRequest request) {         ActionErrors errors = new ActionErrors();      if ((lastName == null)           (lastName.length() == 0)) {             errors.add("lastName",                        new ActionError("error.lastname.required"));      }      if ((firstName == null)           (firstName.length() == 0)) {             errors.add("firstName",                        new ActionError("error.firstname.required"));      }      if ((street1 == null)           (street1.length() == 0)) {             errors.add("street1",                        new ActionError("error.street1.required"));      }      if ((city == null)           (city.length() == 0)) {             errors.add("city",                        new ActionError("error.city.required"));      }      if ((state == null)           (state.length() == 0)) {             errors.add("state",                        new ActionError("error.state.required"));      }      if ((postalCode == null)           (postalCode.length() == 0)) {             errors.add("postalCode",                        new ActionError("error.postal.required"));      }      return errors;     } } 

The validate method is called between when the form is submitted and when the Action object is invoked. If the validation fails (because of one or more errors being returned), the Action never even sees the form submission. This is another way that Struts isolates the business logic from form handling. ActionError instantiation automatically looks up the string against the ApplicationResource , meaning that all error messages can be internationalized.

If the validation succeeds, the controller hands off to the business logic, as implemented in the Action (see Listing 16.7). Because the addressForm initializes the ID to -1 , the code can tell whether it's creating or editing a record. It either creates a new Address object by copying the properties out of the form into the object and then calls createAddress , or it finds the old address in the address book, copies the properties, and calls updateAddress . In either case, flow returns to the myAccount page afterward.

Listing 16.7 addressAction.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 java.text.NumberFormat; import java.io.*; public class addressAction extends Action {     public ActionForward perform(ActionMapping mapping,                      ActionForm form,                      HttpServletRequest request,                      HttpServletResponse response)      throws IOException, ServletException {      addressForm af = (addressForm) form;      Customer cust = ((Customer)request.getSession().getAttribute("customer"));      try {          if (af.getAddressID() == -1) {           Address addr = new Address();           PropertyUtils.copyProperties(addr, af);           addr.createAddress();           cust.addAddress(addr);           return mapping.findForward("myaccount");          }  else {           Address addr = Address.findAddress(af.getAddressID());           PropertyUtils.copyProperties(addr, af);           addr.updateAddress();           cust.getAddressBook().put(new Integer(addr.getAddressID()),                            addr);           return mapping.findForward("myaccount");          }      }  catch (Exception ex) {          ex.printStackTrace();      }      return mapping.findForward("address");     } } 
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