Jmesa简单使用
package com.jesea.test; import java.io.IOException;import java.lang.reflect.InvocationTargetException;import java.util.ArrayList;import java.util.Collection;import java.util.Date;import java.util.List;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import jxl.demo.CSV;import org.apache.commons.beanutils.BeanUtils;import org.apache.struts2.ServletActionContext;import org.jmesa.facade.TableFacade;import org.jmesa.facade.TableFacadeImpl;import org.jmesa.limit.Limit;import com.opensymphony.xwork2.Action;import static org.jmesa.limit.ExportType.CSV;import static org.jmesa.limit.ExportType.JEXCEL;import static org.jmesa.limit.ExportType.PDF;import org.jmesa.core.filter.DateFilterMatcher;import org.jmesa.core.filter.MatcherKey;import org.jmesa.facade.TableFacade;import org.jmesa.facade.TableFacadeImpl;import org.jmesa.limit.Limit;import org.jmesa.util.ItemUtils;import org.jmesa.view.component.Column;import org.jmesa.view.component.Row;import org.jmesa.view.component.Table;import org.jmesa.view.editor.BasicCellEditor;import org.jmesa.view.editor.CellEditor;import org.jmesa.view.editor.DateCellEditor;import org.jmesa.view.html.HtmlBuilder;import org.jmesa.view.html.component.HtmlColumn;import org.jmesa.view.html.component.HtmlRow;import org.jmesa.view.html.component.HtmlTable;import org.jmesa.view.html.editor.DroplistFilterEditor;import static org.jmesa.facade.TableFacadeFactory.createTableFacade;import com.sthdemo.commons.AbstractAction; public class TestAction extends AbstractAction { private int totalRows; private String html = null; private String Patch; HttpServletRequest request; HttpServletResponse response; private String id; public String getHtml() {return html; } public void setHtml(String html) {this.html = html; } public String presidents() throws Exception {request = ServletActionContext.getRequest();response = ServletActionContext.getResponse();totalRows =200 ; //Collectionitems = productService.findProduct(1, totalRows); List items = null ; // blogAllList.getAllBlogList();//创建id为presidents的表单TableFacade tableFacade = new TableFacadeImpl("presidents", request);tableFacade.setItems(items); // set the items//导出格式tableFacade.setExportTypes(response, CSV, JEXCEL);tableFacade.setStateAttr("restore"); // return to the table in the same state that the user left it.Limit limit = tableFacade.getLimit();if (limit.isExported()) { export(tableFacade); return null; // In Spring returning null tells the controller not to do anything.}String html = html(tableFacade);request.setAttribute("presidents", html); // Set the Html in the request for the JSP.return SUCCESS; } private String html(TableFacade tableFacade) {// add a custom filter matcher to be the same pattern as the cell editor used.tableFacade.addFilterMatcher(new MatcherKey(Date.class, "createdTime"),new DateFilterMatcher("MM/yyyy"));//设定表格属性,注意此处的url用于诸如增加、删除、修改、查询操作,并不是实际的数据库表属性,//但表单需要有对应的po对新,因此需要在Person中增加此属性//"bcontent" ,"bcon" ,"bcont" , tableFacade.setColumnProperties("bname", "bdate", "edit", "delete");HtmlTable table = (HtmlTable) tableFacade.getTable();table.setCaption("Presidents");table.getTableRenderer().setWidth("700px");HtmlRow row = table.getRow();// HtmlColumn firstName = row.getColumn("id");//firstName.setTitle("编号");// firstName.setFilterable(false);// firstName.setSortable(false);HtmlColumn lastName = row.getColumn("bname");lastName.setTitle("名称");//lastName.setFilterable(false);lastName.setSortable(false);// HtmlColumn price = row.getColumn("price");// price.setTitle("金额 ");// price.setFilterable(false);// price.setSortable(false);HtmlColumn createdTime = row.getColumn("bdate");createdTime.setTitle("日期");createdTime.setFilterable(false);createdTime.setSortable(false);// HtmlColumn career = row.getColumn("createdTime");// career.getFilterRenderer().setFilterEditor(new DroplistFilterEditor());Column born = row.getColumn("bdate");born.getCellRenderer().setCellEditor(new DateCellEditor(" yyyy-MM-DD"));HtmlColumn Edit = row.getColumn("edit");Edit.setTitle("操作");Edit.setFilterable(false);Edit.setSortable(false);Edit.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();//取得每一行的id号Object id = ItemUtils.getItemValue(item, "id");String js = request.getContextPath() + "/editer.action?id="+ id;html.a().href().quote().append(js).quote().close();html.append("编辑");html.aEnd();return html.toString(); }});HtmlColumn delete = row.getColumn("delete");delete.setTitle("操作");delete.setFilterable(false);delete.setSortable(false);delete.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();//取得每一行的id号Object id = ItemUtils.getItemValue(item, "id");String js = request.getContextPath() + "/president.action?id="+ id;html.a().href().quote().append(js).quote().close();html.append("删除");html.aEnd();return html.toString(); }});/*// 用于演示在表格中增加超链接firstName.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://www.whitehouse.gov/history/presidents/").quote().close(); html.append(value); html.aEnd(); return html.toString(); }}); */return tableFacade.render(); } private void export(TableFacade tableFacade) {// set the column propertiestableFacade.setColumnProperties("bname", "bdate");Table table = tableFacade.getTable();table.setCaption("Presidents");Row row = table.getRow();//Column firstName = row.getColumn("id");//firstName.setTitle("id");Column lastName = row.getColumn("bname");lastName.setTitle("bname");Column price = row.getColumn("bdate");price.setTitle("bdate");//Column createdTime = row.getColumn("createdTime");//createdTime.setTitle("createdTime");tableFacade.render(); } //修改方法 public String editer() throws Exception {//Product product =new Product();request = ServletActionContext.getRequest();String deleteId = request.getParameter("id");System.out.print(deleteId);int id = Integer.parseInt(deleteId);return SUCCESS; } public String getId() {return id; } public void setId(String id) {this.id = id; }}
页:
[1]