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