I am using struts 2 framework and trying to see as what is the best way to check for user id existence in the database.
In my last project, I did this with jquery ajax,but was not satisfied with it.
In this project, I am using validation framework for server side checks for input fields and jquery validate plugin on client side.
I have a DAO class which makes call to DB to do checks for existence,I dont want to use jquery ajax but would prefer to go with struts 2 validation framework.
Is their a way I can use my output of this DAO class and combine it with my validation xml either using field expression or by using validate method ? if I use validate method ? what is the order of execution of this method compare to execute of action class ? I want the order in this format, first I do client side validation , followed by server side with validation and then only once server side is completed, I need to initiate check for user id existence and then finally do insert into the DB ?
My action validation file for field is something like below,
<field name="Email">
<field-validator type="requiredstring">
<message>Email is required</message>
</field-validator>
<field-validator type="email">
<message>Please enter valid email id</message>
</field-validator>
Now I need to check if email exists in db, so for that I have DAO class which returns true or false, how do I add validation from dao class?
You can add fieldexpression validator to the field. The value returned by DAO should be placed to the value stack. It's easy if you create a getter in the action class and return the value.
action class:
public boolean getMyBoolean(){
return myDAO.getMyboolean();
}
-validation.xml:
<field name="Email">
<field-validator type="requiredstring">
<message>Email is required</message>
</field-validator>
<field-validator type="email">
<message>Please enter valid email id</message>
</field-validator>
<field-validator type="fieldexpression">
<param name="expression"><![CDATA[myBoolean == false]]></param>
<message>email exists in db</message>
</field-validator>
</field>