JSON (JavaScript Object Notation) is a lightweight data interchange format, which is based on a subset of JavaScript, often used with ajax, using ajax to load data.
json ajax end of the configuration steps:
1. Guide package. Using json jar package of support needed, use the jar before the first import the following package: commons-beanutils-1.8.0.jar, commons-collections-3.2.1.jar (who did not import this package as a waste One morning my time to check for errors, Khan), commons-lang-2.4.jar, commons-logging-1.1.jar, ezmorph-1.0.6.jar, json-lib-2.3-jdk15.jar, jsonplugin-0.32 . jar;
2. Add JsonResultType class.
3. Configuration struts.xml profile. In the case of no use to use json, struts.xml the package configuration file inheritance is "struts-default" that is: extens = "struts-default", but when using the json inheritance no longer struts-default, but "json-default", open jsonplugin-0.32.jar under the struts-plugin.xml file you can see, the package has inherited the "struts-default". in the package by adding the following code
<result-types>
<Result-type name = "json"
"JsonResultType where the package">
</ Result-type>
</ Result-types>
Action in the appropriate configuration to do the following,
<action name="jsonAction""action Package ">
<result name="json" type="json">
<param name="name"> json </ param>
</ Result>
</ Action>
4. To write action code, (note the following code may not be prepared for any errors, please indicate)
private String json; / / need to get / set methods
/ / Get the collection may not necessarily be a collection of city
List list = this.service.method ();
/ / Filters do not need to convert the property
JsonConfig con = new JsonConfig ();
con.setExcludes (new String [] ("filter properties"));
/ / The list into JSONArray
JSONArray array = new JSONArray ();
/ / The array into a String
this.json = array.toString ();
/ / Is the action in the name struts.xml
return "json";
5.javascript call
<script type="text/javascript">
function method () (
/ / Request Action
var url = "jsonInfo / jsonAction! showCity.action";
/ / Ajax call
var ajax = new Ajax.Request (url, (
method: 'post',
onComplete: function (data) (
/ / Here for the city is a string, according to the corresponding conversion needs to be done
var city = JSON.parse (data.responseText);
)
));
)
</ Script>
json ajax end of the configuration steps:
1. Guide package. Using json jar package of support needed, use the jar before the first import the following package: commons-beanutils-1.8.0.jar, commons-collections-3.2.1.jar (who did not import this package as a waste One morning my time to check for errors, Khan), commons-lang-2.4.jar, commons-logging-1.1.jar, ezmorph-1.0.6.jar, json-lib-2.3-jdk15.jar, jsonplugin-0.32 . jar;
2. Add JsonResultType class.
3. Configuration struts.xml profile. In the case of no use to use json, struts.xml the package configuration file inheritance is "struts-default" that is: extens = "struts-default", but when using the json inheritance no longer struts-default, but "json-default", open jsonplugin-0.32.jar under the struts-plugin.xml file you can see, the package has inherited the "struts-default". in the package by adding the following code
<result-types>
<Result-type name = "json"
"JsonResultType where the package">
</ Result-type>
</ Result-types>
Action in the appropriate configuration to do the following,
<action name="jsonAction""action Package ">
<result name="json" type="json">
<param name="name"> json </ param>
</ Result>
</ Action>
4. To write action code, (note the following code may not be prepared for any errors, please indicate)
private String json; / / need to get / set methods
/ / Get the collection may not necessarily be a collection of city
List list = this.service.method ();
/ / Filters do not need to convert the property
JsonConfig con = new JsonConfig ();
con.setExcludes (new String [] ("filter properties"));
/ / The list into JSONArray
JSONArray array = new JSONArray ();
/ / The array into a String
this.json = array.toString ();
/ / Is the action in the name struts.xml
return "json";
5.javascript call
<script type="text/javascript">
function method () (
/ / Request Action
var url = "jsonInfo / jsonAction! showCity.action";
/ / Ajax call
var ajax = new Ajax.Request (url, (
method: 'post',
onComplete: function (data) (
/ / Here for the city is a string, according to the corresponding conversion needs to be done
var city = JSON.parse (data.responseText);
)
));
)
</ Script>