国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

使用easyui從servlet傳遞json數據到前端頁面的兩種方法

2024-05-06 15:34:56
字體:
來源:轉載
供稿:網友

 兩種方法獲取的數據在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}    ]]     });});            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 和平县| 龙游县| 长顺县| 永年县| 当涂县| 虎林市| 务川| 腾冲县| 河南省| 抚州市| 琼海市| 西和县| 横峰县| 印江| 木兰县| 奉化市| 宁国市| 公主岭市| 福建省| 南木林县| 赤壁市| 库伦旗| 乃东县| 泉州市| 忻州市| 内江市| 红桥区| 利津县| 从江县| 嘉黎县| 昭通市| 定日县| 商城县| 天柱县| 汉寿县| 监利县| 绍兴县| 渝北区| 信丰县| 安陆市| 上思县|