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>
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.
来源:https://stackoverflow.com/questions/14480631/actionerror-message-not-getting-displayed-in-ui