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
ValidatorFormthat your form should extend and override thevalidatemethod. In thevalidatemethod you can check the form fields manually or using Apache commons validator.GenericValidator.isBlankOrNullfor example checks for fields are required. Fill theActionErrorsand call thesuper.validateto get additional errors from the framework that you can merge. Once you callsuper.validatetheERROR_KEYis 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 putEXCEPTION_KEYto theerrorattribute. Other things like skip validation in the actions and validation dispatch methods possible due to overridingRequestProcessoradding validation method keys (Validation Method Key is the key used to map the method used bygetKeyMethodMap()), and handle theprocessValidation.EDIT:
If you want to ignore the validation framework and do validate manually in the action
After that it will be possible to display it in JSP via
<html:messages.