ActionError message not getting displayed in UI

て烟熏妆下的殇ゞ 提交于 2019-12-06 07:58:00
Roman C

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>

First of all, add a blank validate() method even if you don't write the code in it as suggested by @RomanC.

Then, try adding

saveErrors(request, errors);

after

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

If this does not work, reply back. Let us make it work first, and then you better use the standard way as @RomanC suggests.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!