For practice purposes, ajax is required to obtain the values in the List set in the background. I have never touched on it before, so I will study it today.
1. Download the jar package dependent on JSON. It depends mainly 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
Ii. Instances.
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 card
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" %>
<Html>
<Head>
<Script type = "text/javascript" src = "$ {pageContext. request. contextPath}/js/jquery-1.7.2.js"> </script>
</Head>
<Body>
<Input type = "button" value = "click I show data" id = "clickMe">
<Table id = "showTable" border = "1">
<Tr>
<Td> name </td>
<Td> ID card </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>
</Html>
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 ("Li Si", "430682198902191112", "X", "verification error ");
ErrorCondition r3 = new ErrorCondition ("Wang Wu", "", "N", "ID card information is blank ");
List <ErrorCondition> list = new ArrayList <ErrorCondition> ();
List. add (r1 );
List. add (r2 );
List. add (r3 );
// Convert list into 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 configuration file and element of 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. Running results
The start page is as follows:
After clicking the button
The returned results are as follows: