How to Use the phototype framework to implement a simple Ajax Verification

Source: Internet
Author: User

How to Use the phototype framework to implement a simple Ajax verification:
It is easy to implement with phototype
1. Download a phototype. js file and put it in your project. Import the file to the page that requires Ajax verification.
2. Add the following code to create a simple FTL page:
<# Include "/error/actionerrors. FTL">
<# Assign base = Req. contextpath/>

<TD> * User Name: </TD> <input type = "text" name = "loginname" id = "loginname" size = "21" Height = "21" maxlength = "16" onblur = "searchsales (); "/>
<Div id = "result"/>
</TD>

<SCRIPT>
Function searchsales (){
VaR loginname = $ ('loginname'). value;
VaR url = '$ {base}/regist/checkname. action ';
VaR pars = 'loginname = '+ loginname;
VaR myajax = new Ajax. Request (
URL,
{
Method: 'get ',
Parameters: pars,
Oncomplete: showresponse
});
}
Function showresponse (originalrequest ){
// Put returned XML in the textarea
VaR resultdiv = $ ('result ');
Resultdiv. innerhtml = originalrequest. responsetext;
}
</SCRIPT>

3. Write an action to process requests sent from Ajax. Note that under the webwork2 framework, you can modify the action according to the actual situation. The steps are basically the same.
Package action;

Import java. Io. ioexception;
Import java. Io. printwriter;
Import java. util. List;

Import javax. servlet. http. httpservletrequest;
Import javax. servlet. http. httpservletresponse;

Import com. opensymphony. webwork. servletactioncontext;
Import com. opensymphony. xwork. actioncontext;

Import domain. admin;

Public class checknameaction extends baseaction {

 
// Servletactioncontext. Application;
// Servletactioncontext. Session;
// Servletactioncontext. page_context;

Public String execute (){
Actioncontext CTX = actioncontext. getcontext ();
// Obtain the request object in webwork
Httpservletrequest request = (httpservletrequest) CTX. Get (servletactioncontext. http_request );
// Obtain the response object in webwork
Httpservletresponse response = (httpservletresponse) CTX. Get (servletactioncontext. http_response );
String loginname = request. getparameter ("loginname ");
Try {
Response. setcontenttype ("text/html ");
Response. setcharacterencoding ("UTF-8 ");
Response. setheader ("cache-control", "No-Cache ");
Response. setheader ("Pragma", "No-Cache ");
List <admin> List = This. getloginservice (). listadmin ();
Int COUNT = 0;
If (list. Size ()! = 0 ){
For (Admin admin: List ){
If (Admin. GetUserName (). Equals (loginname )){
Count ++;
Break;
}
}
}
Printwriter out = response. getwriter ();
If (COUNT = 0)
Out. println ("<font color = 'green'> congratulations, the user name is available! </Font> ");
Else
Out. println ("<font color = 'red'> sorry, the user name is unavailable! </Font> ");
Out. Flush ();
Out. Close ();
} Catch (ioexception e ){
E. printstacktrace ();
}
Return NULL;
}
}
After most of the three parts, we can configure the action, which is very convenient.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.