KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > appfuse > webapp > form > BaseForm


1 package org.appfuse.webapp.form;
2
3 import java.io.Serializable JavaDoc;
4
5 import javax.servlet.http.HttpServletRequest JavaDoc;
6
7 import org.apache.commons.lang.builder.EqualsBuilder;
8 import org.apache.commons.lang.builder.HashCodeBuilder;
9 import org.apache.commons.lang.builder.ToStringBuilder;
10 import org.apache.commons.lang.builder.ToStringStyle;
11 import org.apache.struts.Globals;
12 import org.apache.struts.action.ActionErrors;
13 import org.apache.struts.action.ActionMapping;
14 import org.apache.struts.util.MessageResources;
15 import org.apache.struts.validator.ValidatorForm;
16
17
18 /**
19  * Base ActionForm bean. Used to give child classes readable
20  * representation of their properties using toString() method.</p>
21  *
22  * <p>Also has a validate() method to cancel validation on cancel actions.</p>
23  *
24  * <p><a HREF="BaseForm.java.htm"><i>View Source</i></a></p>
25  *
26  * @author <a HREF="mailto:matt@raibledesigns.com">Matt Raible</a>
27  * @version $Revision: 1.7 $ $Date: 2005-04-11 15:09:49 -0600 (Mon, 11 Apr 2005) $
28  */

29 public class BaseForm extends ValidatorForm implements Serializable JavaDoc {
30     private static final long serialVersionUID = 3257005453799404851L;
31
32     public String JavaDoc toString() {
33         return ToStringBuilder.reflectionToString(this,
34                 ToStringStyle.MULTI_LINE_STYLE);
35     }
36
37     public boolean equals(Object JavaDoc o) {
38         return EqualsBuilder.reflectionEquals(this, o);
39     }
40
41     public int hashCode() {
42         return HashCodeBuilder.reflectionHashCode(this);
43     }
44
45     /**
46      * This validation method is designed to be a parent of all other Form's
47      * validate methods - this allows the cancel and delete buttons to bypass
48      * validation.
49      *
50      * @param mapping The <code>ActionMapping</code> used to select this
51      * instance
52      * @param request The servlet request we are processing
53      * @return <code>ActionErrors</code> object that encapsulates any
54      * validation errors
55      */

56     public ActionErrors validate(ActionMapping mapping,
57                                  HttpServletRequest JavaDoc request) {
58         // Identify the request parameter containing the method name
59
String JavaDoc parameter = mapping.getParameter();
60
61         if (parameter != null) {
62             // Identify the method name to be dispatched to.
63
String JavaDoc method = request.getParameter(parameter);
64             MessageResources resources =
65                 (MessageResources) request.getAttribute(Globals.MESSAGES_KEY);
66
67             // Identify the localized message for the cancel button
68
String JavaDoc cancel = resources.getMessage("button.cancel");
69             String JavaDoc delete = resources.getMessage("button.delete");
70
71             // if message resource matches the cancel button then no
72
// need to validate
73
if ((method != null) &&
74                     (method.equalsIgnoreCase(cancel) ||
75                     method.equalsIgnoreCase(delete))) {
76                 return null;
77             }
78         }
79
80         // perform regular validation
81
return super.validate(mapping, request);
82     }
83 }
84
Popular Tags