ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

用easyui从servlet传递json数据到前端页面的两种方法

2019-09-04 23:50:45  阅读:180  来源: 互联网

标签:easyui title request flag field json servlet response


用easyui从servlet传递json数据到前端页面的两种方法

 

两种方法获取的数据在servlet层传递的方法相同,下面为Servlet中代码,以查询表中所有信息为例。

//重写doGet方法
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
​
    request.setCharacterEncoding("UTF-8");//防止request请求时中文数据出现乱码
    String flag = request.getParameter("flag");//通过flag值判定增删改查操作
    if(flag == null) {
        queryOffer(request,response);
    }else if("add".equals(flag)){
        addOffer(request,response);
    }else if("del".equals(flag)) {
        deleteOffer(request,response);
    }else if("update".equals(flag)) {
        updateOffer(request,response);
    }
}
​
//处理从数据库查询到的数据以返回前端
protected void queryOffer(HttpServletRequest request, HttpServletResponse response) {
    // TODO Auto-generated method stub
    List<Offer> offers = new ArrayList<Offer>();
    offers = offerservice.queryOfferService();
    try {
        String str=JSONArray.toJSONString(offers);//将数据库查询到的集合转换成JSON字符串
        System.out.println(str);
        response.setContentType("text/html;charset=utf-8");//防止response时中文数据乱码
        response.getWriter().print(str);//向前台传递字符串
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

 

  • 通过easyui包含的table标签中的属性来获取后端传递的数据

    jsp代码:

    • url:传递数据的地址(本篇使用的是servlet,所以路径为servlet路径;也可以使用action或者php)

    • field:传递的JSON数据的字段名称,就是数据库的字段(列名)

    <table id="dg" title="用户列表" class="easyui-datagrid" style="width:80%;height:250px"
                url="<%=request.getContextPath() %>/OfferServlet"     
                toolbar="#toolbar"
                rownumbers="true" fitColumns="true" singleSelect="true">
            <thead>
                <tr>
                    <th field="offerid" width="50">商品ID</th>
                    <th field="offername" width="100">商品名称</th>
                    <th field="offertype" width="200">商品类型</th>
                    <th field="offerdesc" width="200">商品描述</th>
                    <th field="price" width="200">商品价格</th>
                </tr>
            </thead>
        </table>

     

  • 通过JS来传递JSON数据到前端

    jsp代码:

    <table id="dg" title="用户列表" class="easyui-datagrid" style="width:1000px;height:250px" toolbar="#toolbar">
    </table>

     

    js代码:

    • title:显示的表格列名

    $(function(){
        $('#dg').datagrid({
            url:"${pageContext.request.contextPath}/OfferServlet",//servlet路径
            columns:[[
                {field:'offerid',title:'商品ID',width:100},
                {field:'offername',title:'商品名称',width:100},
                {field:'offertype',title:'商品类型',width:100},
                {field:'offerdesc',title:'商品描述',width:300},
                {field:'price',title:'商品价格',width:150}
            ]]      
        });
    }); 

     

 

标签:easyui,title,request,flag,field,json,servlet,response
来源: https://www.cnblogs.com/bobozz/p/11462539.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有