溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

如何使用easyui從servlet傳遞json數(shù)據(jù)到前端頁面

發(fā)布時(shí)間:2021-07-20 10:53:31 來源:億速云 閱讀:123 作者:小新 欄目:web開發(fā)

這篇文章給大家分享的是有關(guān)如何使用easyui從servlet傳遞json數(shù)據(jù)到前端頁面的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過來看看吧。

 兩種方法獲取的數(shù)據(jù)在servlet層傳遞的方法相同,下面為Servlet中代碼,以查詢表中所有信息為例。

//重寫doGet方法
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  // TODO Auto-generated method stub

  request.setCharacterEncoding("UTF-8");//防止request請求時(shí)中文數(shù)據(jù)出現(xiàn)亂碼
  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);
  }
}

//處理從數(shù)據(jù)庫查詢到的數(shù)據(jù)以返回前端
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);//將數(shù)據(jù)庫查詢到的集合轉(zhuǎn)換成JSON字符串
    System.out.println(str);
    response.setContentType("text/html;charset=utf-8");//防止response時(shí)中文數(shù)據(jù)亂碼
    response.getWriter().print(str);//向前臺(tái)傳遞字符串
  } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
  }
}

通過easyui包含的table標(biāo)簽中的屬性來獲取后端傳遞的數(shù)據(jù)

jsp代碼:

url:傳遞數(shù)據(jù)的地址(本篇使用的是servlet,所以路徑為servlet路徑;也可以使用action或者php)

field:傳遞的JSON數(shù)據(jù)的字段名稱,就是數(shù)據(jù)庫的字段(列名)

<table id="dg" title="用戶列表" class="easyui-datagrid" 
      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">商品價(jià)格</th>
      </tr>
    </thead>
  </table>

通過JS來傳遞JSON數(shù)據(jù)到前端

jsp代碼:

<table id="dg" title="用戶列表" class="easyui-datagrid"  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:'商品價(jià)格',width:150}
    ]]   
  });
});

感謝各位的閱讀!關(guān)于“如何使用easyui從servlet傳遞json數(shù)據(jù)到前端頁面”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI