We have completed the user management List of additions and deletions and image upload, and then use the POI technology to complete the user list information of Excel import and Excel file export work. Next we will write the account-related business.
Can see us add user to assign an account
However, our account must not be repeated, if the account already exists, we will not be able to keep it in the system, to tell the user account already exists. So this time we are going to check the uniqueness of the user account.
Verifying the uniqueness of user accounts:
Places to check: Add and edit; When you edit a page, the account number is already present and you need to exclude the current account from the check range
Check timing: When the value of the account changes;
Verify that the account number is unique:
Front End: Ajax
Back end:
1. Get Account Number
2. Check
2.1, according to the user account query
SELECT * from user where account= ' account ' and id! = ' user ID '
Let's do the following:
At the front desk we use AJAX technology to achieve verification, we first add the corresponding JS code in the addui.jsp, when our account value changes, we execute the corresponding JS code:
<tr> <td class= "tdbg" width= "200px" > Accounts:</td> <td><s:textfield id= "account" name = "User.account" onchange= "doverify ()"/></td></tr> the corresponding JS method: <script type= "Text/javascript" >// Verify account Unique function doverify () {//1. Get account var account=$ ("#account"). Val (); if (account!= "") {//2. Checksum Get/post/getjson/ajax $. Ajax ({URL: "${basepath}tax/user_verifyaccount.action", data:{"User.account": Account},trpe: "POST", Success: Function (msg) {if ("true"!=msg) {///account already exists alert ("Account already exists, please use another account!"); /fixed focus $ ("#account"). focus ();}});}} </script>
Front of the code basically finished, we implement User_verifyaccount in the background.
First add the Verifyaccount method to the Useraction:
Verify user account unique public void Verifyaccount () {try {//1. Get account if (user! = null && Stringutils.isnotblank (User.getaccount ( )) {//2. According to the account to the database, whether there is a user list<user> list = Userservice.finduserbyaccountandid (User.getid (), User.getaccount ()); String strresult = "true"; if (list = null && list.size () > 0) {//indicates that the account already exists strresult = "false";} Output HttpServletResponse response = Servletactioncontext.getresponse (); Response.setcontenttype ("text/html"); Servletoutputstream outputstream = Response.getoutputstream (); Outputstream.write (Strresult.getbytes ()); O Utputstream.close ();}} catch (Exception e) {e.printstacktrace ();}}
There is no Finduserbyaccountandid method in our UserService to implement it.
Defining the Finduserbyaccountandid () method in the interface, we implement this method in Userserviceimpl:
@Overridepublic list<user> Finduserbyaccountandid (string ID, string account) {return Userdao.finduserbyaccountandid (Id,account);}
There is no Finduserbyaccountandid method in our Userdao to implement it.
Defining the Finduserbyaccountandid () method in the interface, we implement this method in Userdaoimpl:
@Overridepublic list<user> Finduserbyaccountandid (string ID, String account) {string hql= ' from User-WHERE account = ?"; if (Stringutils.isnoneblank (ID)) {hql+= "and id! =?";} Query query=getsession (). CreateQuery (HQL), Query.setparameter (0, account), if (Stringutils.isnoneblank (ID)) { Query.setparameter (1, id);} return Query.list ();}
The front-end code is all done, so let's test it out:
We click Add, before we have an account called "Jack", we deliberately entered this account will be found to give the error prompt
Then we change a not used, then no longer have the hint:
So our account verification function is successful!
The light is not good enough, if the user ignore this warning to continue to save or not, so we have to click on the "Save" button when the user check again:
<input type= "Submit" class= "BtnB2" value= "Save" onclick= "return Dosubmit ()"/>
Detection of the JS code:
(We modified the previous doverify () method, so I put the previous code on it as well.) We set a global variable vresult, if our account verification succeeds, Vresult is true, if the checksum failure is the default false, so that we can control the form of the submission. However, because Ajax is asynchronous commit, so our vresult value is not properly obtained, so we have to change the way Ajax is submitted to "non-asynchronous", so as to correctly get the value of Vresult, so as to determine whether the user can submit a form)
<script type= "Text/javascript" >var vresult=false;//Verify account unique function doverify () {//1. Get account var account=$ ("#account "). Val (), if (account!=" ") {//2. Checksum get/post/getjson/ajax $.ajax ({url:" ${basepath}tax/user_verifyaccount.action ", data:{"User.account": Account},trpe: "POST", async:false,//non-asynchronous Success:function (msg) {if ("true"!=msg) {//account already exists alert ("Account already exists, please use another account!"); /fixed Focus $ ("#account"). focus ();} Else{vresult=true;}});}} Submit Form Function Dosubmit () {var name=$ ("#name"), if (name.val () = = "") {alert ("Username cannot be empty! "); Name.focus (); return false;} var password=$ ("#password"), if (password.val () = = "") {alert ("Password cannot be empty!") ");p Assword.focus (); return false;} Account check doverify (); if (vresult) {//Allow the form to be submitted return true; } else{ return false; }} </script>
We tested, deliberately entered a duplicate account, ignoring the warning to save directly, but found an error
Show that our control is successful!
Below we also complete the editing interface data verification, the same first in the user name, account number, password plus ID, and then add the Change event on the account:
<tr> <td class= "tdbg" width= "200px" > User name:</td> <td><s:textfield id= "name" Name= " User.Name "/> </td></tr><tr> <td class=" tdbg "width=" 200px "> account:</td> < Td><s:textfield id= "Account" Name= "User.account" onchange= "doverify ()"/></td></tr><tr> <td class= "tdbg" width= "200px" > Password:</td> <td><s:textfield id= "password" name= " User.password "/></td></tr>
And, of course, the calibration method at the time of preservation.
<input type= "Submit" class= "BtnB2" value= "Save" onclick= "return Dosubmit ()"/>
JS Method (We detect that there is no one else to use this account except ourselves):
<script type= "Text/javascript" >var vresult = false;//Verify account unique function doverify () {//1, get account var = $ ("#account "). Val (); if (account! =" ") {//2, checksum $.ajax ({URL:" ${basepath}tax/user_verifyaccount.action ", data: {" User.account "): Account, "User.ID": "${user.id}"},type: "POST", async:false,//non-asynchronous Success:function (msg) {if ("true"! = msg) {// Account already exists alert ("Account already exists. Please use another account! ");//fixed Focus $ (" #account "). focus (); vresult = false;} else {vresult = true;}}});}} Submit Form Function Dosubmit () {var name = $ ("#name"), if (name.val () = = "") {alert ("Username cannot be empty! "); Name.focus (); return false;} var password = $ ("#password"), if (password.val () = = "") {alert ("Password cannot be empty!") ");p Assword.focus (); return false;} Account check doverify (); if (Vresult) {return true;} Else{return false;}} </script>
Test, we clicked on a user for "edit" operation, we deliberately changed its account name "Jack" to the already registered "Aim":
Discovery cannot be submitted, so our validation of the edit page is also successful!
All of our calibration work has been completed.
Reprint Please specify source: http://blog.csdn.net/acmman/article/details/49420837
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
"SSH Project Combat" national tax cooperation platform-8. User account Uniqueness Check