【问题标题】:The problems in error handling using Struts validation framework使用 Struts 验证框架处理错误的问题
【发布时间】:2010-04-26 07:24:09
【问题描述】:

我在

中定义了以下内容

struts-config.xml:

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

登录表单:

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:

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

属性文件:

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 中显示错误。

【问题讨论】:

    标签: java validation exception struts struts-1


    【解决方案1】:

    在获得ActionMessages/ActionErrors 对象后,该对象包含要在输入页面中显示的消息或错误(使用&lt;html:messages&gt; 标签或&lt;html:errors&gt; 标签),您必须调用以下方法之一从您的 Action 对象中将您的验证结果放在范围内:

    addMessages(HttpServletRequest request, ActionMessages messages)
    

    addErrors(HttpServletRequest request, ActionMessages errors)
    

    你在做吗?

    【讨论】:

      【解决方案2】:

      我真的不在乎 struts 如何处理异常。在覆盖 RequestProcesor 时,通常使用 1.2 中的旧原始代码,我可能应该替换这两种方法 - 进程和 processException。在发出processValidation 之后,首先要从请求中捕获异常感到高兴。代码片段可能看起来像

      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.printStackTrace();
      log.error(exception);
      request.setAttribute("error", exception.getMessage());
      return mapping.findForward("error");
      

      它已经完成,因为来自ActionFormValidatorForm 的验证方法不会抛出任何异常,并且我无法正确覆盖此方法而不抛出一些异常。一旦扔出去,谁会在乎?!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多