Need to check userid existence with struts 2

∥☆過路亽.° 提交于 2019-12-08 08:03:46

问题


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?


回答1:


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>


来源:https://stackoverflow.com/questions/23936580/need-to-check-userid-existence-with-struts-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!