A preliminary summary of AJAX applications

Source: Internet
Author: User
Tags object functions include return string window client
Ajax

There are some actions in the form, in particular, many of the calls to the server-side method functions, which caused the trouble believe that every programmer knows that is the page refresh, both time-consuming and annoying, the head has been brushed, and he refreshed, but also triggers the server side of the event, now the advent of Ajax, Make a lot of solution to this problem, really a bit like the taste of CS. Recently some time I saw some of the articles on Ajax, but also asked the path and other people, in many places of the project used Ajax, such as form verification, small pull box linkage, and so on, a bit available today, on the rough finish, write point document. Detailed experience and so on after having a deep understanding later in to talk about.

Today, as an example of user authentication, real-time data validation is one of the great advantages of Ajax technology. By applying this technology, the Struts validation framework enhances struts MVC and makes Web applications closer to desktop applications. This article focuses on using AJAX to enhance the existing struts validation framework.
  
This validation framework is used to validate fields. There are many ways to validate on a Web application. These methods can be divided into two categories: server-side methods and client methods. The Struts validation framework is one of the best frameworks for a java-based Web-application environment. It is able to configure the application by using server-side validation and error messages, which are rendered on the calling verification process when processing the request, and are capable of client-side validation by using the JavaScript rendered on the request page. Ajax is a JavaScript technology that makes it possible to call servers asynchronously and get XML documents, a document that has been very popular recently. One of its uses is real-time data validation.

1,newactor.jsp, need to verify the new user interface, the other slightly, only keep the user name fill box.
<%@ page language= "java" pageencoding= "GB2312"%>
<%@ include file= "taglibs.jsp"%>


<%@ include file= "metas.jsp"%>




Verify that the user exists
function Check () {
var Userid=document.forms[0].userid.value;
var url=document.forms[0].action = "omactor.do?method=isuseridexit&userid=" +userid;
var tag= "Load";
Send_request (Url,tag);
}


Display background information back at this point



<TD class= "Tdtitle" > User name:
<TD class= "Tdright" >





2,omactoraction, background code, the background function that the check () in the JSP points to.
public class Omactoraction extends Dispatchaction {
/**
* Front desk using AJAX to verify that the user has been registered
*/
Public Actionforward isuseridexit (actionmapping mapping,actionform form,
HttpServletRequest request,httpservletresponse response) throws exception{
try {
Request.setcharacterencoding ("gb2312");
catch (Exception e) {
E.printstacktrace ();
}
try{
String msgstr= "";
Response.setcontenttype ("text/xml;charset=gb2312");
Response.setheader ("Cache-control", "No-cache");
String Username= (String) request.getparameter ("userid");
if (Getactorservice (). Isuserexit (username)) {//Check the database for the user name
Msgstr = "Sorry, this username already exists, please change user name registration!";
}else{
MSGSTR = "User is not registered, you can use!";
}
Response.getwriter (). println (" ')";
Response.getwriter (). println ("");
Response.getwriter (). println ("");
Response.getwriter (). print (MSGSTR);
Response.getwriter (). println ("");
Response.getwriter (). println ("");
Response.getwriter (). Close ();
}catch (Exception ex) {

}
return null;
}
}


3,ywajax.js
var http_request = false;
var divid= "";

function Send_request (Url,tag) {//initialization, specifying handler functions, sending requests
var Ajaxid=tag;
Http_request = false; File: Start initializing XMLHttpRequest Object
if (window. XMLHttpRequest) {//mozilla browser
Http_request = new XMLHttpRequest ();
if (http_request.overridemimetype) {//Set MIME category
Http_request.overridemimetype (' Text/xml ');
}
}else if (window. ActiveXObject) {//IE browser
try{
Http_request = new ActiveXObject ("Msxml2.xmlhttp");
}catch (e) {
try{
Http_request = new ActiveXObject ("Microsoft.XMLHTTP");
}catch (e) {
}
}
}
if (!http_request) {//exception, failed to create object instance
Window.alert ("Cannot create XMLHttpRequest object instance.");
return false;
}
Divid=ajaxid;
Http_request.open ("Get", url, True); Determine how and when the request is sent and whether the next code is executed synchronously
Http_request.onreadystatechange = ProcessRequest;

Http_request.send (NULL);
}

function ProcessRequest () {//functions to process return information
if (http_request.readystate = = 4) {//Judge object state
if (Http_request.status = = 200) {//information has been successfully returned to start processing information
var response = Http_request.responsetext;
document.getElementById (divID). Innerhtml=response;
}else {//page is not normal
Alert ("The page you are requesting has an exception.) ");
}
}
}

Form verification is probably the case, as well as the dropdown box linkage and data query and so on do not talk about, as for the use of DWR and so on the next time when the summary summary, now for his theory is not very understanding.



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.