日期:2014-05-17  浏览次数:20817 次

在action中有接受一个list集合 用json 装起来了 怎么在jsp中遍历请看代码
Action 代码
//查询出页面所需数据
public String findAll(){
//此处需要servlet的打印流和相应对象
HttpServletResponse response = ServletActionContext.getResponse();
//设置相应对象
response.setContentType("text/html");
//设置编码
response.setCharacterEncoding("UTF-8");
if(pr!=null){
try {
String pname = new String(pr.getPname().getBytes("ISO-8859-1"),"UTF-8");
String manufact=new String(pr.getManufact().getBytes("ISO-8859-1"),"UTF-8");
String price = new String(pr.getPrice().getBytes("ISO-8859-1"),"UTF-8");
String model=new String(pr.getModal().getBytes("ISO-8859-1"),"UTF-8");
System.out.println(pname+":"+manufact+":"+price+":"+model);
pr.setManufact(manufact);
pr.setModal(model);
pr.setPname(pname);
pr.setPrice(price);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

//执行dao方法获取数据
List<Product> li = new ProductDao().findAll(page,pr); 
System.out.println(li.toString());
//new 出一个json对象
JSONObject j = new JSONObject();
for(int i=0;i<li.size();i++){
JSONObject jn = new JSONObject();
//添加json格式
jn.put("id", li.get(i).getId());
jn.put("pname", li.get(i).getPname());
jn.put("modal", li.get(i).getModal());
jn.put("price", li.get(i).getPrice()); 
jn.put("manufact", li.get(i).getManufact());
jn.put("stockNum", li.get(i).getStockNum());
jn.put("inDate",String.valueOf(li.get(i).getInDate()));
j.put(i, jn);
}
response.setCharacterEncoding("UTF-8");
try {
PrintWriter out = response.getWriter();
out.print(j);
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("0000000000000000000000000");
return null;
}


JSP 


<table id="selectable1">
<tr class="ui-widget-content">
<th>序号</th>
<th>商品名</th>
<th>型号</th>
<th>价格</th>
<th>生产厂家</th>
<th>库存数量</th>
<th>入库时间</th>
</tr>
</table>
<table id="selectable">
<s:iterator value="#li" var="it">
<tr><td><s:property value="#it.id"/></td><td><s:property value="#it.pname"/></td><td><s:property value="#it.modal"/></td><td><s:property value="#it.price"/></td><td><s:property value="#it.manufact"/></td><td><s:property value="#it.stockNum"/></td><td><s:property value="#it.inDate"/></td></tr>
</s:iterator>
</table>
</div>

JSP 不知道怎么弄了。。急!!

------解决方案--------------------
个人认为:你这个不需用json封装数据。如果你直接用struts2,定义一个list,action中有这个list,getter方法。jsp就可以直接struts2标签读取数据。
------解决方案--------------------
josn 的例子,自己看吧
HTML code
    function testxJsonAjax()
    {    
        
        var url ="<%=urlroot%>/criterion.do?method=josns&bmid