Use of JSON in struts2

Source: Internet
Author: User

First, my class relationships

Entity class:

Class entityclass {

Private int ID;

}

Class model extends entityclass {

Private string username;

Private string password;

}

 

Then in the action

Class modelaction extends actionsupport {

Private Boolean success;

Private model;

// The get method is required to return the JSP page as a JSON object.

// Get method ,,,,

}

 

Action configuration file:

 

<Package name = "model" namespace = "/"
Extends = "JSON-Default">
<Action name = "delete_action" class = "modelaction"
Method = "delete">
<Result name = "delete" type = "JSON">
<! -- Set the data to be returned, such as GetModel, getsuccess, and other get methods in the action. You can use the root attribute to set the sequence. -->
<Param name = "root"> success </param>
<! -- Includeproperties: this parameter directly returns the JSON data of the object. The foreground does not require eval conversion. <Param name = "root"> result </param> is different. The foreground needs eval conversion. -->
<! -- <Param name = "includeproperties"> success </param> -->
<! -- Ignorehierarchy parameter: indicates whether the hierarchy is missing or not, that is, the inheritance relationship,
For example, if testaction is inherited from baseaction, the returned JSON string in testaction does not include the attribute value of the parent class baseaction,
The default value of ignorehierarchy is true. If it is set to false, attributes of the parent class and subclass are returned together.

If this attribute is set to false, IDs in entityclass are also serialized. Otherwise, IDs are not serialized.

-->
<Param name = "ignorehierarchy"> false </param>
<! -- Remove some attributes in the model, which are generally not used together with the includeproperties attribute -->
<! -- <Param name = "excludeproperties"> id </param> -->
</Result>
<Result name = "error" type = "JSON"> </result>
</Action>

 

JSON objects received on the JSP page:

Function postconnect (){
VaR request = false;
Try {
Request = new XMLHttpRequest ();
} Catch (trymicrosoft ){
Try {
Request = new activexobject ("msxml2.xmlhttp ");
} Catch (othermicrosoft ){
Try {
Request = new activexobject ("Microsoft. XMLHTTP ");
} Catch (failed ){
Request = false;
}
}
}

If (! Request ){
Alert ("error initializing XMLHttpRequest! ");
Return false;
}
Return request;
}

Function booleancheck (ID ){
VaR request = postconnect ();
If (! Request ){
Alert ("Access failed ");
}
VaR url = "<% = request. getcontextpath () %>/delete_action? Id = "+ ID;
Request. Open ("get", URL, true );
Request. onreadystatechange = function (){
 
If (request. readystate = 4 ){
If (request. Status >=200 & request. Status <300) | request. Status = 304 ){
VaR OBJ = eval ('+ request. responsetext + '); // The two above if statements must be added; otherwise, the eval method returns multiple data records (including error data ). Request. responsetext is the returned JSON object.
If (OBJ ){
$ ("Tr [ID = '" + ID + "']"). Remove (); // refresh the list here
} Else {
Alert ("deletion failed! ");
}
}
Else {
Alert ("request was unsuccessful:" + request. status );
}
}
};
Request. Send (null );
}

 

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.