java jquery_jQuery数据表和Java集成
生活随笔
收集整理的這篇文章主要介紹了
java jquery_jQuery数据表和Java集成
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
java jquery
- jQuery DataTables是一個開放源代碼插件,用于在瀏覽器中創建表。
- 它具有許多功能,例如排序,服務器端處理, JQUERY UI主題滾動。
- 該插件的下載鏈接:
http://www.datatables.net/download/
- 在本演示中,我們展示了數據表與Java的集成。數據表將通過調用Ajax來加載所有數據來加載數據。
- 響應數據必須包含“ aaData”屬性。
- 該演示的主要組件是:
項目結構:
與Java集成:
- 靜態數據StudentDataService.java,
- 數據表對象將作為響應傳遞到Servlet DataTableObject.java中,
- package com.sandeep.data.object;import java.util.List;public class DataTableObject {int iTotalRecords;int iTotalDisplayRecords;String sEcho;String sColumns;List<student> aaData;public int getiTotalRecords() {return iTotalRecords;}public void setiTotalRecords(int iTotalRecords) {this.iTotalRecords = iTotalRecords;}public int getiTotalDisplayRecords() {return iTotalDisplayRecords;}public void setiTotalDisplayRecords(int iTotalDisplayRecords) {this.iTotalDisplayRecords = iTotalDisplayRecords;}public String getsEcho() {return sEcho;}public void setsEcho(String sEcho) {this.sEcho = sEcho;}public String getsColumns() {return sColumns;}public void setsColumns(String sColumns) {this.sColumns = sColumns;}public List<student> getAaData() {return aaData;}public void setAaData(List<student> aaData) {this.aaData = aaData;}}
- 返回JSON作為String StudentDataServlet.java的Servlet ,
- package com.sandeep.json.data.servlet;import java.io.IOException; import java.io.PrintWriter; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.sandeep.data.object.DataTableObject; import com.sandeep.data.object.Student; import com.sandeep.data.service.StudentDataService;public class StudentDataServlet extends HttpServlet {private static final long serialVersionUID = 1L;public StudentDataServlet() {super();}protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {response.setContentType('application/json');PrintWriter out = response.getWriter();List<Student> lisOfStudent = StudentDataService.getStudentList();DataTableObject dataTableObject = new DataTableObject();dataTableObject.setAaData(lisOfStudent);Gson gson = new GsonBuilder().setPrettyPrinting().create();String json = gson.toJson(dataTableObject);out.print(json);}protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {doGet(request, response);}}
- 帶有表元素文件ajaxDataTable.jsp的html文件,
- <%@ page language='java' contentType='text/html; charset=ISO-8859-1'pageEncoding='ISO-8859-1'%> <!DOCTYPE html PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN' 'http://www.w3.org/TR/html4/loose.dtd'> <html> <head> <meta http-equiv='Content-Type' content='text/html; charset=ISO-8859-1'> <title>Ajax Data Response And JQuery data table</title><style type='text/css' title='currentStyle'>@import './resource/css/demo_table_jui.css';@import './resource/css/jquery-ui-1.9.2.custom.min.css'; </style> <style> .table-container{width:800px; } tr.odd{ background: #EDE4D4 !important; } tr.odd td.sorting_1{ background: #EDE4D4 !important; } tr.even td.sorting_1{ background: #fff !important; } </style> </head><body><div class='table-container'><table cellpadding='0' cellspacing='0' border='0' class='display jqueryDataTable'><thead><tr><th>Name</th><th>Mark</th></tr></thead><tbody></tbody></table></div><script src='./resource/js/jquery-1.8.3.min.js'></script><script src='./resource/js/jquery.dataTables.min.js'></script><script src='http://code.jquery.com/ui/1.9.2/jquery-ui.js'></script><script src='./resource/js/my-demo-table-script.js'></script></body> </html>
- 用于初始化數據表my-demo-table-script.js的 Java腳本代碼,
- $(document).ready(function() {$('.jqueryDataTable').dataTable( {'bProcessing': false,'bServerSide': false,'sAjaxSource': './StudentDataServlet','bJQueryUI': true,'aoColumns': [{ 'mData': 'name' },{ 'mData': 'mark' }]} );
} );
輸出:
集成表的輸出將是:
視頻:
下載演示代碼:
演示代碼下載鏈接參考: My Small Tutorials博客上的JCG合作伙伴 Sandeep Kumar Patel提供的JQuery DataTables和Java Integration 。
翻譯自: https://www.javacodegeeks.com/2013/02/jquery-datatables-and-java-integration.html
java jquery
總結
以上是生活随笔為你收集整理的java jquery_jQuery数据表和Java集成的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 装饰信封
- 下一篇: 蓝牙安卓软件下载(蓝牙安卓软件)