一直以来,都没发现什么好的分页组件,最初时用过displaytag,仔细研究了一下,发现它并没有别人说的那么强大,至少离自己的期望还很远,因此尝试寻找其它新的分页组件,但很久以来都没发现自己满意的。无意中又发现了jmesa,也受到了很多的人的吹捧。于是自己也尝试用一下,不过发现其资料相当的少,就算其官方网站上也没多少资料,把整个2.4.5的ZIP包下载下来后,发现连基本的文档都没有,更让人郁闷的是基本的API文档都没有。看来要看看基本的效果也不是那么简单的事情,唯一的资料就是官方的几个例子,实在搞不明白也只有看源码,说实话,源码的注释很少,有些方法或类是用来做什么的根本就没说,理解起来确实有点难度,说了这么多,也把这一两天的成果写下来,怕自己坚持不了多久,索性写一点算一点。

需要用到的组件有点多,如附件图所示。需要注意的是,web层用了Stripes,个人认为此框架很好,源码也写得很好。2.4.5的ZIP包中,有大多数文件,但是有些也没有,如slf4j与jquery等。

jmesa分页的方式有两种,一种是通过java代码直接控制,第二种是通过标签来控制。

首先介绍一下直接在java代码中控制的方式:

  1. public class PaginationActionBean implements ActionBean {
  2. //此id表示将要创建的table的id
  3. private String id = "user_table";
  4. private ActionBeanContext context;
  5. private String html;
  6. public String getHtml() {
  7. return html;
  8. }
  9. public void setHtml(String html) {
  10. this.html = html;
  11. }
  12. ........
  13. @DefaultHandler
  14. public Resolution display() {
  15. TableFacade tableFacade = TableFacadeFactory.createTableFacade(id, this
  16. .getContext().getRequest());
  17. addItems(tableFacade);
  18. html = html(tableFacade);
  19. ForwardResolution resolution = new ForwardResolution("/jsp/page.jsp");
  20. return resolution;
  21. }
  22. private void addItems(TableFacade tableFacade) {
  23. tableFacade.setItems(FillListData.getData());
  24. }
  25. private String html(TableFacade tableFacade) {
  26. tableFacade.setColumnProperties("name", "password", "deleteUser");
  27. HtmlTable table = (HtmlTable) tableFacade.getTable();
  28. table.setCaption("用户列表");
  29. table.getTableRenderer().setWidth("600px");
  30. HtmlRow row = table.getRow();
  31. HtmlColumn name = row.getColumn("name");
  32. name.setTitle("名字");
  33. HtmlColumn password = row.getColumn("password");
  34. password.setTitle("密码");
  35. HtmlColumn delete = row.getColumn("deleteUser");
  36. delete.setTitle("删除");
  37. delete.setWidth("100px");
  38. // Using an anonymous class to implement a custom editor.
  39. // 用于演示在表格中增加超链接
  40. name.getCellRenderer().setCellEditor(new CellEditor() {
  41. public Object getValue(Object item, String property, int rowcount) {
  42. Object value = new BasicCellEditor().getValue(item, property,
  43. rowcount);
  44. HtmlBuilder html = new HtmlBuilder();
  45. html.a().href().quote().append("http://baidu.com").quote()
  46. .close();
  47. html.append(value);
  48. html.aEnd();
  49. return html.toString();
  50. }
  51. });
  52. delete.getCellRenderer().setCellEditor(new CellEditor() {
  53. public Object getValue(Object item, String property, int rowcount) {
  54. HtmlBuilder html = new HtmlBuilder();
  55. // 取得每一行的id号
  56. Object user = ItemUtils.getItemValue(item, "name");
  57. String js = " onclick='javascript:del(\"user\"," + user + ") '";
  58. html.a().append(js).href().quote().append(
  59. getContext().getRequest().getContextPath()
  60. + "/Pagination.action?delete&user=" + user)
  61. .quote().close();
  62. html.append("删除");
  63. html.aEnd();
  64. return html.toString();
  65. }
  66. });
  67. return tableFacade.render(); // Return the Html.
  68. }
  69. ..............
  70. }
public class PaginationActionBean implements ActionBean {

//此id表示将要创建的table的id
private String id = "user_table";
private ActionBeanContext context;
private String html; public String getHtml() {
return html;
}
public void setHtml(String html) {
this.html = html;
}
........ @DefaultHandler
public Resolution display() {
TableFacade tableFacade = TableFacadeFactory.createTableFacade(id, this
.getContext().getRequest()); addItems(tableFacade);
html = html(tableFacade);
ForwardResolution resolution = new ForwardResolution("/jsp/page.jsp");
return resolution;
} private void addItems(TableFacade tableFacade) {
tableFacade.setItems(FillListData.getData());
} private String html(TableFacade tableFacade) { tableFacade.setColumnProperties("name", "password", "deleteUser"); HtmlTable table = (HtmlTable) tableFacade.getTable();
table.setCaption("用户列表");
table.getTableRenderer().setWidth("600px"); HtmlRow row = table.getRow(); HtmlColumn name = row.getColumn("name");
name.setTitle("名字"); HtmlColumn password = row.getColumn("password");
password.setTitle("密码"); HtmlColumn delete = row.getColumn("deleteUser");
delete.setTitle("删除");
delete.setWidth("100px"); // Using an anonymous class to implement a custom editor.
// 用于演示在表格中增加超链接
name.getCellRenderer().setCellEditor(new CellEditor() {
public Object getValue(Object item, String property, int rowcount) {
Object value = new BasicCellEditor().getValue(item, property,
rowcount);
HtmlBuilder html = new HtmlBuilder();
html.a().href().quote().append("http://baidu.com").quote()
.close();
html.append(value);
html.aEnd();
return html.toString();
}
}); delete.getCellRenderer().setCellEditor(new CellEditor() {
public Object getValue(Object item, String property, int rowcount) { HtmlBuilder html = new HtmlBuilder();
// 取得每一行的id号
Object user = ItemUtils.getItemValue(item, "name");
String js = " onclick='javascript:del(\"user\"," + user + ") '";
html.a().append(js).href().quote().append(
getContext().getRequest().getContextPath()
+ "/Pagination.action?delete&user=" + user)
.quote().close();
html.append("删除");
html.aEnd();
return html.toString();
}
}); return tableFacade.render(); // Return the Html.
} ..............
}

上面的代码中最重要的就是那上html方法,此方法完成了整个表格的定制工作,包括链接在内。如果要重新设置每页显示的记录数(默认每页可显示15,50,100),要么修改jmesa.properties文件,要么通过java代码设置。修改配置文件的话,同时要修改两个地方:

limit.rowSelect.maxRows=15

html.toolbar.maxRowsDroplist.increments=15,50,100

如果修改,必须满足第一行的数字必须是第二行所有数字中的一个。用java代码修改的也要遵照同样的原则。

jsp代码很简单:

  1. <script type="text/javascript"
  2. src="${pageContext.request.contextPath}/js/jquery.js"></script>
  3. <script type="text/javascript"
  4. src="${pageContext.request.contextPath}/js/jquery.jmesa.js"></script>
  5. <script type="text/javascript"
  6. src="${pageContext.request.contextPath}/js/jmesa.js"></script>
  7. <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/css/jmesa.css"></link>
  8. </head>
  9. <body>
  10. <h1>Stripes Calculator</h1>
  11. <form>
  12. ${actionBean.html}
  13. <script type="text/javascript">
  14. function onInvokeAction(id) {
  15. createHiddenInputFieldsForLimitAndSubmit(id);
  16. }
  17. </script>
  18. </form>
  19. </body>
<script type="text/javascript"
src="${pageContext.request.contextPath}/js/jquery.js"></script>
<script type="text/javascript"
src="${pageContext.request.contextPath}/js/jquery.jmesa.js"></script>
<script type="text/javascript"
src="${pageContext.request.contextPath}/js/jmesa.js"></script>
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/css/jmesa.css"></link>
</head>
<body>
<h1>Stripes Calculator</h1> <form>
${actionBean.html}
<script type="text/javascript">
function onInvokeAction(id) {
createHiddenInputFieldsForLimitAndSubmit(id);
}
</script>
</form>
</body>

上面这段代码中,需要注意一下JS声明的顺序,而且还要申明一个form,不然分页的时候JS会出错。还要加上上面的那段JS代码。

第二种方式是直接通过标签的方式申明,因此需要在JSP头部申明:

  1. <%@ taglib uri="/WEB-INF/tld/jmesa.tld" prefix="jmesa" %>
  2. .........
  3. ...............
<%@ taglib uri="/WEB-INF/tld/jmesa.tld" prefix="jmesa" %>
.........
...............

将${actionBean.html}替换成:

  1. <jmesa:tableFacade id="user_table" items="${items}" var="bean" >
  2. <jmesa:htmlTable width="600px">
  3. <jmesa:htmlRow>
  4. <jmesa:htmlColumn property="name"/>
  5. <jmesa:htmlColumn property="password" title="Last Name"/>
  6. </jmesa:htmlRow>
  7. </jmesa:htmlTable>
  8. </jmesa:tableFacade>
<jmesa:tableFacade id="user_table" items="${items}" var="bean" >
<jmesa:htmlTable width="600px">
<jmesa:htmlRow>
<jmesa:htmlColumn property="name"/>
<jmesa:htmlColumn property="password" title="Last Name"/>
</jmesa:htmlRow>
</jmesa:htmlTable>
</jmesa:tableFacade>

这部分代码的作用与上面action直接操纵表格的方式一样,只是把工作转移到JSP中。只是上面的${items}表示一个将要显示的collection,不用再使用字符串的方式显示。如果两种方式都用的话,则以JSP中的方式为准。

jmesa的配置文件已经集成在JAR中,如果需要改变,可以将此文件复制出来,改变其内容,然后在web.xml重新指定其路径:

  1. <context-param>
  2. <param-name>jmesaPreferencesLocation</param-name>
  3. <param-value>WEB-INF/jmesa.properties</param-value>
  4. </context-param>
<context-param>
<param-name>jmesaPreferencesLocation</param-name>
<param-value>WEB-INF/jmesa.properties</param-value>
</context-param>
  • 大小: 27.3 KB

jmesa应用的更多相关文章

  1. 使用 Spring 2.5 TestContext 测试DAO层

    资源准备:   mysql5.0 spring-2.5  hibernate-3.2  junit-4.jar 创建表 DROP TABLE IF EXISTS `myproject`.`boys`; ...

  2. Java开源JSP标签库

    01displytag 与Struts结合使用最出名的一个tag主要是显示表格数据很漂亮.完善. 02cewolf tag 用来在web上显示复杂图形报表的一个jsp tag. 03Loading T ...

随机推荐

  1. C#基础学习3

    运算符,表达式!

  2. [转]MVC 检测用户是否已经登录

    本文转自:http://blog.csdn.net/jayzai/article/details/41252137 当我们访问某个网站的时候需要检测用户是否已经登录(通过Session是否为null) ...

  3. AJPFX分享eclipse自动生成java注释方法

    设置方法介绍:eclipse中:Windows->Preferences->Java->Code Style->Code Template->Comments,然后对应的 ...

  4. AJPFX关于枚举,泛型详解

    枚举类型是JDK5.0的新特征.Sun引进了一个全新的关键字enum来定义一个枚举类.下面就是一个典型枚举类型的定义:public enum Color{RED,BLUE,BLACK,YELLOW,G ...

  5. [转]Android APK签名原理及方法

    准备知识:数据摘要 这个知识点很好理解,百度百科即可,其实他也是一种算法,就是对一个数据源进行一个算法之后得到一个摘要,也叫作数据指纹,不同的数据源,数据指纹肯定不一样,就和人一样. 消息摘要算法(M ...

  6. postgresql版sde(10.4.1)安装说明

    从ArcGIS 10.3开始,彻底没有了sde的安装包,安装sde数据库需要先安装arcgis desktop,通过arccatalog建数据库,同时也不能建sde服务,只能使用直连 以下演示在sde ...

  7. Kotlin学习的一些笔记

    Introduction 写在前面 关于本书 这本书适合你吗? 关于作者 介绍 什么是Kotlin? 我们通过Kotlin得到什么 准备工作 Android Studio 安装Kotlin插件 创建一 ...

  8. http://bbs.chinaunix.net/thread-1463276-1-1.html

    http://bbs.chinaunix.net/thread-1463276-1-1.html

  9. JS实现让其他浏览器自动转至微信浏览器打开链接

    //判断是否是pc function is_pc() { var sUserAgent = navigator.userAgent.toLowerCase(); var bIsIpad = sUser ...

  10. linux_2

    mac和linux上默认安装着SSH客户端,Windows上需要自己安装个软件. Windows下SSH客户端的安装:建议从官方网站下载正式程序安装(免费) Putty:https://www.chi ...