2

我有以下定义

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

4

2 回答 2

2

我真的不在乎 struts 如何处理异常。通常在覆盖 a 时使用 1.2 中的旧原始代码RequestProcesor,我可能应该替换这两种方法 - process 和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");

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

于 2011-01-12T16:30:06.190 回答
2

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

addMessages(HttpServletRequest request, ActionMessages messages)

或者

addErrors(HttpServletRequest request, ActionMessages errors)

你这样做吗?

于 2010-04-26T13:54:22.303 回答