Stripes tips(12)-使用 JMesa 进行分页

4219阅读 4评论2009-03-23 hantsy
分类:Java

JMesa 提供了更多的特性,如结果集过滤等。它也提供了类似的 Display Tag 类似的 下载,解压将 jmesa.jar 复制到项目的 lib 中。 另外 JMesa 依赖一些第三方的库。

前面三个都可以从 下载。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}


...

JMesa 依赖 JQuery,对于熟悉的 JQuery 的用户来说,这无疑是个好消息。

JMesa 的例子程序复制 cssjsimages 目录到项目的 web 目录中。

现在你可以运行程序了。

[注意]注意
这里我们没有使用 JMesa 的taglib,你可以尝试使用 taglib 来替代这种编程的方式。
上一篇:Stripes tips(11)-使用 Display Tag 进行分页
下一篇:Stripes tips(13)- 多页面表单处理

文章评论