Struts2-integrate Json plug-in to implement Ajax
The previous blog introduced the implementation of Ajax in the Custom result set of Struts2, and analyzed its disadvantages: the custom result set is dead, the client does not need to transmit any data as needed; Struts2-custom result set implements ajax
This blog introduces Struts2's Integrated Json plug-in, which effectively solves the problems caused by custom result sets.
I. Introduction to Json Data Format
Because JSON is an ideal data exchange format separated from the language, it is undoubtedly used frequently in the communication between the client and the server. In the communication process between the client and the server, the transmission of JSON data is divided into the transmission of JSON data from the server to the client, and the transmission of JSON data from the client to the server, the core process of the former is to convert the object into JSON, while that of the latter is to convert the JSON into an object, which is essentially different. In addition, it is worth mentioning that during the transmission of JSON data, it is actually to pass a common string that complies with the JSON syntax format, the so-called "JSON object" refers to the result after parsing and packaging the JSON string.
2
, Struts2 returns JSON data to the client
This is the most common requirement. As AJAX is widely used today, it is essential for every WEB application to request JSON data from the server. Aside from Struts2, there are two ways to return JSON data from the server to the client in conventional WEB applications: one is to output the JSON string in the Servlet, the second is to output the JSON string on the JSP page. As mentioned above, when a server returns JSON data like a client, it actually returns a string that complies with the JSON syntax specification. Therefore, the two methods have one thing in common, the data to be returned is packaged as a string that complies with the JSON syntax and displayed on the page.
3
,
Struts
2
Action
Returns in the traditional way
Json
Data
Omitted .....
4
,
Struts
2
Integration
Json
Plug-in, Configuration
Json
Format result set, return
Json
Data
The JSON plug-in is the Ajax plug-in of Structs 2. By using the JSON plug-in, developers can easily and flexibly use Ajax for development.
Json is a lightweight data exchange format. The JSon plug-in provides an Action ResultType named json.
Advantages of using this result set: the transformation of JSON data is handed over to Struts2. Unlike the traditional JSON output in Action, Action only needs to be responsible for business processing, instead of worrying about how the result data is converted to JSON and returned to the client. Through simple xml configuration and jar package reference, Struts2 will help us do better.
2, 1,
4
Implementation steps:
1
, Introduce
Struts
Package,
Struts
And
Json
Integrated
Jar
Package;
Struts-plugin.xml
Configuration File
--
-- Struts-plugin.xml: the information of the integrated Json plug-in is configured (the result set named "json" is defined, and the interceptor named "json"; note: the json plug-in of the specific json type result set and interceptor Struts has been implemented. We only need to configure the implementation class in the configuration file );
2
,
Web
. Xml
-- ConfigurationStruts2 core Filter
Struts2
Org. apache. struts2.dispatcher. ng. filter. StrutsPrepareAndExecuteFilter
Struts2
/*
Index. jsp
3. User class-entity class
Private Long uid; // user idprivate String username; // user name private String sex; // gender/************ get/set Method ******************** * *********************/publicLong getUid () {returnuid;} publicvoid setUid (Long uid) {this. uid = uid;} publicString getUsername () {returnusername;} publicvoid setUsername (String username) {this. username = username;} publicString getSex () {returnsex;} publicvoid setSex (String sex) {this. sex = sex ;}
4
,
UserAction. java
-- Process Business and obtain users
Importcom. opensymphony. xwork2.ActionSupport; public classUserAction extends ActionSupport {privateLong uid; privateString username; privateString password; privateString sex; privateString phone;/*** get user * @ return */publicString showUser () {User user = new User (); // create a User object user. setUid (1L); user. setUsername (" min"); user. setSex ("female"); user. setPassword ("123"); user. setPhone ("15466554589"); this. uid = user. getUid (); this. sex = user. getSex (); this. password = user. getPassword (); this. phone = user. getPhone (); this. username = user. getUsername (); returnSUCCESS ;} /************************************/publicLong getUid () {returnuid;} publicvoid setUid (Long uid) {this. uid = uid;} publicString getUsername () {returnusername;} publicvoid setUsername (String username) {this. username = username;} publicString getPassword () {returnpassword;} publicvoid setPassword (String password) {this. password = password;} publicString getSex () {returnsex;} publicvoid setSex (String sex) {this. sex = sex;} publicString getPhone () {returnphone;} publicvoid setPhone (String phone) {this. phone = phone ;}}
5
, Configure
Strut
2
Configuration File
Struts. xml
--Inherit json-default and specify the type of the result set returned by the Action as json;
(Note:Once this result processing type is specified for the Action, the JSON plug-in will automatically serialize the data in the Action into JSON format and return the data to the client's physical view JavaScript. Simply put, the JSON plug-in allows us to call Action asynchronously in JavaScript, and the Action does not need to specify a view to display the information of the Action. The JSON plug-in is responsible for returning the specific information in the Action to the call page .)
6
,
Test
. Html
Page
Tree.html
<Scriptsrc = "js/jquery-1.4.2.js"> </script> <scriptsrc = "js/test. js"> </script>This is my HTML page.
7
,
Test
. Js
File
// Execute page loading $ (). ready (function () {load (); // call the load () function}); functionload () {$. post ("userJSONAction_showUser.action", null, function (data) {// The data returned by the server is alert ("No.:" + data. uid + ", name:" + data. username + ", Gender:" + data. sex );});}
8. Run
Address: http: // localhost: 8080/Struts2 + Ajax/test.html
Result:
Iii. Sequence diagram of the execution principle of the json plug-in
... To be continued ....
Iv. Summary
Benefits of using the Integrated Json plug-in to implement Ajax:
... To be continued .......