這篇文章給大家分享的是有關(guān)如何使用easyui從servlet傳遞json數(shù)據(jù)到前端頁面的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
創(chuàng)新互聯(lián)服務(wù)項目包括白山網(wǎng)站建設(shè)、白山網(wǎng)站制作、白山網(wǎng)頁制作以及白山網(wǎng)絡(luò)營銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢、行業(yè)經(jīng)驗、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機(jī)構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,白山網(wǎng)站推廣取得了明顯的社會效益與經(jīng)濟(jì)效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到白山省份的部分城市,未來相信會繼續(xù)擴(kuò)大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!
兩種方法獲取的數(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ù)據(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ù)據(jù)亂碼 response.getWriter().print(str);//向前臺傳遞字符串 } 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">商品價格</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:'商品價格',width:150} ]] }); });
感謝各位的閱讀!關(guān)于“如何使用easyui從servlet傳遞json數(shù)據(jù)到前端頁面”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
網(wǎng)站名稱:如何使用easyui從servlet傳遞json數(shù)據(jù)到前端頁面
文章源于:http://chinadenli.net/article20/gdopco.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供建站公司、、域名注冊、搜索引擎優(yōu)化、微信小程序、移動網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)