www.pudn.com > project.rar > BaseAction.java


package cn.com.iaspec.workflow.client.web.action.base; 
 
import java.lang.reflect.*; 
import java.util.*; 
import javax.servlet.http.*; 
import org.apache.log4j.*; 
import org.apache.struts.action.*; 
 
public abstract class BaseAction 
    extends Action 
    implements Interceptor{ 
  public final Logger logger=Logger.getLogger(this.getClass()); 
 
  public ActionForward execute(ActionMapping mapping,ActionForm form, 
      HttpServletRequest request,HttpServletResponse response) 
      throws Exception{ 
    try{ 
      String methodName=request.getParameter("method"); 
      if(methodName==null){ 
        throw new RuntimeException( 
            "the method param must be specifed for the action "+ 
            this.getClass().getName()); 
      } 
      logger.info("the method '"+methodName+"' will be executed..."); 
      Method method=this.getClass().getMethod(methodName,new Class[]{ 
          ActionMapping.class,ActionForm.class,HttpServletRequest.class, 
          HttpServletResponse.class 
      }); 
      ActionForward forward=this.before(mapping,request,methodName); 
      if(forward!=null){ 
        return forward; 
      } 
      forward=(ActionForward)method.invoke(this,new Object[]{ 
          mapping,form,request,response}); 
      this.after(request,methodName); 
      return forward; 
    } 
    catch(Exception ex){ 
      ex.printStackTrace(); 
      throw ex; 
    } 
  } 
 
  protected void saveErrorInfo(HttpServletRequest request,String key, 
      String error){ 
    Map errors=(Map)request.getAttribute("errorMap"); 
    if(errors==null){ 
      errors=new HashMap(); 
      request.setAttribute("errorMap",errors); 
    } 
    errors.put(key,error); 
  } 
 
  protected void saveErrorInfo(HttpServletRequest request,String error){ 
    final String defaultErrorKey="default_web_error_key"; 
    this.saveErrorInfo(request,defaultErrorKey,error); 
  } 
 
  /** 
   * after advice 
   * 
   */ 
  public void after(HttpServletRequest request,String methodName) 
      throws Exception{ 
  } 
 
  /** 
   * before advice 
   * 
   */ 
  public ActionForward before(ActionMapping mapping,HttpServletRequest request, 
      String methodName) 
      throws Exception{ 
    return null; 
  } 
 
}