AJAX+JSON+STRUTS2 Implementing list Delivery examples

Source: Internet
Author: User

Because of the internship needs, Ajax is required to get the value inside the list collection in the background. Because there is no contact before, so today to study the next.
first, you need to download the JSON-dependent jar package. It is mainly dependent on the following :
Json-lib-2.2.2-jdk15
ezmorph-1.0.4
commons-logging-1.0.4
commons-lang-2.4
commons-collections-3.2.1
Commons-beanutils
Second, the example .
1. ID card error message Bean class (Errorcondition.java)

Copy CodeThe code is as follows:
/**
* @Project: Excel
* @Author: Chenssy
* @Date: 2013-4-4
* @Copyright: Chenssy All rights reserved.
*/
public class Errorcondition {
private String name; Name
Private String Idcard; Id
Private String status; Error status
Private String message; Error message
Errorcondition (String name,string idcard,string status,string message) {
THIS.name = name;
This.idcard = Idcard;
This.status = status;
this.message = message;
}
Public String GetName () {
return name;
}
public void SetName (String name) {
THIS.name = name;
}
Public String Getidcard () {
return idcard;
}
public void Setidcard (String idcard) {
This.idcard = Idcard;
}
Public String GetStatus () {
return status;
}
public void SetStatus (String status) {
This.status = status;
}
Public String GetMessage () {
return message;
}
public void Setmessage (String message) {
this.message = message;
}
}


2. JSP page (index.jsp)

Copy CodeThe code is as follows:
<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%>
<script type= "Text/javascript" src= "${pagecontext.request.contextpath}/js/jquery-1.7.2.js" ></script>
<body>
<input type= "button" value= "point I show Data" id= "ClickMe" >
<table id= "showtable" border= "1" >
<tr>
<td> name </td>
<td> ID Card </td>
<td> Error Status </td>
<td> error Messages </td>
</tr>
</table>
<script>
$ ("#clickMe"). Click (function () {
var url = "Json/jsontest.action";
$.ajax ({
Type: ' Get ',
Url:url,
DataType: ' JSON ',
Success:function (data) {
$.each (Data,function (i,list) {
var _tr = $ ("<tr><td>" +list.name+ "</td><td>" +
List.idcard+ "</td><td>" +list.status+
"</td><td>" +list.message+ "</td></tr>");
$ ("#showTable"). Append (_TR);
})
}
})
})
</script>
</body>


3. Action processing class (Jsontest_01.java)

Copy CodeThe code is as follows:
/**
* @Project: Jsontest
* @Author: Chenssy
* @Date: 2013-4-5
* @Copyright: Chenssy All rights reserved.
*/
public class Jsontest_01 {
Public String Execute () throws ioexception{
errorcondition r1 = new Errorcondition ("Zhang San", "4306821989021611", "L", "Length error");
errorcondition r2 = new errorcondition ("John Doe", "430682198902191112", "X", "checksum error");
errorcondition r3 = new Errorcondition ("Harry", "" "," N "," ID information is empty ");
list<errorcondition> list = new arraylist<errorcondition> ();
List.add (R1);
List.add (R2);
List.add (R3);
Convert list to JSON object
Jsonarray Jsonarray = jsonarray.fromobject (list);
HttpServletResponse response = (httpservletresponse) actioncontext.getcontext (). Get (Servletactioncontext.http_ RESPONSE);
Response.setcharacterencoding ("UTF-8");
Response.getwriter (). print (Jsonarray);
return null;
}
}


4. Struts.xml Configuration

Copy CodeThe code is as follows:
<?xml version= "1.0" encoding= "GBK"?>
<! DOCTYPE Struts Public
"-//apache software foundation//dtd Struts Configuration 2.1.7//en"
"Http://struts.apache.org/dtds/struts-2.1.7.dtd" >
<!--Specify the elements of the configuration file for Struts 2--
<struts>
<package name= "JSON" namespace= "/json" extends= "Struts-default" >
<action name= "Jsontest" class= "com.json.action.JsonTest_01" method= "Execute" ></action>
</package>
</struts>


iii. Results of Operation
The start page is as follows:

When you click the button

The results returned are as follows:

AJAX+JSON+STRUTS2 Implementing list Delivery examples

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.