Saturday 26 May 2012

How to do field validation using the Acegi security in spring



Hi Friends , Today i am going to explain about the validation of username or password while using the Acegi security in spring. let assume our application is using the form based authentication and user clicks on the submit button without entering the field(username,password) value, Acegi security internally executes the query. Here no point in executing the query.





In the terms of DB transaction it is costly operation. In other word , if in our application some pattern is defined for username and password like username shouldn't contain special character etc.Then this type of validation we could do before the request goes to the acegi security otherwise it would be unusual operation. To stop this unusual operation we can modify our code.

We can check before submitting to the server that the values entered by user are satisfied the required format if no then we can show the login page with appropriate error message. Here using this technique we can save so many db interactions.
For that we have to change the following code







login.htm will be mapped to controller. A delegate Controller will use a Model which having 2 parameter with the name of j_username and j_password it validated both field value based on the define rule once the validation passes then it will navigate to success jsp.
In the success jsp we have a java script , called by onLoad event. in this function we have to write following code


Now it will work fine.

1 comment: