??????ν???????????????????????????JDK???????????+????????

????struts2???????????????????к??????????????????????????????????????????

???????????action??service???dao??????б???????????????try...catch??catch????????log???????????????????????????????????

 

//action???????????????
public String save(){
   try{
         //????service??save????
         service.save(obj);
   }catch(Exception e){
      //????????????Runtime????????????????????дthrows  xx
      throw new RuntimeException("????????????????"??e);
  }
   return "success";
}

????????????????????????д??????????????

 

public String intercept(ActionInvocation actioninvocation) {
 
  String result = null; // Action??????
  try {
   // ???б??????Action????????????????catch?
   result = actioninvocation.invoke();
   return result;
  } catch (Exception e) {
   /**
    * ??????
    */
   String errorMsg = "δ?????";
   //???instanceof?ж??????????????
   if (e instanceof BaseException) {
    BaseException be = (BaseException) e;
    be.printStackTrace(); //??????????????????????
    if(be.getMessage()!=null||Constants.BLANK.equals(be.getMessage().trim())){
     //?????????
     errorMsg = be.getMessage().trim();
    }
   } else if(e instanceof RuntimeException){
    //δ??????????
    RuntimeException re = (RuntimeException)e;
    re.printStackTrace();
   } else{
    //δ?????????
    e.printStackTrace();
   }
   //?????????????
   HttpServletRequest request = (HttpServletRequest) actioninvocation
     .getInvocationContext().get(StrutsStatics.HTTP_REQUEST);
   
   /**
    * ???????????????
    */
   request.setAttribute("errorMsg"?? errorMsg);
  
   /**
    * log4j??????
    */
   Log log = LogFactory
     .getLog(actioninvocation.getAction().getClass());
   if (e.getCause() != null){
    log.error(errorMsg?? e);
   }else{
    log.error(errorMsg?? e);
   }
 
   return "error";
  }// ...end of catch
 }