1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.ibatis.struts;
18
19 import org.apache.struts.action.ActionForm;
20 import org.apache.struts.action.ActionMapping;
21 import org.apache.struts.action.ActionErrors;
22 import org.apache.struts.action.ActionError;
23
24 import javax.servlet.ServletRequest;
25 import javax.servlet.http.HttpServletRequest;
26 import java.util.List;
27 import java.util.Map;
28
29 /***
30 * All actions mapped through the BeanAction class should be mapped
31 * to a subclass of BaseBean (or have no form bean mapping at all).
32 * <p/>
33 * The BaseBean class simplifies the validate() and reset() methods
34 * by allowing them to be managed without Struts dependencies. Quite
35 * simply, subclasses can override the parameterless validate()
36 * and reset() methods and set errors and messages using the ActionContext
37 * class.
38 * <p/>
39 * <i>Note: Full error, message and internationalization support is not complete.</i>
40 * <p/>
41 * Date: Mar 12, 2004 9:20:39 PM
42 *
43 * @author Clinton Begin
44 */
45 public abstract class BaseBean extends ActionForm {
46
47 public void reset(ActionMapping mapping, ServletRequest request) {
48 ActionContext.initialize((HttpServletRequest)request, null);
49 reset();
50 }
51
52 public void reset(ActionMapping mapping, HttpServletRequest request) {
53 ActionContext.initialize((HttpServletRequest)request, null);
54 reset();
55 }
56
57 public ActionErrors validate(ActionMapping mapping, ServletRequest request) {
58 ActionContext.initialize((HttpServletRequest)request, null);
59 ActionContext ctx = ActionContext.getActionContext();
60 Map requestMap = ctx.getRequestMap();
61
62 List errorList = null;
63 requestMap.put("errors",errorList);
64 validate();
65 errorList = (List) requestMap.get("errors");
66 ActionErrors actionErrors = null;
67 if (errorList != null && !errorList.isEmpty()) {
68 actionErrors = new ActionErrors();
69 actionErrors.add(ActionErrors.GLOBAL_ERROR, new ActionError("global.error"));
70 }
71 return actionErrors;
72 }
73
74 public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
75 ActionContext.initialize(request, null);
76 ActionContext ctx = ActionContext.getActionContext();
77 Map requestMap = ctx.getRequestMap();
78
79 List errorList = null;
80 requestMap.put("errors",errorList);
81 validate();
82 errorList = (List) requestMap.get("errors");
83 ActionErrors actionErrors = null;
84 if (errorList != null && !errorList.isEmpty()) {
85 actionErrors = new ActionErrors();
86 actionErrors.add(ActionErrors.GLOBAL_ERROR, new ActionError("global.error"));
87 }
88 return actionErrors;
89 }
90
91 public void validate() {
92 }
93
94 public void reset() {
95 }
96
97 public void clear() {
98 }
99
100 protected void validateRequiredField(String value, String errorMessage) {
101 if (value == null || value.trim().length() < 1) {
102 ActionContext.getActionContext().addSimpleError(errorMessage);
103 }
104 }
105
106 }