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  不知道怎么弄了。。急!!

解决方案 »

  1.   

    个人认为:你这个不需用json封装数据。如果你直接用struts2,定义一个list,action中有这个list,getter方法。jsp就可以直接struts2标签读取数据。
      

  2.   

    josn 的例子,自己看吧
    function testxJsonAjax()
    {

    var url ="<%=urlroot%>/criterion.do?method=josns&bmid="+document.getElementById('bmid').value;
    new Ajax.Request(url,{onComplete:showResponsesx});
    }

    function showResponsesx(xmlHttp)
    {
    if(xmlHttp.readyState==4)
    {
    if(xmlHttp.status==200)
    {
    var result = xmlHttp.responseText;

    var str = eval("("+result+")");

    while(document.all.bzid.options.length>0)
    {
    document.all.bzid.options.remove(0);  
    }
    var objSelect = document.getElementById('bzid');
    var objOption = new Option();

    for(var i=0;i<str.users.length;i++)
    {
    var objSelect = document.getElementById('bzid');
    var objOption = new Option();
    objOption.text= str.users[i].name;
    objOption.value= str.users[i].id;
    objSelect.options.add(objOption);



    }
    }
    }
    } public void josns(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response){
    response.setContentType("text/html");


    PrintWriter out;

    try {
    out = response.getWriter();
    CommonDAO cmdao=new CommonDAO(ds);
    String bmid=request.getParameter("bmid");
    ArrayList<Item_Common> elementlist=cmdao.grouplist ( "", bmid , request );
    JSONArray array = new JSONArray();
    if(elementlist.size()>0){
    for (int i = 0; i < elementlist.size(); i++) {
    JSONObject object = new JSONObject();

    try {
    object.put("id", elementlist.get(i).getGetid());
    object.put("name", elementlist.get(i).getGetname());
    array.put(object);
    } catch (JSONException e) {
    e.printStackTrace();
    }
    }
    }
    JSONObject object = new JSONObject();
    try {
    object.put("users", array);
    } catch (JSONException e) {
    e.printStackTrace();
    }
    out.print(object.toString());
    out.flush();
    out.close();

    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
      

  3.   


    现在我访问action都出错了。。他不知道跳转到显示页面纠结
      

  4.   


    我的程序走到 List<Product> li = new ProductDao().findAll(page,pr);  
    System.out.println(li.toString());   就不走了。503  505 错误。
      

  5.   

    没明白楼主为什么要把list转json,直接用list,然后用Struts2的标签不就能解析出来了吗
      

  6.   

    后面我要做一个帅选数据的操作,,通过ajax 传到action里面来。。要用到json     而且方法还是findAll那我是不是该这样做呢??
      

  7.   

    现在我已经写好了组装JSON 的操作了。。    //查询出数据库有几页
     var my=function(){
        $.ajax({
               url:"pr_pageSum",
               dataType:"json",
               type:"post",
               success:function(sum){
                       i=sum;
                       $("#s").text(sum);
                       show(i);
    }});
     };
         //查询所有
    var show=function(i){
    $("#s").text(i);
    var page = $("#p").text();
    $.ajax({
              url:"pr_findAll",
              type:"post",
              data:{page:page},
              dataType:"json",
              success:function(data){
    var html="";//用来组装HTML元素
    var i=0;//用来存放序号
    $.each(data,function(key,value){
    i++; 
    $.each(value,function(k,v){//第二步是取得每个对象的属性值!
    if(k=="id")
    html+="<tr class='ui-widget-content' id="+v+"><th>"+i+"</th>";
    if(k=="pname")
          html+="<th>"+v+"</th>"; 
    if(k=="modal")
    html+="<th>"+v+"</th>";
    if(k=="price")
    html+="<th>"+v+"</th>";
        if(k=="stockNum")
         html+="<th>"+v+"</th>";
        if(k=="manufact")
         html+="<th>"+v+"</th>";
    if(k=="inDate")
    html+="<th>"+v+"</th><tr/>";
    });
    });
    $("#selectable").html(html);
            }
    });
    };
    但是还是显示不出来数据,,查是查到了数据