KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibatis > struts > BaseBean


1 package com.ibatis.struts;
2
3 import org.apache.struts.action.ActionForm;
4 import org.apache.struts.action.ActionMapping;
5 import org.apache.struts.action.ActionErrors;
6 import org.apache.struts.action.ActionError;
7
8 import javax.servlet.ServletRequest JavaDoc;
9 import javax.servlet.http.HttpServletRequest JavaDoc;
10 import java.util.List JavaDoc;
11 import java.util.Map JavaDoc;
12
13 /**
14  * All actions mapped through the BeanAction class should be mapped
15  * to a subclass of BaseBean (or have no form bean mapping at all).
16  * <p/>
17  * The BaseBean class simplifies the validate() and reset() methods
18  * by allowing them to be managed without Struts dependencies. Quite
19  * simply, subclasses can override the parameterless validate()
20  * and reset() methods and set errors and messages using the ActionContext
21  * class.
22  * <p/>
23  * <i>Note: Full error, message and internationalization support is not complete.</i>
24  * <p/>
25  * Date: Mar 12, 2004 9:20:39 PM
26  *
27  * @author Clinton Begin
28  */

29 public abstract class BaseBean extends ActionForm {
30
31   public void reset(ActionMapping mapping, ServletRequest JavaDoc request) {
32     ActionContext.initialize((HttpServletRequest JavaDoc)request, null);
33     reset();
34   }
35
36   public void reset(ActionMapping mapping, HttpServletRequest JavaDoc request) {
37     ActionContext.initialize((HttpServletRequest JavaDoc)request, null);
38     reset();
39   }
40
41   public ActionErrors validate(ActionMapping mapping, ServletRequest JavaDoc request) {
42     ActionContext.initialize((HttpServletRequest JavaDoc)request, null);
43     ActionContext ctx = ActionContext.getActionContext();
44     Map JavaDoc requestMap = ctx.getRequestMap();
45
46     List JavaDoc errorList = null;
47     requestMap.put("errors",errorList);
48     validate();
49     errorList = (List JavaDoc) requestMap.get("errors");
50     ActionErrors actionErrors = null;
51     if (errorList != null && !errorList.isEmpty()) {
52       actionErrors = new ActionErrors();
53       actionErrors.add(ActionErrors.GLOBAL_ERROR, new ActionError("global.error"));
54     }
55     return actionErrors;
56   }
57
58   public ActionErrors validate(ActionMapping mapping, HttpServletRequest JavaDoc request) {
59     ActionContext.initialize(request, null);
60     ActionContext ctx = ActionContext.getActionContext();
61     Map JavaDoc requestMap = ctx.getRequestMap();
62
63     List JavaDoc errorList = null;
64     requestMap.put("errors",errorList);
65     validate();
66     errorList = (List JavaDoc) requestMap.get("errors");
67     ActionErrors actionErrors = null;
68     if (errorList != null && !errorList.isEmpty()) {
69       actionErrors = new ActionErrors();
70       actionErrors.add(ActionErrors.GLOBAL_ERROR, new ActionError("global.error"));
71     }
72     return actionErrors;
73   }
74
75   public void validate() {
76   }
77
78   public void reset() {
79   }
80
81   public void clear() {
82   }
83
84   protected void validateRequiredField(String JavaDoc value, String JavaDoc errorMessage) {
85     if (value == null || value.trim().length() < 1) {
86       ActionContext.getActionContext().addSimpleError(errorMessage);
87     }
88   }
89
90 }
91
Popular Tags