View Javadoc

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