如
List list=查询的方法;
然后用request对象传过去,再在界面上这样写:
<table>
<c:forEach id="aa" name="list">
<tr><td>${aa.对象}</td><td>${aa.对象}</</td></tr>
</table>
</c:forEach>
这样就可以显示所以数据。
ajax本身是无法取得后台的list对象的~只能取基本类型的值~
可以这样试试:
1、首先下面这几个包要存在:
commons-beanutils.jar
commons-collections.jar
commons-lang.jar
ezmorph-1.0.6.jar
json-lib-2.2.3-jdk15.jar
2、然后改后台代码,转换为基本类型的值,传到前台:
String json = JSONArray.fromObject(list).toString()
out.print(json)
3、jsp页面上的js:
$.getJSON("test.action",function(data)
{
for(var i = 0i <data.lengthi++)
{
var id = data[i].id
var name = data[i].name
}
})
using Systemusing System.Collections.Generic
using System.Linq
using System.Text
using System.Data
using System.Collections
using System.Reflection
namespace DatableToList
{
class ConvertHelper<T>where T : new()
{
/// <summary>
/// 利用反射和泛型
/// </summary>
/// <param name="<a href="https://www.baidu.com/s?wd=dt&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1YLmWb4PA7Bm1TknycYnHTY0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3Erj04P1D3rjDzP10sPHT3PjRz" target="_blank" class="baidu-highlight">dt</a>"></param>
/// <returns></returns>
public static List<T>ConvertToList(DataTable <a href="https://www.baidu.com/s?wd=dt&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1YLmWb4PA7Bm1TknycYnHTY0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3Erj04P1D3rjDzP10sPHT3PjRz" target="_blank" class="baidu-highlight">dt</a>)
{
// 定义集合
List<T><a href="https://www.baidu.com/s?wd=ts&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1YLmWb4PA7Bm1TknycYnHTY0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3Erj04P1D3rjDzP10sPHT3PjRz" target="_blank" class="baidu-highlight">ts</a>= new List<T>()
// 获得此模型的类型
Type type = typeof(T)
//定义一个临时变量
string tempName = string.Empty
//遍历DataTable中所有的数据行
foreach (DataRow dr in <a href="https://www.baidu.com/s?wd=dt&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1YLmWb4PA7Bm1TknycYnHTY0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3Erj04P1D3rjDzP10sPHT3PjRz" target="_blank" class="baidu-highlight">dt</a>.Rows)
{
T t = new T()
// 获得此模型的公共属性
PropertyInfo[] propertys = t.GetType().GetProperties()
//遍历该对象的所有属性
foreach (PropertyInfo pi in propertys)
{
tempName = pi.Name//将属性名称赋值给临时变量
//检查DataTable是否包含此列(列名==对象的属性名)
if (dt.Columns.Contains(tempName))
{
// 判断此属性是否有Setter
if (!pi.CanWrite) continue//该属性不可写,直接跳出
//取值
object value = dr[tempName]
//如果非空,则赋给对象的属性
if (value != DBNull.Value)
pi.SetValue(t, value, null)
}
}
//对象添加到泛型集合中
<a href="https://www.baidu.com/s?wd=ts&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1YLmWb4PA7Bm1TknycYnHTY0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3Erj04P1D3rjDzP10sPHT3PjRz" target="_blank" class="baidu-highlight">ts</a>.Add(t)
}
return <a href="https://www.baidu.com/s?wd=ts&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1YLmWb4PA7Bm1TknycYnHTY0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3Erj04P1D3rjDzP10sPHT3PjRz" target="_blank" class="baidu-highlight">ts</a>
}
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)