AJAX framework Day1
1, Ajax Introduction
a) Concept A Synchronous J avascript A Nd X- ML (asynchronous request javaScript And XML)
b) traditional request
- Traditional way of initiating the request
- Address Bar
- Super Link
- Forms
- Javascript: location.href function
- The problem with traditional request
because the traditional response is to request a new page (refresh the page). Therefore, users have to wait for a response result, subsequent operations can.
Poor user experience.
- Based on the characteristics of the asynchronous request
content of the response is not a new page, a local, a character string information page, the user in the use of asynchronous requests, without waiting for a response, the subsequent processing may be performed directly.
- Difference between traditional asynchronous request request
- In response to the contents of
the content of the traditional response: a new page, refresh the page
asynchronous response content: local, string information page - Operation by the user for
traditional request: waiting for a response
asynchronous requests: without waiting for a response, the user can directly operate the subsequent
- In response to the contents of
- Recommended in the appropriate scene, use asynchronous requests to improve the user experience
2, the development of asynchronous request
Create a) an asynchronous request object
javascript: XmlHttpRequest objects XHR
the Chrome | the FF | Safari in the XmlHttpRequest
var = new new XHR the XmlHttpRequest ();
IEs (IE5 IE6) in the ActiveXObject
var = new new XHR the ActiveXObject ( "Microsoft.XMLHTTP");
Use b) xhr object
3, case detection username
No longer jump
Control whether to submit the form, the detection time of submission
4, Ajax development steps
a) creating asynchronous request
IE based xhr = new ActiveXObject ( "Microsoft.XMLHTTP" );
non-IE-based xhr = new XmlHttpRequest ();
b) sending the request the queryString
xhr.open ( "the GET", the URL name = & Suns = pwd = 123 & Sex MALE);?
xhr.send (null);
xhr.open ( "the POST", URL, to true (whether asynchronous));
XHR .setRequestHeader ( "content-type", "application / x-www-form-urlencoded");
Why set: analog form
basic Web developers can use the form to submit data, the default specified when submitting data using a form
enctype = "application / x-www -form-urlencoded", but now using ajax programming, no longer in use
<form a tag it did not provide a value corresponding to the attribute enctype way. Then this time the need to
manually set the application / x-www-form- urlencoded request header to simulate form.
xhr.send ( "name = suns & pwd = 123 & sex = male") ;, communicating data in quotes,
c) listens for a response
xhr.onreadystatechange = function () {
IF (xhr.readyState == == 200 is xhr.status &&. 4) {
xhr.responseText
the DOM programming
}
}
d) the server side:
no page jump, but by returning the character string information, complete interaction
request scope is not used
obtain an output bottom stream is used response.getWriter () interacting
. 5, JSON protocol string
JSON protocol string is essentially a string.
a) action: the heterogeneous system programming, data transmission, interactive.
b) Core:
- It defines the format string
- Object form
- Aggregate form
- Embedded object
- Form consistent with the object of type Map
- JSON protocol analysis support a variety of tools can automatically convert
java end: the library JSONlib GSON (Google) FASTJSON (Ali) jakeson (SpringMVC)
Gson // objects are braces, a collection of small brackets
- Object form
Note that map is as objects to write, braces
- Gson specific basic应用
Gson Gson = New Gson ();
Gson.ToJson ();
- Gson specific application scenarios
-
Inconsistent with the attribute name a) Json string object property name problem
b) loop problem resolution objects
- Notes @Expose
with the annotations @Expose of the property will be excluded and will not be converted
Too inflexible, complicated both by gsonbuilder
- And use interface,
to achieve a (notice the contents of Gson excluded) exclusion policy interface
-
You can specify multiple exclusion policies, more flexible
c) the date of the special type of problem
- Implement an interface
-
- Sign up to GsonBuilder
- Object converted to JSON JS (browser built, Jquery, Ext ..)
browser built-in objects, subsequent recommended Jquery
the JSON.parse (JSONString) JS objects -----
----- JS array
6, Case: Application of Ajax technology used to display user information
AJAX framework Day2
1, SpringMVC support for Ajax
a) Original Integration Integration bottom
b) by @ResponseBody
automatically convert objects into SpringMVC JSON string, and by the character output stream.
Note: @ResponseBody encapsulated object component is converted JSON jackson
environment structures: introducing jackson jar package related
development step;
- The key attribute name is inconsistent with the JSON string
-
- Date format type
@JsonFormat (pattern = "yyyy-MM -dd") - Null type not converted
@JsonInclude (the Include. NON_NULL ) - How to ignore a property
@JsonIgnore - Chinese character sets automatic processing
The original approach
Actual processing based on the automatic date processing null value
2, Struts2 and Ajax technology integration
3, Ajax-based way of thinking about programming:
the current development system in addition to using a ajax ui framework of (easyui ext dojo) program, the rest used by the application, in the development, should use ajax part of the session.
1 local operations interact with the server side
2 is preferably a simple structure