View Javadoc

    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *      http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    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 }