Now, you need to verify the existence of the user name asynchronously. The technology used is jQuery asynchronous verification and struts2 (in fact, springMVC is the same principle, both of which are just a controller ).
Form:
Copy codeThe Code is as follows:
<Input class = "width150" maxlength = "32" type = "text" id = "dept_name" name = "dept. dept_name"
Value = "$ {dept. dept_name}" onchange = "tocheckname ()"/>
Js Code:
Copy codeThe Code is as follows:
Function tocheckname ()
{
Var deptName = $ ("# dept_name"). val ();
$. Ajax ({
Type: "POST ",
Cache: false,
Url: "$ {rootPath}/dept/checkdeptname.htm ",
DataType: "text ",
Data: {"dept. id": "$ {dept. id}", "dept. dept_name": deptName },
Async: false,
Success: function (data ){
If (0 <parseInt (data )){
Alert ("this department already exists ");
$ ("# Dept_name"). attr ("value ","");
$ ("# Dept_name"). focus ();
}
}
});
}
Background code:
Copy codeThe Code is as follows:
/**
* Query whether the specified department exists based on the department name.
*
* @ Return
* @ Throws Exception
*/
Public String checkdeptname () throws Exception {
If (UtilAPI. isNull (dept )){
Dept = new Dept ();
}
Int count = this. deptService. checkdeptname (dept. getId (), dept. getDept_name (); // if 1 exists, 0 is returned if no exists
Try {
Response. getOutputStream (). print (count );
}
Catch (IOException e ){
E. printStackTrace ();
}
Return Action. NONE;
}