Ajax App ———— local refresh after click

Source: Internet
Author: User

I was just learning struts, so take struts for example

1. First import the following files into your Web project

Commons-beanutils-1.7.0.jar
Commons-collections-3.1.jar
Commons-lang-2.5.jar
Commons-logging-1.1.1.jar
Ezmorph-1.0.3.jar
Json-lib-2.1-jdk15.jar

2. Write a login.jsp example, here I only write body contents

<body>
<script type= "Text/javascript" >
$ (function () {

$ ("#s1"). Click (function () {
/*getjson The following three parameters are the Url,{key:value,key1:value1},callback callback function, JSON is the background through the response obtained by the PrintWriter written over the * *
$.getjson ("<%=basepath%>/authority/test", {name:1}, function (JSON) {
$ ("#s1"). empty ();
For (var. v in JSON) {
$ ("#s1"). Append ("<option>" +json[v]+ "<option>");
}

});


});
<!--Click End-


})
</script>
<!--
this gives a variety of JSON data processing can be used for reference;
$ (function () {
var json = {"name": "Jack", "gender": "Male", "height": 174.00, "Birthday": New Date ()};//define a JSON object
$ ("#b100"). Click (function () {
alert (json.birthday);
});

$ ("#b101"). Click (function () {
for (Var key in JSON) {
Alert (Json[key]);
}
});


var Jsonarray = [{"Program name": "Non-sincerity Not Disturb", "host": "Mengfei", "ratings": 0.2},
{"Program name": "Limit Challenge", "anchor": "Willow", "ratings": 0.1},
{"Program name": "NBA", "host": "Zhou Jianrong", "ratings": 0.3},
{"Program name": "Mother Mia", "host": "Mom", "ratings": 0.9},
{"Program name": "Fancy Sister", "anchor": "Linzhiling", "ratings": 0.1},
{"Program name": "Day Up", "host": "Wang Han", "ratings": 0.15}
];

$ ("#b102"). Click (function () {
/*
for (Var index=0;index<jsonarray.length;index++)
Alert (jsonarray[index]["program name"]);
*/
For (var index in Jsonarray) {
var v= "";
for (var key in Jsonarray[index]) {
v=key+ "=" +jsonarray[index][key]+ "\ T" +V;
}
Alert (v);
}
});

$ ("#b103"). Click (function () {
var jsonstr = ' ({"name": "Rose", "Address": "Jiangsu Wuxi"}) ';
var jsons = eval (jsonstr);
Alert (jsons["name"]);
});
})
-
<select id= "S1" >
</select>
</body>

3.struts Configure yourself (if you are a servlet.) Web. XML also configures itself), the connection of an action, the code is as follows:

public class Testjson extends Actionsupport {

/* (Non-javadoc)
* @see Com.opensymphony.xwork2.actionsupport#execute ()
*/
@Override
Public String Execute () throws Exception {

//If your class is servlet,response can be used directly
HttpServletResponse Response=servletactioncontext.getresponse ();
Response.setcontenttype ("Text/html;charset=utf-8");


//2. Data take map as an example
map<string,string> map = new hashmap<string, string> ();
Map.put ("100", "Washing machine");
Map.put ("101", "washing powder");
Map.put ("102", "refrigerator");
Map.put ("103", "air conditioner");
Map.put ("104", "color TV");
Map.put ("105", "oven");

//3. Converting data to bit JSON, converting to string jsonstr;
String jsonstr = jsonobject.fromobject (map). ToString ();

//4. Data write out
PrintWriter out = Response.getwriter ();
Out.write (JSONSTR);
Out.flush ();
Out.close ();
return Super.execute ();

}

}

Ajax App ———— local refresh after click

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.