The problems in error handling using Struts validation framework

前端 未结 2 1436
北恋
北恋 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?!

    0 讨论(0)
  • 2020-12-21 11:36

    After you obtain the ActionMessages/ActionErrors object which contains the messages or errors you want to display in your input page (using <html:messages> tags or <html:errors> tags), you must call one of the following methods from your Action object to place the result of your validation in scope:

    addMessages(HttpServletRequest request, ActionMessages messages)
    

    or

    addErrors(HttpServletRequest request, ActionMessages errors)
    

    Are you doing that?

    0 讨论(0)
提交回复
热议问题