The problems in error handling using Struts validation framework

前端 未结 2 1435
北恋
北恋 2020-12-21 10:41

I have following defined in

struts-config.xml:



    

        
2条回答
  •  囚心锁ツ
    2020-12-21 11:22

    I'm really do not care how about struts handles an exception. Using old raw code from 1.2 generally when overriding a RequestProcesor, I should probably replace the two methods - process and processException. First thing is happy about catching exception from the request after processValidation has been made. The fragment of code could look like

    Exception exception = null;
    if (needValidation)
      try {
        if (! processValidate(request, response, form, mapping)) {
          return;
        }
        exception = (Exception)request.getAttribute(Globals.EXCEPTION_KEY);
      } catch (InvalidCancelException ex) {
        exception = ex;
      }
    ActionForward forward;
    // Check out if exception occurred
    
    if (exception != null){
      forward = processException(request, response, exception, form, mapping);
    

    The second is pretty easy if you have configured the errors forward. The errors forward is usually one of the global forwards that easily found from the mapping. Once it found, it likes to display your error message on the page. I think those would probably enough for processing an exception

    exception.printStackTrace();
    log.error(exception);
    request.setAttribute("error", exception.getMessage());
    return mapping.findForward("error");
    

    It has been done because validate method from ActionForm or ValidatorForm doesn't throw any exceptions and I couldn't properly override this method without throwing some. Once thrown, who will care about it?!

提交回复
热议问题