Home:ALL Converter>How to properly validate method in struts by extending Action support class?

How to properly validate method in struts by extending Action support class?

Ask Time:2012-09-22T13:04:44         Author:fedvasu

Json Formatter

Here is my snippet of code,

public class LoginAction extends ActionSupport {
     private String userId;
 private String password;
     ...........

     public void validate() {
    if (getUserId().length() == 0) {
        addFieldError("userId",getText("userId.required"));
    } 
    else if (!getUserId().equals("va1525252")) {
        addFieldError("userId", "Invalid User");
    }
    if (getPassword().length() == 0) {
        addFieldError("password",getText("password.required"));
    }
}

when userId field is empty I get error saying Login Name is required I have never set such error message , so what is the problem here?

here is our LoginAction.properties file which is in the same package as LoginAction class

password.required = Password is required
userId.required = UserId is required

here is our screenshot of login page when clicked on submit with fields empty Login Page

Author:fedvasu,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/12540874/how-to-properly-validate-method-in-struts-by-extending-action-support-class
yy