请您先登录,才能继续操作
Flexigrid Demo 详解(二)
Flexigrid Demo总结
(1) FLEXIGRID与IE浏览器的兼容 ,这里需要为Table加上div标签,否则在IE中会产生JS错误。参见search.jsp。
(2)
Flexigrid 插件后台获取数据转换成json串之后传到前台
,所以需要在项目中引入JSONObject相关插件包。
该插件的查询以及排序功能可以参考SearchAction类中的代码。
(3) 实现PDF与图片文件的预览与下载功能 ,参见SearchAction.java。
(4) 使用Flexigrid进行条件查询 。在前台JSP页面中添加form表单,需要注意的是,form表单中不能缺少name属性。否者Flexigrid无法获得form中的参数。
本文只是针对Flexigrid插件的基本用法,如分页、排序、条件查询进行了简单展示, 至于Flexigrid的详细用法及参数讲解,还请参考官网文档。希望本文对大家有所帮助。
首先,在Mysql中建立表GCIB ,如下:
CREATE TABLE `gcib` ( `id` int(11) NOT NULL, `name` varchar(45) default NULL, `file` mediumblob, `filetype` varchar(45) default NULL, PRIMARY KEY (`id`) )
下面是此Demo的详细代码:
1.配置文件
<!-- Standard Action Servlet Configuration --> <servlet> <servlet-name>action</servlet-name> <servlet-class>org.apache.struts.action.ActionServlet</servlet-class> <init-param> <param-name>config</param-name> <param-value>/WEB-INF/struts-config.xml</param-value> </init-param> <load-on-startup>2</load-on-startup> </servlet> <!-- Standard Action Servlet Mapping --> <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping>
<struts-config> <!-- Form beans Definitions --> <form-beans> <form-bean name="gcibForm" type="com.form.GcibBean" ></form-bean> </form-beans> <!-- Action Mapping Definitions --> <action-mappings> <action path="/SearchAction" type="com.action.SearchAction" parameter="method"> </action> </action-mappings> </struts-config>
2.Java文件
public class DBConnection { private static Connection conn; private final static String url = "jdbc:mysql://localhost:3306/test?unicode=true&characterEncoding=UTF-8"; private final static String user = "root"; private final static String password = "root"; private final static String DRIVER="com.mysql.jdbc.Driver"; public static Connection getConnection(){ try { try { Class.forName(DRIVER).newInstance(); conn = DriverManager.getConnection(url, user, password); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } catch (SQLException e) { System.out.println("ERROR: database connection wrong."); } return conn; } public void closeConnection() { if (!(conn == null)) { try { conn.close(); } catch (SQLException e) { System.out.println("ERROR: database close wrong."); } } } }
/** * GCIB class mapping the table GCIB in mysql. * add a url property for deal with the record. * @author Victor */ public class Gcib { private int id; private String name; private String filetype; private String url; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public String getFiletype() { return filetype; } public void setFiletype(String filetype) { this.filetype = filetype; } }
/** * Manage the GCIB object. * @author Victor */ public class GcibDAO { /** * Get the search result with pagination. * @param paraMap * @param start * @param end * @return */ public List<Gcib> getGcibList(Map<String,Object> paraMap,int start, int end) { List<Gcib> folderList = new ArrayList<Gcib>(); String sql = "select id,name,filetype from gcib where 1=1 "; //search conditions in where statement. String idPara = (String)paraMap.get("idPara"); String namePara = (String)paraMap.get("namePara"); //flexigrid order. String sortname = (String)paraMap.get("sortname"); String sortorder = (String)paraMap.get("sortorder"); String orderSQL = ""; //record order statement. if(!this.isEmpty(sortname)||!this.isEmpty(sortorder)){ orderSQL= " order by " + sortname+" "+sortorder; } sql = sql+(!this.isEmpty(idPara)?" and id ="+idPara :""); sql = sql+(!this.isEmpty(namePara)?" and name ='"+namePara+"'":""); sql = sql + orderSQL; sql = sql + " limit "+start+","+end+""; Connection connect = DBConnection.getConnection(); Statement stmt = null; try { stmt = connect.createStatement(); ResultSet rs = stmt.executeQuery(sql); while(rs.next()){ Gcib gcib = new Gcib(); Integer idInt = rs.getInt("id"); gcib.setId(idInt); gcib.setName(rs.getString("name")); gcib.setFiletype(rs.getString("filetype")); //String url = "<a href=\"/blank/SearchAction.do?method=showPDFandIMG&fileID="+idInt.toString()+"\" traget=\"_black\"\"><font color=\'blue\'>预览</font></a>"; //如果使用window.showModalDialog方法弹出下载文件,在IE中不好用。所以使用window.open或者上面的方法。 String url = "<a href=\"javascript:onClick=GCIB.showObject(\'"+idInt.toString()+"\')\"><font color=\'blue\'>预览</font></a>"; gcib.setUrl(url); folderList.add(gcib); } } catch (SQLException e) { e.printStackTrace(); } return folderList; } /** * Get the file in byte[]. * @param id * @return */ public byte[] getDBFile(int id) { String sqlFind = "select file from gcib where id = "+id; byte[] pdf = null; Connection conn = DBConnection.getConnection(); try { Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery(sqlFind); while(rs.next()){ pdf = rs.getBytes("file"); } } catch (SQLException e) { e.printStackTrace(); } catch (NullPointerException enull){ enull.printStackTrace(); } return pdf; } /** * Get the file in Blob. * @param id * @return */ public Blob getDBBlobFile(int id) { String sqlFind = "select file from gcib where id = "+id; Blob pdf = null; Connection conn = DBConnection.getConnection(); try { Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery(sqlFind); while(rs.next()){ pdf = rs.getBlob("file"); } } catch (SQLException e) { e.printStackTrace(); } catch (NullPointerException enull){ enull.printStackTrace(); } return pdf; } /** * Get the total of the records. * @return */ public int getTotal() { int total = 0; String sqlFind = "select count(*) from gcib "; Connection conn = DBConnection.getConnection(); try { Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery(sqlFind); while(rs.next()){ total = rs.getInt(1); } } catch (SQLException e) { e.printStackTrace(); } return total; } /** * Get the filetype of the stream for download and preview the file. * @param id * @return */ public String getFileType(Integer id) { String sqlFind = "select fileType from gcib where id = "+id; String result = null; Connection conn = DBConnection.getConnection(); try { Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery(sqlFind); while(rs.next()){ result = rs.getString("fileType"); } } catch (SQLException e) { e.printStackTrace(); } catch (NullPointerException enull){ enull.printStackTrace(); } return result; } /** * To check the string whether is empty. * @param para * @return */ private boolean isEmpty(String para){ return null==para || para.length()==0; } }
这里我使用的是Json-lib插件获取JSONObejct
。需要注意的是,要导入如下包到Lib中。
Json-lib jar
jakarta commons-lang 2.5
jakarta commons-beanutils 1.8.0
jakarta commons-collections 3.2.1
jakarta commons-logging 1.1.1
ezmorph 1.0.6
/** * Action class to search records and show file. * @author Victor */ public class SearchAction extends DispatchAction{ /** * Search method. * @param mapping * @param form * @param request * @param response */ public void getList(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { PrintWriter writer = null; JSONObject result = new JSONObject(); response.setContentType("text/json"); String idPara = request.getParameter("idPara"); //APP search condition. String namePara = request.getParameter("namePara"); //APP search condition. String sortname = request.getParameter("sortname"); //flexigrid's attribute. String sortorder = request.getParameter("sortorder"); //flexigrid's attribute. /* String query = request.getParameter("query"); //flexigrid's query attribute. String qtype = request.getParameter("qtype"); //flexigrid's query attribute. */ try { request.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8"); writer = response.getWriter(); Map<String,Object> paraMap = new HashMap<String,Object>(); paraMap.put("idPara", idPara); paraMap.put("namePara", namePara); paraMap.put("sortname", sortname); paraMap.put("sortorder", sortorder); GcibDAO dao = new GcibDAO(); int page = Integer.valueOf(StringUtils.defaultIfEmpty(request.getParameter("page"), "1")); int rp = Integer.valueOf(StringUtils.defaultIfEmpty(request.getParameter("rp"), "10")); int start = (page - 1) * rp; int end = start + rp; int total = dao.getTotal(); List<?> demoList = new ArrayList<Gcib>(); demoList = dao.getGcibList(paraMap,start, end); result.put("rows", demoList); result.put("total", total); result.put("page", page); result.put("rp", rp); }catch (Exception e) { e.printStackTrace(); } finally { //IMPORTANT: this statement is very important for flexigrid. if without this, there will show nothing in the grid. writer.println(result.toString()); writer.flush(); writer.close(); } } /** * Show file method. For now. * @param mapping * @param form * @param request * @param response * @return * @throws IOException */ public ActionForward showPDFandIMG(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws IOException { OutputStream out = new BufferedOutputStream(response.getOutputStream()); GcibDAO dao = new GcibDAO(); String fileName = "FileName"; String fileType = ""; Blob blob = dao.getDBBlobFile(new Integer(request.getParameter("fileID"))); fileType = dao.getFileType(new Integer(request.getParameter("fileID"))); fileName = fileName + fileType; try { //stop IE cookie /*response.setHeader("pragma", "no-cache"); response.setHeader("Cache-Control", "no-cache"); response.setDateHeader("Expires", 0);*/ response.reset(); response.setCharacterEncoding("UTF-8"); response.setHeader("Content_Length", new Long(blob.length()).toString()); if (fileType.contains("pdf")) { response.setContentType("application/pdf;charset=UTF-8"); } if (fileType.contains("jpg")||fileType.contains("jpeg")) { response.setContentType("image/*;charset=UTF-8"); } //IMPORTANT: different browser different ways to deal file download and preview. String agent = request.getHeader("USER-AGENT"); if(agent.indexOf("MSIE")==-1){ String enableName = new String(fileName.getBytes("UTF-8"),"ISO-8859-1"); //IMPORTANT: attachment mean download the file;inline mean open file in browser. response.setHeader("Content-Disposition","inline; filename=" + enableName ); }else{ response.setHeader("Content-Disposition","inline; filename=" + java.net.URLEncoder.encode(fileName,"UTF-8") ); } BufferedInputStream in = new BufferedInputStream(blob.getBinaryStream()); byte[] data = new byte[1024]; long k = 0; while (k<blob.length()) { int j = in.read(data, 0, 1024); k+=j; out.write(data, 0, j); } out.flush(); in.close(); out.close(); } catch (SQLException e) { e.printStackTrace(); } return null; } }
3.前台页面及JS文件
到Flexigrid 官网下载插件,并集成到项目中。
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <% String webPath = request.getContextPath(); %> <!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=UTF-8"> <title>Search Page.</title> <link rel="stylesheet" type="text/css" href="<%=webPath%>/View/common/Flexigrid-master/css/flexigrid.pack.css" /> <script type="text/javascript" src="<%=webPath%>/View/common/Flexigrid-master/js/jquery-1.6.3.js"></script> <script type="text/javascript" src="<%=webPath%>/View/common/Flexigrid-master/js/flexigrid.pack.js"></script> <script type="text/javascript"> var webpath = "<%=webPath%>"; var $jQuery = $; </script> <script type="text/javascript" src="<%=webPath%>/View/search.js"></script> </head> <body> <div style="align:center;margin:0 auto;width:800px"> <div align="center" style="margin-top:20px"> <form id="queryForm" name="queryForm"> <table> <tr> <th align="center" colspan="2">Search conditions.</th> </tr> <tr> <td align="right">id:</td> <td align="left"><input type="text" id="idPara" name="idPara" /></td> </tr> <tr> <td align="right">name:</td> <td align="left"><input type="text" id="namePara" name="namePara" /></td> </tr> <tr> <td align="center" colspan="2"> <span><input type="button" id="queryBtn" value="查询"></span> <span><input type="reset" id="resetBtn" value="重置"></span></td> </tr> <tr> <td align="center" colspan="2"><font size="2">Please input your conditions to have a search, <br/>and the result will showed below.</font></td> </tr> </table> </form> </div> <!-- 考虑到FLEXIGRID对IE浏览器的兼容,这里需要为Table加上div标签,否则在IE中会产生JS错误。 --> <div id="divTable"> <table id="gcibTable" style="display: none;"></table> </div> </div> </body> </html>
var GCIB = new Object(); $jQuery().ready(function() { //bind search button to FLEXIGRID. $jQuery("#queryBtn").bind("click",GCIB.getList); }); GCIB.getList = function(){ //search conditions parameters. var params = [ {"name" : "idPara", "value" : $jQuery("#idPara").val()}, {"name" : "namePara", "value" : $jQuery("#namePara").val()} ]; $jQuery('#gcibTable').flexOptions({params : params, newp : 1}).flexReload(); $("#gcibTable").flexigrid( { url: webpath+'/SearchAction.do?method=getList', dataType: 'json', colModel : [ {display: '编号', name : 'id', width: 100, sortable: true, align: 'center'}, {display: '文件名称', name : 'name', width: 200, sortable: true, align: 'center'}, {display: '文件类型', name : 'filetype', width: 200, sortable: true, align: 'center'}, {display: '操作', name : 'url', width: 100, sortable: false, align: 'center'} ], checkbox : false, sortname: "id", sortorder: "asc", usepager: true, title: 'Results.', useRp: true, rp: 10, showTableToggleBtn: true, height:400, width:800 } ); }; //show the file which from database. GCIB.showObject= function(picId){ var url = webpath + '/SearchAction.do?method=showPDFandIMG&fileID=' + picId; url=encodeURI(url); //注释掉下面两行模态窗口代码,因为IE无法下载或显示流文件(其他浏览器ModalDialog可以) //var openStyle = "dialogHeight:600px; dialogWidth:800px; status:no; help:no; scroll:auto"; //window.showModalDialog(url, window.document, openStyle); var name = 'ShowPDF'; //open的网页名称,可为空; 但是不能有特殊字符,甚至连空格都不能有。否则会包参数无效JS错误。 var iWidth = 800; ; var iHeight = 600; var iTop = (window.screen.availHeight-30-iHeight)/2; //获得窗口的垂直位置; var iLeft = (window.screen.availWidth-10-iWidth)/2; //获得窗口的水平位置; var properties = "height="+iHeight+",width="+iWidth+",top="+iTop+",left="+iLeft+",toolbar=no,menubar=no,scrollbars=auto,resizable=no,location=no,status=no"; window.open(url, name, properties); };
发表评论
-
WebService资源
2013-12-17 17:49 0https://metro.java.net/guide/ ... -
解决生成CSV文件中文乱码问题
2013-12-09 17:46 0import java.io.File; import j ... -
Java jar包以及生成可执行文件所需资源介绍
2013-12-02 13:41 01. exe4j 将jar包封装成可在WINDOWS系统中的 ... -
高斯日记【JAVA实现】
2013-11-21 10:21 1301大数学家高斯有个好习惯:无论如何都要记日记。 他 ... -
James development guide
2013-04-09 17:34 0============================ ... -
DEVELOPMENT GUIDE
2013-03-21 15:07 0=============================== ... -
输出1,2,...,n这组数中任意三个数的不重复组合
2013-03-07 09:50 1597/** * 有一组数1,2,3,4,5输出不重复的 ... -
SVN针对GoogleCode项目版本控制的问题汇总
2013-01-24 11:14 1314使用GoogleCode管理项目,通过SVN进行版本控制时 ... -
Flexigrid Demo 详解(一)
2013-01-03 10:50 2399个人网站:www.diaopg.com 本文主要讲解 ...
相关推荐
**jQuery Flexigrid JSP Demo 知识点详解** `jQuery Flexigrid` 是一个流行的JavaScript库,用于创建数据网格,具有强大的数据管理和用户交互功能。这个Demo是将Flexigrid与Java服务器页面(JSP)结合使用的一个...
**jQuery Grid 演示详解** `jQuery Grid` 是一个强大的数据网格插件,它允许开发者在网页上展示和操作结构化的数据。本演示集中了 `jQuery Flexigrid` 和 `DatePicker` 的应用,旨在展示如何结合这两个组件来创建...
**jQuery表格插件详解** jQuery表格插件是前端开发中常用的一种工具,它极大地提升了HTML表格的功能性和用户体验。本文将深入探讨这些插件的特点、优势以及如何利用它们创建功能丰富的数据展示界面。 ### 1. ...
### jQuery 插件大全知识点详解 #### 一、概述 jQuery 是一款优秀的 JavaScript 库,以其简洁、高效著称,在 Web 开发领域占有极其重要的地位。由于其强大的易用性和扩展性,许多开发者都倾向于使用 jQuery 来简化...
基于的手势识别系统可控制灯的亮_3
untitled2.zip
S7-1500和分布式外围系统ET200MP模块数据
anaconda配置pytorch环境
高校教室管理系统,主要的模块包括查看首页、个人中心、教师管理、学生管理、教室信息管理、教师申请管理、学生申请管理、课时表管理、教师取消预约管理、学生取消预约管理等功能。
半挂汽车列车横向稳定性控制研究:基于模糊PID与制动力矩分配的联合仿真分析在典型工况下的表现,半挂汽车列车在典型工况下的横向稳定性控制研究:基于模糊PID与制动力矩分配的联合仿真分析,半挂汽车列车4自由度6轴整车model,横向稳定性控制,在低附着系数路面,进行典型3个工况,角阶跃,双移线,方向盘转角。 采用算法:模糊PID,制动力矩分配,最优滑移率滑膜控制。 以上基于trucksim和simulink联合仿真,有对应 p-a-p-e-r参考 ,关键词: 1. 半挂汽车列车 2. 4自由度6轴整车model 3. 横向稳定性控制 4. 低附着系数路面 5. 典型工况(角阶跃、双移线、方向盘转角) 6. 模糊PID算法 7. 制动力矩分配 8. 最优滑移率滑膜控制 9. Trucksim和Simulink联合仿真 10. P-A-P-E-R参考; 用分号隔开上述关键词为:半挂汽车列车; 4自由度6轴整车model; 横向稳定性控制; 低附着系数路面; 典型工况; 模糊PID算法; 制动力矩分配; 最优滑移率滑膜控制; Trucksim和Simulink联合仿真; P-A-P-E-R参考
路径规划人工势场法及其改进算法Matlab代码实现,路径规划人工势场法及其改进算法Matlab代码实现,路径规划人工势场法以及改进人工势场法matlab代码,包含了 ,路径规划; 人工势场法; 改进人工势场法; MATLAB代码; 分隔词“;”。,基于Matlab的改进人工势场法路径规划算法研究
本文介绍了范德堡大学深脑刺激器(DBS)项目,该项目旨在开发和临床评估一个系统,以辅助从规划到编程的整个过程。DBS是一种高频刺激治疗,用于治疗运动障碍,如帕金森病。由于目标区域在现有成像技术中可见性差,因此DBS电极的植入和编程过程复杂且耗时。项目涉及使用计算机辅助手术技术,以及一个定制的微定位平台(StarFix),该平台允许在术前进行图像采集和目标规划,提高了手术的精确性和效率。此外,文章还讨论了系统架构和各个模块的功能,以及如何通过中央数据库和网络接口实现信息共享。
三菱FX3U步进电机FB块的应用:模块化程序实现电机换算,提高稳定性和移植性,三菱FX3U步进电机换算FB块:模块化编程实现电机控制的高效性与稳定性提升,三菱FX3U 步进电机算FB块 FB块的使用可以使程序模块化简单化,进而提高了程序的稳定性和可移植性。 此例中使用FB块,可以实现步进电机的算,已知距离求得脉冲数,已知速度可以求得频率。 程序中包含有FB和ST内容;移植方便,在其他程序中可以直接添加已写好的FB块。 ,三菱FX3U;步进电机换算;FB块;程序模块化;稳定性;可移植性;距离与脉冲数换算;速度与频率换算;FB和ST内容;移植方便。,三菱FX3U步进电机换算FB块:程序模块化与高稳定性实现
光伏逆变器TMS320F28335设计方案:Boost升压与单相全桥逆变,PWM与SPWM控制,MPPT恒压跟踪法实现,基于TMS320F28335DSP的光伏逆变器设计方案:Boost升压与单相全桥逆变电路实现及MPPT技术解析,光伏逆变器设计方案TMS320F28335-176资料 PCB 原理图 源代码 1. 本设计DC-DC采用Boost升压,DCAC采用单相全桥逆变电路结构。 2. 以TI公司的浮点数字信号控制器TMS320F28335DSP为控制电路核心,采用规则采样法和DSP片内ePWM模块功能实现PWM和SPWM波。 3. PV最大功率点跟踪(MPPT)采用了恒压跟踪法(CVT法)来实现,并用软件锁相环进行系统的同频、同相控制,控制灵活简单。 4.资料包含: 原理图,PCB(Protel或者AD打开),源程序代码(CCS打开),BOM清单,参考资料 ,核心关键词:TMS320F28335-176; 光伏逆变器; 升压; 逆变电路; 数字信号控制器; 规则采样法; ePWM模块; PWM; SPWM波; MPPT; 恒压跟踪法; 原理图; PCB; 源程序代码; BOM
centos9内核安装包
昆仑通态触摸屏与两台台达VFD-M变频器通讯实现:频率设定、启停控制与状态指示功能接线及设置说明,昆仑通态TPC7062KD触摸屏与两台台达VFD-M变频器通讯程序:实现频率设定、启停控制与状态指示,昆仑通态MCGS与2台台达VFD-M变频器通讯程序实现昆仑通态触摸屏与2台台达VFD-M变频器通讯,程序稳定可靠 器件:昆仑通态TPC7062KD触摸屏,2台台达VFD-M变频器,附送接线说明和设置说明 功能:实现频率设定,启停控制,实际频率读取等,状态指示 ,昆仑通态MCGS; 台达VFD-M变频器; 通讯程序; 稳定可靠; 频率设定; 启停控制; 实际频率读取; 状态指示; 接线说明; 设置说明,昆仑通态MCGS与台达VFD-M变频器通讯程序:稳定可靠,双机控制全实现
研控步进电机驱动器方案验证通过,核心技术成熟可生产,咨询优惠价格!硬件原理图与PCB源代码全包括。,研控步进电机驱动器方案验证通过,核心技术掌握,生产准备,咨询实际价格,包含硬件原理图及PCB源代码。,研控步进电机驱动器方案 验证可用,可以生产,欢迎咨询实际价格,快速掌握核心技术。 包括硬件原理图 PCB源代码 ,研控步进电机驱动器方案; 验证可用; 可生产; 核心技术; 硬件原理图; PCB源代码,研控步进电机驱动器方案验证通过,现可生产供应,快速掌握核心技术,附硬件原理图及PCB源代码。
高质量的OPCClient_UA源码分享:基于C#的OPC客户端开发源码集(测试稳定、多行业应用实例、VS编辑器支持),高质量OPC客户端源码解析:OPCClient_UA C#开发,适用于VS2019及多行业现场应用源码分享,OPCClient_UA源码OPC客户端源码(c#开发) 另外有opcserver,opcclient的da,ua版本的见其他链接。 本项目为VS2019开发,可用VS其他版本的编辑器打开项目。 已应用到多个行业的几百个应用现场,长时间运行稳定,可靠。 本项目中提供测试OPCClient的软件开发源码,有详细的注释,二次开发清晰明了。 ,OPCClient_UA; OPC客户端源码; C#开发; VS2019项目; 稳定可靠; 详细注释; 二次开发,OPC客户端源码:稳定可靠的C#开发实现,含详细注释支持二次开发
毕业设计