ajax+json+Struts2实现list传递实例讲解
程序员文章站
2023-11-12 15:20:58
由于实习需要,需要通过ajax来获取后台的list集合里面的值。由于前面没有接触过,所以今天就来研究下了。 一、首先需要下载json依赖的jar包。它主要是依赖如下: js...
由于实习需要,需要通过ajax来获取后台的list集合里面的值。由于前面没有接触过,所以今天就来研究下了。
一、首先需要下载json依赖的jar包。它主要是依赖如下:
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
二、实例。
1、身份证错误信息bean类(errorcondition.java)
/**
*@project: excel
*@author: chenssy
*@date: 2013-4-4
*@copyright: chenssy all rights reserved.
*/
public class errorcondition {
private string name; // 姓名
private string idcard; // 身份证
private string status; // 错误状态
private string 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页面(index.jsp)
<%@ 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="点我显示数据" id="clickme">
<table id="showtable" border="1">
<tr>
<td>姓名</td>
<td>身份证</td>
<td>错误状态</td>
<td>错误信息</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处理类(jsontest_01.java)
/**
*@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("张三", "4306821989021611", "l", "长度错误");
errorcondition r2 = new errorcondition("李四", "430682198902191112","x", "校验错误");
errorcondition r3 = new errorcondition("王五", "", "n", "身份证信息为空");
list<errorcondition> list = new arraylist<errorcondition>();
list.add(r1);
list.add(r2);
list.add(r3);
//将list转化成json对象
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配置
<?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">
<!-- 指定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>
三、运行结果
开始页面如下:
一、首先需要下载json依赖的jar包。它主要是依赖如下:
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
二、实例。
1、身份证错误信息bean类(errorcondition.java)
复制代码 代码如下:
/**
*@project: excel
*@author: chenssy
*@date: 2013-4-4
*@copyright: chenssy all rights reserved.
*/
public class errorcondition {
private string name; // 姓名
private string idcard; // 身份证
private string status; // 错误状态
private string 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页面(index.jsp)
复制代码 代码如下:
<%@ 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="点我显示数据" id="clickme">
<table id="showtable" border="1">
<tr>
<td>姓名</td>
<td>身份证</td>
<td>错误状态</td>
<td>错误信息</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处理类(jsontest_01.java)
复制代码 代码如下:
/**
*@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("张三", "4306821989021611", "l", "长度错误");
errorcondition r2 = new errorcondition("李四", "430682198902191112","x", "校验错误");
errorcondition r3 = new errorcondition("王五", "", "n", "身份证信息为空");
list<errorcondition> list = new arraylist<errorcondition>();
list.add(r1);
list.add(r2);
list.add(r3);
//将list转化成json对象
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配置
复制代码 代码如下:
<?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">
<!-- 指定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>
三、运行结果
开始页面如下:
当点击按钮后
返回的结果如下: