package com.ibatis.struts;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionError;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
* All actions mapped through the BeanAction class should be mapped
* to a subclass of BaseBean (or have no form bean mapping at all).
*
* The BaseBean class simplifies the validate() and reset() methods
* by allowing them to be managed without Struts dependencies. Quite
* simply, subclasses can override the parameterless validate()
* and reset() methods and set errors and messages using the ActionContext
* class.
*
* Note:  Full error, message and internationalization support is not complete.
*
* Date: Mar 12, 2004 9:20:39 PM
*
* @author Clinton Begin
*/
public abstract class BaseBean extends ActionForm {

  public void reset(ActionMapping mapping, ServletRequest request) {
    ActionContext.initialize((HttpServletRequest)request, null);
    reset();
  }

  public void reset(ActionMapping mapping, HttpServletRequest request) {
    ActionContext.initialize((HttpServletRequest)request, null);
    reset();
  }

  public ActionErrors validate(ActionMapping mapping, ServletRequest request) {
    ActionContext.initialize((HttpServletRequest)request, null);
    ActionContext ctx = ActionContext.getActionContext();
    Map requestMap = ctx.getRequestMap();

    List errorList = null;
    requestMap.put("errors",errorList);
    validate();
    errorList = (List) requestMap.get("errors");
    ActionErrors actionErrors = null;
    if (errorList != null && !errorList.isEmpty()) {
      actionErrors = new ActionErrors();
      actionErrors.add(ActionErrors.GLOBAL_ERROR, new ActionError("global.error"));
    }
    return actionErrors;
  }

  public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
    ActionContext.initialize(request, null);
    ActionContext ctx = ActionContext.getActionContext();
    Map requestMap = ctx.getRequestMap();

    List errorList = null;
    requestMap.put("errors",errorList);
    validate();
    errorList = (List) requestMap.get("errors");
    ActionErrors actionErrors = null;
    if (errorList != null && !errorList.isEmpty()) {
      actionErrors = new ActionErrors();
      actionErrors.add(ActionErrors.GLOBAL_ERROR, new ActionError("global.error"));
    }
    return actionErrors;
  }

  public void validate() {
  }

  public void reset() {
  }

  public void clear() {
  }

  protected void validateRequiredField(String value, String errorMessage) {
    if (value == null || value.trim().length() < 1) {
      ActionContext.getActionContext().addSimpleError(errorMessage);
    }
  }

}
번호 제목 글쓴이 날짜 조회 수
31 샘플예제 file 황제낙엽 2009.03.02 445
30 A Walking Tour of the Struts 2 MailReader Application (2) 황제낙엽 2008.12.10 314
29 A Walking Tour of the Struts 2 MailReader Application (1) 황제낙엽 2008.12.10 458
28 스트러츠2 샘플 어플리케이션 (MailReader Demonstration Application) file 황제낙엽 2008.11.20 904
27 스트러츠2 실습 예제 모음 (Struts Showcase) 황제낙엽 2008.11.20 58252
26 Struts와 Struts2의 로그인 매커니즘 비교 황제낙엽 2008.10.23 319
25 Struts2 시작하기 황제낙엽 2008.09.22 54365
24 Struts2(스트럿츠2) Feature(특징) 황제낙엽 2008.07.12 351
23 책 Starting Struts 2 - Free Online Version (InfoQ.com) file 황제낙엽 2008.07.11 730
22 스트러츠2 활용 Upload 예제 file 황제낙엽 2007.08.14 344
21 Struts 프레임워크를 이용한 효율적인 개발 전략 황제낙엽 2007.01.30 324
20 MyEclipse를 사용한 Struts 빠른 시작 황제낙엽 2007.01.26 585
19 EJB+JSP를 활용한 J2EE기반의 샘플 프로젝트 (JPetStore-5.0) 황제낙엽 2007.01.18 397
18 EJB+JSP를 활용한 J2EE기반의 샘플 프로젝트 (JPetStore-4.0.5) file 황제낙엽 2005.10.28 491
17 Struts GUI XML 작성 Tool 들 황제낙엽 2006.02.24 486
16 스트러츠로 작성된 웹서점 샘플화일 (Struts+Hsql) 황제낙엽 2005.11.24 305
15 Validation과 Dispatcher액션 황제낙엽 2006.05.22 550
14 폼빈의 Reset 황제낙엽 2006.05.22 371
13 Struts-Config.xml 과 액션과 JSP의 관계 황제낙엽 2006.05.22 327
12 struts-config 설명 및 작성방법 황제낙엽 2006.02.27 331