使用Struts验证框架的错误处理中的问题 [英] The problems in error handling using Struts validation framework

查看:183
本文介绍了使用Struts验证框架的错误处理中的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下定义在

<struts-config>
<form-beans>
    <form-bean name="LoginForm" type="com.actionform.LoginForm"/>
      </form-beans>

        <action-mappings>

    <!-- action for login  -->
    <action input="/views/login.jsp" name="LoginForm" path="/Login" scope="session" type="com.actions.LoginAction"
    parameter="method" validate="true">
        <forward name="success" path="/views/Frameset.html" />

    </action>
       </action-mappings>

<message-resources parameter="/WEB-INF/ApplicationResources"/>
    <!-- ========================= Validator plugin ================================= -->
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
    <set-property
        property="pathnames"
        value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/>
</plug-in>

</struts-config>



登录表单:



The login form:

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:



login.jsp:

<html:form action="/Login?method=loginUser">

<html:errors/>

<html:text name="LoginForm" property="userName" /> 

<html:messages id="err_userName" property="userName">
            <bean:write name="err_userName" />
</html:messages>
</html:form>



属性文件:



Property file:

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

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

Where am I doing wrong? I am getting the following error

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

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

I just want to display the error in the same JSP.

推荐答案

我真的不在乎struts如何处理异常。在覆盖一个 RequestProcesor 时通常使用旧的原始代码,我应该替换这两个方法 - process和 processException 。首先,在 processValidation 已经完成之后,从请求中捕获异常是第一件事。代码片段可能看起来像

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

这是因为验证方法来自 ActionForm ValidatorForm 不会引发任何异常,我无法正确覆盖此方法而不抛出一些。一旦抛出,谁会关心它?

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?!

这篇关于使用Struts验证框架的错误处理中的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆