ActionError消息未在UI中显示 [英] ActionError message not getting displayed in UI

查看:141
本文介绍了ActionError消息未在UI中显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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

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

 登录loginDetails = validateUser(loginForm); 
if(loginDetails == null){
errors.add(ActionErrors.GLOBAL_ERROR,new ActionError(error.invalidCredentials));
返回mapping.findForward(failure);
}

以下是 ApplicationResources.properties中的条目 file:

  error.invalidCredentials =用户名和密码不匹配。 

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



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

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

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

 < action input =/ mediaLogin.jsppath =/ welcometype =com.media.action.LoginAction
name =loginForm scope =sessionvalidate =true>
< forward name =successpath =/ rentLibrary.jsp>< / forward>
< forward name =failurepath =/ mediaLogin.jspredirect =false>< / forward>
< / action>

对于身份验证失败,我已将路径指定为具有登录屏幕的同一页面并进行了重定向 false



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



编辑



完整代码:

  LoginForm loginForm =(LoginForm)表单; 
HttpSession session = request.getSession();
ActionErrors errors = new ActionErrors();
if(loginForm.getAction()。equalsIgnoreCase(Login)){
//提供NUll Check
登录loginDetails = validateUser(loginForm);
if(loginDetails == null){
errors.add(ActionErrors.GLOBAL_ERROR,new ActionError(error.invalidCredentials));
返回mapping.findForward(failure);
}
populateLoginDetails(loginDetails,loginForm);
}
System.out.println(********************************** ****+ mapping.getAttribute()+************************************* *);
session.setAttribute(userDetails,loginForm);
返回mapping.findForward(success);

Struts-Config.xml

 < form-b​​eans> 
< form-b​​ean name =loginFormtype =com.media.form.LoginForm>< / form-b​​ean>
< / form-b​​eans>
< action-mappings>
< action input =/ mediaLogin.jsppath =/ welcometype =com.media.action.LoginAction
name =loginFormscope =sessionvalidate =true >
< forward name =successpath =/ rentLibrary.jsp>< / forward>
< forward name =failurepath =/ mediaLogin.jspredirect =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>


解决方案

错误消息未显示,因为你没有得到他们来自验证者表格。 Struts使用 ValidatorForm 表单应该扩展并覆盖 validate 方法。在 validate 方法中,您可以手动或使用Apache commons验证器检查表单字段。 GenericValidator.isBlankOrNull 例如需要检查字段。填写 ActionErrors 并调用 super.validate 以从可以合并的框架中获取其他错误。一旦调用 super.validate ERROR_KEY 已经被请求。另外在使用struts验证器框架进行错误处理的问题中我已经描述了如何在验证期间处理异常以放置 EXCEPTION_KEY 错误属性。其他诸如动作中的跳过验证和验证调度方法可能由于覆盖 RequestProcessor 添加验证方法键(验证方法键是用于映射<$使用的方法的键c $ c> getKeyMethodMap()),并处理 processValidation

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

编辑:



如果您想忽略验证框架并在操作中手动验证

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

之后可以通过< html在JSP中显示它:消息

 < logic:messagesPresent> 
< html:messages id =error>
< span>< bean:write name =error/>< / span>< br />
< / html:messages>
< / logic:messagesPresent>


The application is made on Struts 1.2. I have a login form, all the validations related to the login are getting displayed properly. For the validations, I am using the Validator framework.

When the validation succeeds but the user authentication fails, then I am not able to display the login failure message properly.

I am setting the ActionError message in the Action like this:

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

Below is the entry made in the ApplicationResources.properties file:

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

When the loginDetails is null then the errors are getting added properly (I verified this by debugging the code). But the messages are not getting displayed in the UI.

This is what I added in the UI:

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

Below is the entry made in struts-config file:

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

For an authentication failure, I have given the path as the same page having the login screen and made the redirect to false.

Please let me know what am I missing :)

EDIT

Full code:

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>

解决方案

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>

这篇关于ActionError消息未在UI中显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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