需要检查用户ID存在与struts 2 [英] Need to check userid existence with struts 2
问题描述
我正在使用struts 2框架并尝试查看在数据库中检查用户ID存在的最佳方法。
在我的上一个项目中,我用jquery ajax做了这个,但是对它不满意。
在这个项目中,我使用验证框架对服务器端检查输入字段和jquery验证插件在客户端上b / p>
我有一个DAO类,它调用DB来检查是否存在,我不想使用jquery ajax但是更喜欢使用struts 2验证框架。
他们是否可以使用我的DAO类输出并使用字段表达式或使用validate方法将其与我的验证xml结合使用?如果我使用验证方法?与执行动作类相比,此方法的执行顺序是什么?我想要这种格式的订单,首先我做客户端验证,然后是服务器端验证,然后只有服务器端完成,我需要启动检查用户ID是否存在然后最终插入数据库?
我的字段操作验证文件如下所示,
<字段名称=电子邮件>
< field-validator type =requiredstring>
< message>需要电子邮件< / message>
< / field-validator>
< field-validator type =email>
< message>请输入有效的电子邮件ID< / message>
< / field-validator>
现在我需要检查db中是否存在电子邮件,所以我有DAO类返回true或者,如何从dao类添加验证?
您可以在字段中添加fieldexpression验证器。 DAO返回的值应放在值栈中。如果在action类中创建getter并返回值,则很容易。
行动类:
public boolean getMyBoolean(){
return myDAO.getMyboolean();
}
-validation.xml:
< field name =Email>
< field-validator type =requiredstring>
< message>需要电子邮件< / message>
< / field-validator>
< field-validator type =email>
< message>请输入有效的电子邮件ID< / message>
< / field-validator>
< field-validator type =fieldexpression>
< param name =expression><![CDATA [myBoolean == false]]>< / param> db< / message>中存在
< message>电子邮件
< / field-validator>
< / field>
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>
这篇关于需要检查用户ID存在与struts 2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!