免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2007 | 回复: 0
打印 上一主题 下一主题

Stripes tips(12)-使用 JMesa 进行分页 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-03-23 18:36 |只看该作者 |倒序浏览

JMesa 提供了更多的特性,如结果集过滤等。它也提供了类似的 Display Tag 类似的 JSP 页面 taglib。

JMesa 官方网站
下载,解压将 jmesa.jar 复制到项目的 lib 中。 另外 JMesa 依赖一些第三方的库。
  • commons-lang
  • commons-collections
  • commons-beanutils
  • slf4j
    前面三个都可以从
    Apache Commons
    下载。slf4j可以从
    slf4j官方网站
    下载。
    修改ListUserActionBean。
    public class ListUserActionBean extends BaseActionBean {
        private List users;
        private String html;
        private String id = "users_table";
        public String getId() {
            return id;
        }
        public void setId(String id) {
            this.id = id;
        }
        public String getHtml() {
            return html;
        }
        public void setHtml(String html) {
            this.html = html;
        }
        public List getUsers() {
            return users;
        }
        public void setUsers(List users) {
            this.users = users;
        }
        @DefaultHandler
        public Resolution listUsers() {
            TableFacade tableFacade = TableFacadeFactory.createTableFacade(id, getContext().getRequest());
            tableFacade.setColumnProperties("id", "firstname", "lastname", "action");
            tableFacade.setMaxRows(5);
            tableFacade.setMaxRowsIncrements(5, 10);
            tableFacade.autoFilterAndSort(true);
            tableFacade.setStateAttr("state");
            Limit limit = tableFacade.getLimit();
            if (!limit.isComplete()) {
                int totalRows = Users.getUserList().size();
                tableFacade.setTotalRows(totalRows);
            }
            int rowEnd = limit.getRowSelect().getRowEnd();
            int rowStart = limit.getRowSelect().getRowStart();
            users = Users.subList(rowStart, rowEnd - rowStart);
            
            final SortSet sortSet = limit.getSortSet();
            if (sortSet != null) {
                Sort firstnameSort = sortSet.getSort("firstname");
                if (firstnameSort != null && firstnameSort.getOrder() != Order.NONE) {
                    Collections.sort(users, new FirstnameComparator());
                    if (firstnameSort.getOrder() == Order.DESC) {
                        Collections.reverse(users);
                    }
                }
                Sort lastnameSort = sortSet.getSort("lastname");
                if (lastnameSort != null && lastnameSort.getOrder() != Order.NONE) {
                    Collections.sort(users, new LastnameComparator());
                    if (lastnameSort.getOrder() == Order.DESC) {
                        Collections.reverse(users);
                    }
                }
            }
            // users = Users.subList(rowStart, rowEnd - rowStart);
            tableFacade.setItems(users);
            HtmlTable table = (HtmlTable) tableFacade.getTable();
            //table.setCaption("Presidents");
            table.getTableRenderer().setWidth("600px");
            HtmlRow row = table.getRow();
            HtmlColumn userid = row.getColumn("id");
            userid.setTitle("User ID");
            userid.setSortable(false);
            HtmlColumn firstName = row.getColumn("firstname");
            firstName.setTitle("First Name");
            HtmlColumn lastName = row.getColumn("lastname");
            lastName.setTitle("Last Name");
            HtmlColumn action = row.getColumn("action");
            action.setTitle("Action");
            action.setFilterable(false);
            action.setSortable(false);
            action.getCellRenderer().setCellEditor(new CellEditor() {
                public Object getValue(Object item, String property, int rowcount) {
                    Object value = ItemUtils.getItemValue(item, "id");
                    HtmlBuilder html = new HtmlBuilder();
                    html.a().href().quote().append(getContext().getServletContext().getContextPath()).append("/EditUser.action?id=").append(value).quote().close();
                    html.append("Edit");
                    html.aEnd();
                    html.append(" ");
                    html.a().href().quote().append(getContext().getServletContext().getContextPath()).append("/DeleteUser.action?id=").append(value).quote().close();
                    html.append("Delete");
                    html.aEnd();
                    return html.toString();
                }
            });
            html = tableFacade.render();
            return new ForwardResolution("/userList.jsp");
        }
        class FirstnameComparator implements Comparator {
            public int compare(User o1, User o2) {
                return o1.getFirstname().compareTo(o2.getFirstname());
            }
        }
        class LastnameComparator implements Comparator {
            public int compare(User o1, User o2) {
                return o1.getLastname().compareTo(o2.getLastname());
            }
        }
    }
                           
                           
    修改 userList.jsp 页面,将 body 中间内容替换成如下。
                            ...
            ${actionBean.html}
            
            
                function onInvokeAction(id) {
                    createHiddenInputFieldsForLimitAndSubmit(id);
                }
                   
            ...               
                           
    JMesa 依赖 JQuery,对于熟悉的 JQuery 的用户来说,这无疑是个好消息。
    从 JMesa 的例子程序复制 css,js,images 目录到项目的 web 目录中。
    现在你可以运行程序了。
    file:///home/hantsy/Projects/dbktools/stripes-tutorial/build/html/admonitions/note.gif
    注意这里我们没有使用 JMesa 的taglib,你可以尝试使用 taglib 来替代这种编程的方式。
                   
                   
                   

    本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/1096/showart_1876518.html
  • 您需要登录后才可以回帖 登录 | 注册

    本版积分规则 发表回复

      

    北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
    未成年举报专区
    中国互联网协会会员  联系我们:huangweiwei@itpub.net
    感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

    清除 Cookies - ChinaUnix - Archiver - WAP - TOP