AJAX+JSON+STRUTS2 Implement list Pass instance to explain _JSP programming

Source: Internet
Author: User
Tags reserved
Because of internship needs, we need Ajax to get the values in the list set in the background. Since 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 Error Information Bean class (Errorcondition.java)
Copy Code code 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 Code code 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= "dot I show data" id= "ClickMe" >
<table id= "showtable" border= "1" >
<tr>
<td> name </td>
<td> ID </td>
<td> Error Status </td>
<td> error Message </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 Code code 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 Er Rorcondition ("John", "4306821989021611", "L", "Length error");
errorcondition r2 = new errorcondition ("Dick", "430682198902191112", "X", "checksum error");
errorcondition r3 = new Errorcondition ("Harry", "" "," N "," Identity card 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 Code code 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" >
<!--Specifies the element--> 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 the button is clicked

The results returned are as follows:

Related Article

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.