使用Strutsvalidation框架进行error handling的问题

我有以下定义

struts-config.xml中:

                

登录表格:

 public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) { ActionErrors errors = new ActionErrors(); if (userName == null || userName.length() < 1) { System.out.println("in validate ---"); errors.add("userName", new ActionMessage("error.userName.required")); // TODO: add 'error.name.required' key to your resources } if (password == null || password.length() < 1) { errors.add("password", new ActionMessage("error.password.required")); // TODO: add 'error.name.required' key to your resources } return errors; } 

login.jsp的:

        

属性文件:

 error.userName.required = User Name is required. error.password.required = Password is required. 

我哪里做错了? 我收到以下错误

 javax.servlet.jsp.JspException: Cannot find bean error in any scope 

我只想在同一个JSP中显示错误。

获取包含要在输入页面中显示的消息或错误的ActionMessages / ActionErrors对象(使用标记或标记)后,必须从Action调用以下方法之一对象将validation结果放在范围内:

 addMessages(HttpServletRequest request, ActionMessages messages) 

要么

 addErrors(HttpServletRequest request, ActionMessages errors) 

你在做吗?

我真的不在乎struts如何处理exception。 通常在覆盖RequestProcesor时使用1.2中的旧原始代码,我应该替换两个方法 – process和processException 。 首先,我很高兴在processValidation完成后从请求中捕获exception。 代码片段看起来像

 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); 

如果您已将错误向前配置,则第二个非常简单。 向前的错误通常是从映射中很容易找到的全局转发之一。 一旦找到,它就喜欢在页面上显示您的错误消息。 我认为这些可能足以处理exception

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

它已经完成,因为ActionFormValidatorFormvalidation方法不会抛出任何exception,我无法正确覆盖此方法而不抛出一些。 一旦抛出,谁会关心它?!