KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > blandware > atleap > webapp > form > BaseForm


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

28 public class BaseForm extends ValidatorForm implements Serializable JavaDoc {
29
30     /**
31      * Transient log instance for all forms
32      */

33     protected transient final Log log = LogFactory.getLog(getClass());
34
35     //~ Methods ================================================================
36

37     public String JavaDoc toString() {
38         return ToStringBuilder.reflectionToString(this,
39                 ToStringStyle.MULTI_LINE_STYLE);
40     }
41
42     public boolean equals(Object JavaDoc o) {
43         return EqualsBuilder.reflectionEquals(this, o);
44     }
45
46     public int hashCode() {
47         return HashCodeBuilder.reflectionHashCode(this);
48     }
49
50     /**
51      * This validation method is designed to be a parent of all other Form's
52      * validate methods - this allows the cancel and delete buttons to bypass
53      * validation.
54      *
55      * @param mapping The <code>ActionMapping</code> used to select this
56      * instance
57      * @param request The servlet request we are processing
58      * @return <code>ActionErrors</code> object that encapsulates any
59      * validation errors
60      */

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