使用easyui從servlet傳遞json數(shù)據(jù)到前端頁面的兩種方法
兩種方法獲取的數(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標簽中的屬性來獲取后端傳遞的數(shù)據(jù)
jsp代碼:
url:傳遞數(shù)據(jù)的地址(本篇使用的是servlet,所以路徑為servlet路徑;也可以使用action或者php)
field:傳遞的JSON數(shù)據(jù)的字段名稱,就是數(shù)據(jù)庫的字段(列名)
<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數(shù)據(jù)到前端
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}
]]
});
});
總結(jié)
以上所述是小編給大家介紹的用easyui從servlet傳遞json數(shù)據(jù)到前端頁面的兩種方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!
相關(guān)文章
jquery.tableSort.js表格排序插件使用方法詳解
這篇文章主要為大家詳細介紹了jquery.tableSort.js表格排序插件使用方法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-02-02
JQuery擴展插件Validate—4設(shè)置錯誤提示的樣式
JQuery擴展插件Validate—4設(shè)置錯誤提示的樣式,使用Validate的朋友可以參考下。2011-09-09
基于jQuery中ajax的相關(guān)方法匯總(必看篇)
下面小編就為大家?guī)硪黄诨趈Query中ajax的相關(guān)方法匯總。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-11-11
基于jquery的不規(guī)則矩形的排列實現(xiàn)代碼
現(xiàn)在很多網(wǎng)站都用不規(guī)則矩形來羅列圖片,ipad上面很多應(yīng)該用也都是用的不規(guī)則的矩形,但是還要讓他們各自都靠近排列,不能有空隙2012-04-04
解決jquery submit()提交表單提示:f[s] is not a&nb
jquery submit()無法提交表單 報錯:f[s] is not a function,很是疑惑搜集整理了一些解決方法,感興趣的朋友可以了解下啊,希望本文對你有所幫助2013-01-01

