2

该应用程序是在 Struts 1.2 上进行的。我有一个登录表单,所有与登录相关的验证都会正确显示。对于验证,我使用的是 Validator 框架。

当验证成功但用户身份验证失败时,我无法正确显示登录失败消息。

ActionError在 Action 中设置消息,如下所示:

Login loginDetails = validateUser(loginForm);
if(loginDetails == null){
    errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.invalidCredentials"));
    return mapping.findForward("failure");
}

以下是ApplicationResources.properties文件中的条目:

error.invalidCredentials=User Name and Password does not match.

loginDetails为空时,错误就会被正确添加(我通过调试代码验证了这一点)。但是消息没有显示在 UI 中。

这是我在 UI 中添加的内容:

<td align="right">
    <div style="color:red">
        <html:errors />
    </div>
</td>

下面是 struts-config 文件中的条目:

<action input="/mediaLogin.jsp" path="/welcome" type="com.media.action.LoginAction"
    name="loginForm" scope="session" validate="true">
    <forward name="success" path="/rentLibrary.jsp"></forward>
    <forward name="failure" path="/mediaLogin.jsp" redirect="false"></forward>
</action>

对于身份验证失败,我已将路径与登录屏幕相同的页面指定redirectfalse.

请让我知道我错过了什么:)

编辑

完整代码:

LoginForm loginForm = (LoginForm)form;
HttpSession session = request.getSession();
ActionErrors errors=new ActionErrors();     
if(loginForm.getAction().equalsIgnoreCase("Login")){
    // Provide the NUll Check
    Login loginDetails = validateUser(loginForm);
    if(loginDetails == null){
        errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.invalidCredentials"));
        return mapping.findForward("failure");
    }
    populateLoginDetails(loginDetails, loginForm);
}
System.out.println("**************************************" + mapping.getAttribute() + "**************************************");
session.setAttribute("userDetails", loginForm);
return mapping.findForward("success");      

Struts-Config.xml

<form-beans>    
    <form-bean name="loginForm" type="com.media.form.LoginForm" ></form-bean>   
</form-beans>
<action-mappings>
    <action input="/mediaLogin.jsp" path="/welcome" type="com.media.action.LoginAction"
        name="loginForm" scope="session" validate="true">
        <forward name="success" path="/rentLibrary.jsp"></forward>
        <forward name="failure" path="/mediaLogin.jsp" redirect="false"></forward>
    </action>               
</action-mappings>

<message-resources parameter="resources.ApplicationResources" />

<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
    <set-property property="pathnames"
        value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml" />
</plug-in>
4

2 回答 2

3

首先,添加一个空白的 validate() 方法,即使您没有按照@RomanC 的建议在其中编写代码。

然后,尝试添加

saveErrors(request, errors);

errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.invalidCredentials"));

如果这不起作用,请回复。让我们先让它工作,然后你最好使用@RomanC 建议的标准方式。

于 2013-01-24T16:39:29.770 回答
2

Error messages not getting displayed, right because you not getting them from the validator form. Struts use ValidatorForm that your form should extend and override the validate method. In the validate method you can check the form fields manually or using Apache commons validator. GenericValidator.isBlankOrNull for example checks for fields are required. Fill the ActionErrors and call the super.validate to get additional errors from the framework that you can merge. Once you call super.validate the ERROR_KEY is already put into request. Additionally in the post problem in error handling using struts validator framework I've described how handle exceptions during validation to put EXCEPTION_KEY to the error attribute. Other things like skip validation in the actions and validation dispatch methods possible due to overriding RequestProcessor adding validation method keys (Validation Method Key is the key used to map the method used by getKeyMethodMap()), and handle the processValidation.

ActionErrors actionErrors = super.validate(mapping, request);    
actionErrors.add(ActionErrors.GLOBAL_MESSAGE, new ActionMessage("error.invalidCredentials"));

EDIT:

If you want to ignore the validation framework and do validate manually in the action

ActionErrors errors = new ActionErrors();
errors.add(ActionErrors.GLOBAL_MESSAGE, new ActionMessage("error.invalidCredentials"));
request.setAttribute(Globals.ERROR_KEY, errors);
return mapping.findForward("failure");

After that it will be possible to display it in JSP via <html:messages.

<logic:messagesPresent>
  <html:messages id="error">
    <span><bean:write name="error"/></span><br/>
  </html:messages>
</logic:messagesPresent>
于 2013-01-23T15:06:21.997 回答