如题     后台返回json数据  前台js进行接收 拼接jsp页面   有的发[email protected]谢谢!

解决方案 »

  1.   

    在struts.xml里面配置:
    <result type="json" name="success">
    <param name="includeProperties">success,msg</param>
    <param name="excludeNullProperties">true</param>
    <param name="ignoreHierarchy">false</param>  
    </result>
    在action的属性里面有success和msg属性,jsp里面能调用
      

  2.   

    public String list() {
    //获取分页参数
    HttpServletRequest request = ServletActionContext.getRequest();
    //判断page rows 是否为空
    if (request.getParameter("page") == null || request.getParameter("rows") == null) {
    return JSON;
    }
    int pageno = Integer.parseInt(request.getParameter("page"));
    int rowsCount = Integer.parseInt(request.getParameter("rows"));
    //设置分页
    page = new Page<File>(rowsCount);
    page.setPageNo(pageno);
    //获取数据
    try {
    page = fileManagerImpl.searchFile(page); } catch (Exception e) {
    e.printStackTrace();
    } this.total = page.getTotalCount();
    List<File> list = page.getResult();
    //System.out.println(list.size() + "-->>文件条数");
    rows = JSONArray.fromObject(list, config);
    //System.out.println(rows.size() + rows.toString());
    return JSON;
    }
      

  3.   

    我在jsp  改怎么来获取数据呢
      

  4.   

    js已经通过ajax获取都数据了,可以通过js操作节点进行数据的展示
      

  5.   

    你用Ajax发送的请求,在回调函数中会有一个参数Object对象,通过obj.xxx既可以获取到对用的属性值了,如果某个属性值是List,可以用obj.xxx[i] (i=0,1,2...)获取对应的值。
      

  6.   

    /**
     * struts2 响应  ajax 请求,Json格式的
     *
     */
    public class AjaxPrintUtil {
    //编码格式
    private static String encoding="gbk";
    /**
     * 向客户端输出字符串
     * @param str
     * @throws IOException
     */
    public  static final void printString(String str) throws IOException
    {
    HttpServletResponse response = ServletActionContext.getResponse();
    response.setContentType("text/html;charset="+encoding);
    PrintWriter writer = response.getWriter();
    writer.print(str);
    writer.flush();
    writer.close();
    }
    /**
     * 向客户端输出对象
     * @param o
     * @throws IOException
     */
    public static final void printObject(Object o) throws IOException
    {
    HttpServletResponse response = ServletActionContext.getResponse();
    response.setContentType("text/html;charset="+encoding);
    JSONObject obj=JSONObject.fromObject(o);
    PrintWriter writer = response.getWriter();
    writer.print(obj.toString());
    writer.flush();
    writer.close();
    }
    /**
     * 向客户端输出集合
     * @param list
     * @throws IOException
     */
    @SuppressWarnings("unchecked")
    public static final void   printList(List list) throws IOException {
    HttpServletResponse response = ServletActionContext.getResponse();
    response.setContentType("text/html;charset="+encoding);
    JSONArray jsArray=JSONArray.fromObject(list);
    PrintWriter writer = response.getWriter();
    writer.print(jsArray);
    writer.flush();
    writer.close();
    }
    }Action调用这个方法会以json格式输出到页面
    AjaxPrintUtil.printList(list);//集合
    AjaxPrintUtil.printObject(object);//对象
    AjaxPrintUtil.printString(object);//集合页面接收json
    function aa(){
      $.post(url,function(msg){
           var Chats = eval("("+msg+")");//回调的数据
             for()//遍历下就好了
        })
    }