当前位置: 首页 > 工具软件 > Jmesa > 使用案例 >

Jmesa简单的使用demo

养淇
2023-12-01

1.Bean


package org.jmesaweb.domain;
public class Name {
    private String firstName;
    private String lastName;
    private String nickName;
    
    public Name() {        
    }


    public Name(String firstName, String lastName, String nickName) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.nickName = nickName;
    }


    public String getFirstName() {
        return firstName;
    }


    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }


    public String getLastName() {
        return lastName;
    }


    public void setLastName(String lastName) {
        this.lastName = lastName;
    }


    public String getFullName() {
        return getFirstName() + " " + getLastName();
    }


    public String getNickName() {
        return nickName;
    }


    public void setNickName(String nickName) {
        this.nickName = nickName;
    }
}

--------------------------------------------------------------------------------------------------------
package org.jmesaweb.domain;


import java.io.Serializable;
import java.util.Date;

public class President implements Serializable {
    private int id;
    private Name name;
    private String term;
    private Date born;
    private Date died;
    private String education;
    private String career;
    private String politicalParty;
    private String selected;


    public President(int id, Name name, String term, Date born, Date died,
String education, String career, String politicalParty,
String selected) {
super();
this.id = id;
this.name = name;
this.term = term;
this.born = born;
this.died = died;
this.education = education;
this.career = career;
this.politicalParty = politicalParty;
this.selected = selected;
}


    
public President(int id, String term, Date born, Date died,
String education, String career, String politicalParty,
String selected) {
super();
this.id = id;
this.term = term;
this.born = born;
this.died = died;
this.education = education;
this.career = career;
this.politicalParty = politicalParty;
this.selected = selected;
}




public int getId() {
        return id;
    }


    public void setId(int id) {
        this.id = id;
    }


    public Name getName() {
        return name;
    }


    public void setName(Name name) {
        this.name = name;
    }


    public Date getBorn() {
        return born;
    }


    public void setBorn(Date born) {
        this.born = born;
    }


    public String getCareer() {
        return career;
    }


    public void setCareer(String career) {
        this.career = career;
    }


    public Date getDied() {
        return died;
    }


    public void setDied(Date died) {
        this.died = died;
    }


    public String getEducation() {
        return education;
    }


    public void setEducation(String education) {
        this.education = education;
    }


    public String getPoliticalParty() {
        return politicalParty;
    }


    public void setPoliticalParty(String politicalParty) {
        this.politicalParty = politicalParty;
    }


    public String getTerm() {
        return term;
    }


    public void setTerm(String term) {
        this.term = term;
    }
    
    public String getSelected() {
        return selected;
    }


    public void setSelected(String selected) {
        this.selected = selected;
    }
}


2.Action

package com.base;


import static org.jmesa.facade.TableFacadeFactory.createTableFacade;


import java.util.ArrayList;
import java.util.Date;
import java.util.List;


import static org.jmesa.limit.ExportType.CSV;
import static org.jmesa.limit.ExportType.JEXCEL;
import static org.jmesa.limit.ExportType.PDFP;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.jmesa.core.filter.DateFilterMatcher;
import org.jmesa.core.filter.MatcherKey;
import org.jmesa.facade.TableFacade;
import org.jmesa.limit.Limit;
import org.jmesa.view.component.Column;
import org.jmesa.view.component.Row;
import org.jmesa.view.component.Table;
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 org.jmesa.view.html.editor.HtmlCellEditor;
import org.jmesaweb.domain.Name;
import org.jmesaweb.domain.President;
import org.slf4j.LoggerFactory;


public class FirstJsAction extends DispatchAction {

public ActionForward execute(ActionMapping mapping, ActionForm arg1,
HttpServletRequest request, HttpServletResponse response) throws Exception {
Name name = new Name("firstName", "lastName", "nickName");
President president = null;
List items = new ArrayList();
for(int i=0;i<25;i++) {
president = new President(1, "term"+i, new Date(), new Date(), "education", "career", "politicalPart"," selected");
items.add(president);
}
String id = request.getParameter("id");
id = "base";

TableFacade tableFacade = createTableFacade(id, request);
        System.out.println("id = "+id);
        tableFacade.setItems(items); // set the items
        tableFacade.setExportTypes(response, CSV, JEXCEL, PDFP); // set the exports allowed
        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 = getHtml(tableFacade);
        request.setAttribute("presidents", html); // Set the Html in the request for the JSP
return mapping.findForward("success");
}
private String getHtml(TableFacade tableFacade) {
        // add a custom filter matcher to be the same pattern as the cell editor used.
        tableFacade.addFilterMatcher(new MatcherKey(Date.class, "born"), new DateFilterMatcher("MM/yyyy"));


        // set the column properties
        tableFacade.setColumnProperties("education", "selected", "term", "career", "born");
        
        HtmlTable table = (HtmlTable) tableFacade.getTable();
        table.setCaption("我的第一个jmesa");
        table.getTableRenderer().setWidth("1000px");


        HtmlRow row = table.getRow();


        HtmlColumn firstName = row.getColumn("education");
        firstName.setTitle("First Name");


        HtmlColumn lastName = row.getColumn("selected");
        lastName.setTitle("Last Name");


        HtmlColumn career = row.getColumn("career");
        career.getFilterRenderer().setFilterEditor(new DroplistFilterEditor());
        career.setTitle("career");


        Column born = row.getColumn("born");
        born.getCellRenderer().setCellEditor(new DateCellEditor("MM/yyyy"));


        // Using an anonymous class to implement a custom editor.
        firstName.getCellRenderer().setCellEditor(new CellEditor() {
            public Object getValue(Object item, String property, int rowcount) {
                Object value = new HtmlCellEditor().getValue(item, property, rowcount);
                HtmlBuilder html = new HtmlBuilder();
                html.a().href().quote().append("http://www.baidu.com/").quote().close();
                html.append(value);
                html.aEnd();
                return html.toString();
            }
        });


        return tableFacade.render(); // Return the Html.
    }


    private void export(TableFacade tableFacade) {
        // set the column properties
        tableFacade.setColumnProperties("name.firstName", "name.lastName", "term", "career");


        Table table = tableFacade.getTable();
        table.setCaption("Presidents");//设置标题


        Row row = table.getRow();//获得表行


        Column firstName = row.getColumn("name.firstName");
        firstName.setTitle("First Name");


        Column lastName = row.getColumn("name.lastName");
        lastName.setTitle("Last Name");


        tableFacade.render(); // Will write the export data out to the response.
    }
}


3.Jsp

<%@ page language="java" contentType="text/html; charset=gbk"
    pageEncoding="gbk"%>
<!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=gbk">
<link rel="stylesheet" type="text/css" href="/TestJmesa/css/web.css"></link>
<link rel="stylesheet" type="text/css" href="/TestJmesa/css/jmesa.css"></link>
<script type="text/javascript" src="/TestJmesa/js/jquery-1.3.min.js"></script>
<script type="text/javascript" src="/TestJmesa/js/jquery.bgiframe.pack.js"></script>
<script type="text/javascript" src="/TestJmesa/js/jquery.jmesa.js"></script>
<script type="text/javascript" src="/TestJmesa/js/jmesa.js"></script>


<title>Insert title here</title>
</head>
<body>
test!
   <form name="presidentsForm" action="first.do">
    <div align="center">
            ${presidents}
        </div>    
            kankan
        </form>
        
        <p class="content">
            This example source code can be found 
            <a href="http://code.google.com/p/jmesa/wiki/FacadeExample">here</a>.
        </p>
        <script type="text/javascript">
            function onInvokeAction(id) {
                alert("id"+id);
                $.jmesa.setExportToLimit(id, '');
                $.jmesa.createHiddenInputFieldsForLimitAndSubmit(id);
            }
            function onInvokeExportAction(id) {
                var parameterString = $.jmesa.createParameterStringForLimit(id);
                location.href = '${pageContext.request.contextPath}/first.do?' + parameterString;
            }
        </script>
</body>
</html>


<html>
    
    <head>
        <title>JMesa Example</title>
    </head>
    
    <body>
        
        <p class="content">
            JMesa using the Limit to only retrieve the current rows needed. Also using AJAX to avoid a full page refresh. 
        </p>
        
        <p class="content">
            Other examples:<br/>
            <a href="${pageContext.request.contextPath}/basic.run?restore=true">Basic</a> <br/>
            <a href="${pageContext.request.contextPath}/groovy.run?restore=true">Groovy</a><br/>
            <a href="${pageContext.request.contextPath}/tag.run?restore=true">Tag</a><br/>
            <a href="${pageContext.request.contextPath}/worksheet.run">Worksheet</a><br/>
        </p>
        
        <form name="presidentsForm">
            <div id="presidents">
                ${presidents}
            </div>
        </form>
        
        <p class="content">
            This example source code can be found 
            <a href="http://code.google.com/p/jmesa/wiki/FacadeLimitExample">here</a>.
        </p>
        
        <script type="text/javascript">
            function onInvokeAction(id) {
                $.jmesa.setExportToLimit(id, '');


                var parameterString = $.jmesa.createParameterStringForLimit(id);
                $.get('${pageContext.request.contextPath}/limit.run?ajax=true&' + parameterString, function(data) {
                $("#presidents").html(data)
            });
        }


        function onInvokeExportAction(id) {
            var parameterString = $.jmesa.createParameterStringForLimit(id);
            location.href = '${pageContext.request.contextPath}/limit.run?ajax=false&' + parameterString;
        }
        </script>
        
    </body>
    
</html>


4.jmesa.properties

html.toolbar.maxRowsDroplist.increments=12,24,36
limit.rowSelect.maxRows=12


 类似资料: