Quick mastery of the Ajax-ajax base instance (Ajax returns JSON implementation in Java)

Source: Internet
Author: User

(Turn) example two: Ajax returns JSON implementation in Java

  

Transfer from http://www.cnblogs.com/lsnproj/archive/2012/02/09/2341524.html#2995114

In this article, it is mainly about using JSON to display the data obtained in the background to the foreground page. It can be said that this approach should be the basis for implementing no-refresh paging, and is often used in the development process. The background part here is implemented by Java.

This example is also implemented in the previous article in the project. Create a new secondtest.html page, define a button, and bind the event Ajaxjson () to this button. In the implementation of Ajax in JS, here Ajax compared to the previous article, I made a simple package, the original code into three methods, namely create (), callback (), run ().

The Create method is used for creating the XMLHTTP object, and callback is used to implement the callback function, which is the core method. The specific code is as follows:

Create ():

View Code
function Create () {                 if (window. XMLHttpRequest) {                     xmlhttp=new XMLHttpRequest ();                 } if (window. ActiveXObject) {                     xmlhttp=new ActiveXObject ("Microsoft.XMLHTTP")}}   

Callback ():

View Code
function callback () {                 if (xmlhttp.readystate==4) {                     if (xmlhttp.status==200) {                         // action to be implemented }else{alert ("The AJAX server returned an error! "); } } }

Run ():

View Code
function run (URL) {                 create ();                 Xmlhttp.open ("POST", url,true);                 Xmlhttp.onreadystatechange=callback;                 Xmlhttp.send ();             }

When the above three methods are written, the Run method is called directly in the Ajaxjson () method and the request is passed in as a parameter. The code is as follows:

View Code
function Ajaxjson () {                 run ("test.do?method=jsontest&&msg=" +New Date ());             }

In the Ajaxcontroller class, to create a new Jsontest method, the implementation of JSON in Java requires a JSON rack package, the JSON rack package is: Json-lib-2.3-jdk15.jar,ezmorph-1[1].0.6.jar, Commons-logging-tests.jar,commons-logging-api-1.1.1.jar,commons-logging-adapters-1.1.1.jar,commons-logging-1.1.1-sources . jar,commons-logging-1.1.1-javadoc.jar,commons-logging-1.1.1.jar,commons-lang.jar,commons-collections-3[1]. 2.1.jar,commons-beanutils-core.jar,commons-beanutils-bean-collections.jar,commons-beanutils.jar. Look at a bit Doha, you can go to csdn above to find, I tried to delete some, but found that less will not be so, so in order to insure or put these all add in it.

In the development process, the general data are taken out of the database, the habitual we will be removed in the program to the list of data, and JSON rack package is just the right way to convert the list to JSON. In this example we create some fake data in the list, then convert the list to JSON and then back to the foreground. The code is as follows:

View Code
Public Actionforward jsontest (actionmapping mapping, actionform form, httpservletrequest request, HTTPSERVLETR Esponse response)Throws Exception {//Make fake dataArraylist<usermodel> list=New Arraylist<usermodel> (); Usermodel user1=New Usermodel ();//User Object 1 User1.setuserid (1); User1.setusername ("haha"); User1.setusersex ("male"); List.add (user1) ; Usermodel user2=new Usermodel ();  user object 2 User2.setuserid (2); User2.setusername ("hehe"); User2.setusersex ("female"); List.add (User2); // convert list to Json// set encoding // write to foreground return null;}       

The background part is finished, now the foreground secondtest.html page needs to receive the background feedback the data, this time needs to receive the JSON data in the callback function. The JS code is as follows:

View Code
callback function          callback () {             if (xmlhttp.readystate==4) {                 if (xmlhttp.status==200) {                     var Xmldoc=xmlhttp.responsetext;                     var data=eval (xmldoc);                     Alert (data[0].userid+ "," +data[0].username+ "," +data[0].usersex ");                     Alert (data[1].userid+ "," +data[1].username+ "," +data[1].usersex ");                 else{alert ("The AJAX server returned an error! "); } } }

This completes the implementation of the AJAX return JSON in Java.

  
     

  

Quick mastery of the Ajax-ajax base instance (Ajax returns JSON implementation in Java)

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.