`

基于struts2+spring+hibernate+jquery的jmesa分页实现样例(转)

阅读更多

基于Jmesa,从数据库表Person中查询出记录,能够实现分页、排序、导出功能。同时结合Jquery,利用ajax实现对数据的删除操作。

Jmesa: 2.3

Struts2 :2.0.11

Spring:2.5

Hibernate:3.2.5

Jquery:jquery-1.2.1.pack,jquery.bgiframe.pack

Tomcat:5.5

Mysql :5.0

数据库、页面、JVM编码统一为GBK
 

数据库表结构:

CREATE TABLE `person` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `firstName` varchar(45) NOT NULL,
  `lastName` varchar(45) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=76 DEFAULT CHARSET=latin1; 

页面list.jsp:

<%@ taglib prefix=“s” uri=“/struts-tags”%>
<%@ page language=“java” errorPage=“/error.jsp” pageEncoding=“GBK” contentType=“text/html;charset=GBK” %>
<html>
<head>
<link rel=“stylesheet” type=“text/css” href=“<%= request.getContextPath() %>/css/web.css”></link>
<link rel=“stylesheet” type=“text/css” href=“<%= request.getContextPath() %>/css/jmesa.css”></link>
<!–
对jmesa.js脚本的应用必须放到头部,而不能放到尾部
–>
<script type=“text/javascript” src=“<%= request.getContextPath() %>/js/jmesa.js”></script>
</head>
<body>
<p>Jmesa表单组件使用演示样例</p>

<form name=“personForm” action=“<%= request.getContextPath() %>/list.action” method=“post”>
<div id=“persons”>
<%
    out.println(request.getAttribute(“myhtml”));
%>
</div>
</form>

<script type=“text/javascript”>
function onInvokeAction(id) {
    setExportToLimit(id, ”);
    createHiddenInputFieldsForLimitAndSubmit(id);
}
function onInvokeExportAction(id) {
    var parameterString = createParameterStringForLimit(id);
    location.href = ‘<%= request.getContextPath() %>/list.action?’ + parameterString;
}

function delUser(tableId,rowId) {
    var parameterString = createParameterStringForLimit(tableId);
    $.get(“<%= request.getContextPath() %>/ajax.action?id=”+rowId+“&”+parameterString, function(data) {
        $(“#persons”).html(data)
    });

}
</script>
<script type=“text/javascript” src=“<%= request.getContextPath() %>/js/jquery-1.2.1.pack.js”></script>
<script type=“text/javascript” src=“<%= request.getContextPath() %>/js/jquery.bgiframe.pack.js”></script>

</body>
</html>

Action代码:

package com.mobilesoft.esales.webapp.action;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import jxl.demo.CSV;

import org.apache.commons.beanutils.BeanUtils;
import org.jmesa.facade.TableFacade;
import org.jmesa.facade.TableFacadeImpl;
import org.jmesa.limit.Limit;

import com.mobilesoft.esales.dao.hibernate.Person;
import com.mobilesoft.esales.service.PersonService;
import com.octo.captcha.service.CaptchaServiceException;
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 sun.text.CompactShortArray.Iterator;

/**
 * 用于演示基于jmesa(http://code.google.com/p/jmesa/)的分页、排序组件的使用方法,
 * 在src/java/com/mobilesoft/esales/dao/hibernate/person.sql有Person表的测试数据
 * @author <a href=”mailto:liangchuan@mobile-soft.cn”>liangchuan</a> 
 * @since 2008-03
 */
public class PersonAction extends BaseAction {
    private PersonService personService;
    private List<Person> persons;
    private Person person;
    private Integer id;
    private String tableId;

    public String execute() {
        this.persons = personService.findAll();
        //创建id为tableId为表单
        //<table id=”tableId”  border=”0″  cellpadding=”0″  cellspacing=”0″  class=”table”  width=”600px” >
        TableFacade tableFacade = new TableFacadeImpl(“tableId”, getRequest());
        //设定页面分页数据
        tableFacade.setItems(persons);
        //设定支持的查询结果导出格式为csv,excel,pdf格式
        tableFacade.setExportTypes(getResponse(), CSV, JEXCEL, PDF);
        tableFacade.setStateAttr(“restore”);
        Limit limit = tableFacade.getLimit();
        if (limit.isExported()) {
            export(tableFacade);
            return null;
        } else {
            String html = html(tableFacade);
            getRequest().setAttribute(“myhtml”, html);
        }
        return Action.SUCCESS;
    }

    private String html(TableFacade tableFacade) {
        // 设定表格属性,注意此处的url用于诸如增加、删除、修改、查询操作,并不是实际的数据库表属性,
        //但表单需要有对应的po对新,因此需要在Person中增加此属性
        tableFacade.setColumnProperties(“id”, “firstName”, “lastName”, “url”);

        HtmlTable table = (HtmlTable) tableFacade.getTable();
        table.setCaption(“测试用户信息列表”);
        table.getTableRenderer().setWidth(“600px”);

        HtmlRow row = table.getRow();

        HtmlColumn id = row.getColumn(“id”);
        id.setTitle(“id”);

        HtmlColumn firstName = row.getColumn(“firstName”);
        firstName.setTitle(“属性1″);

        HtmlColumn lastName = row.getColumn(“lastName”);
        lastName.setTitle(“属性2″);

        HtmlColumn deleteAction = row.getColumn(“url”);
        deleteAction.setTitle(“操作”);

        // 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 BasicCellEditor().getValue(item, property,
                        rowcount);
                HtmlBuilder html = new HtmlBuilder();
                html.a().href().quote().append(“http://www.mobile-soft.cn”)
                        .quote().close();
                html.append(value);
                html.aEnd();
                return html.toString();
            }
        });

        // Using an anonymous class to implement a custom editor.
        //用于演示在表格中增加javascript操作,通过jquery来实现ajax式的删除操作
        deleteAction.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=” onclick=’javascript:del(\”tableId\”,”+id+“) ‘”;
                html.a().append(js).href().quote().append(getRequest().getContextPath()+“/remove.action?id=”+id).quote().close();
                html.append(“删除”);
                html.aEnd();
                return html.toString();
            }
        });

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

    private void export(TableFacade tableFacade) {
        tableFacade.setColumnProperties(“id”, “firstName”, “lastName”);

        Table table = tableFacade.getTable();
        table.setCaption(“Persons Test”);

        Row row = table.getRow();

        Column id = row.getColumn(“id”);
        id.setTitle(“id”);

        Column firstName = row.getColumn(“firstName”);
        firstName.setTitle(“First Name”);

        Column lastName = row.getColumn(“lastName”);
        lastName.setTitle(“Last Name”);

        tableFacade.render();
    }

    public String login() {
        Boolean isResponseCorrect = Boolean.FALSE;
        // remenber that we need an id to validate!
        String captchaId = getSession().getId();
        // retrieve the response
        String response = getRequest().getParameter(“j_captcha_response”);
        // Call the Service method
        try {
            isResponseCorrect = CaptchaServiceSingleton.getInstance()
                    .validateResponseForID(captchaId, response);
        } catch (CaptchaServiceException e) {
            // should not happen, may be thrown if the id is not valid
        }
        if (!isResponseCorrect) {
            return Action.LOGIN;
        }
        return execute();
    }

    public String save() {
        this.personService.save(person);
        this.person = new Person();
        return execute();
    }
    /**
     * 用于演示ajax方式删除操作,参看pages/list.jsp
     * @return
     */
    public String remove() {
        String deleteId = getRequest().getParameter(“id”);

        if (deleteId != null) {
            personService.remove(Integer.parseInt(deleteId));
        }
        this.persons = personService.findAll();
        TableFacade tableFacade = new TableFacadeImpl(“tableId”, getRequest());
        tableFacade.setItems(persons); // set the items
        tableFacade.setExportTypes(getResponse(), CSV, JEXCEL, PDF);
        tableFacade.setStateAttr(“restore”);
        Limit limit = tableFacade.getLimit();
        if (limit.isExported()) {
            export(tableFacade);
            return null;
        } else {
            String html = html(tableFacade);
            getRequest().setAttribute(“myhtml”, html);
        }
        return Action.SUCCESS;

    }

    public List<Person> getPersons() {
        return persons;
    }

    public Integer getId() {
        return id;
    }

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

    public void prepare() throws Exception {
        if (id != null)
            person = personService.find(id);
    }

    public Person getPerson() {
        return person;
    }

    public void setPerson(Person person) {
        this.person = person;
    }

    /**
     * @return the personService
     */
    public PersonService getPersonService() {
        return personService;
    }

    /**
     * @param personService
     *            the personService to set
     */
    public void setPersonService(PersonService personService) {
        this.personService = personService;
    }

    /**
     * @return the tableId
     */
    public String getTableId() {
        return tableId;
    }

    /**
     * @param tableId
     *            the tableId to set
     */
    public void setTableId(String tableId) {
        this.tableId = tableId;
    }

}

Model代码:

package com.mobilesoft.esales.dao.hibernate;

/**
 * Person entity.
 * 
 * @author <a href=”mailto:liangchuan@mobile-soft.cn”>liangchuan</a>

 */

public class Person implements java.io.Serializable {

    // Fields

    private Integer id;
    private String firstName;
    private String lastName;
    private String url=“”;

    // Constructors

    /** default constructor */
    public Person() {
    }

    /** full constructor */
    public Person(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }

    // Property accessors

    public Integer getId() {
        return this.id;
    }

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

    public String getFirstName() {
        return this.firstName;
    }

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

    public String getLastName() {
        return this.lastName;
    }

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

    /**
     * @return the url
     */
    public String getUrl() {
        return url;
    }

    /**
     * @param url the url to set
     */
    public void setUrl(String url) {
        this.url = url;
    }

}

Struts.xml:

<?xml version=”1.0″ encoding=”UTF-8″?>
<!DOCTYPE struts PUBLIC
    “-//Apache Software Foundation//DTD Struts Configuration 2.0//EN”
    “http://struts.apache.org/dtds/struts-2.0.dtd”>
<struts>
    <constant name=”struts.objectFactory” value=”spring”/>
    <constant name=”struts.devMode” value=”true”/>
    <constant name=”struts.i18n.encoding” value=”GBK”/>
    <constant name=”struts.action.extension” value=”action”/>
    <constant name=”struts.custom.i18n.resources” value=”ApplicationResources,errors”/>
    <constant name=”struts.multipart.maxSize” value=”2097152″/>
    <constant name=”struts.multipart.saveDir” value=”/resources”/>
    <constant name=”struts.ui.theme” value=”css_xhtml”/>
    <constant name=”struts.enable.SlashesInActionNames” value=”true”/>    

    <package name=”person” extends=”struts-default”>
     <interceptors>
            <!– Copied from struts-default.xml and changed validation exclude methods –>
            <interceptor-stack name=”defaultStack”>
                <interceptor-ref name=”exception”/>
                <interceptor-ref name=”alias”/>
                <interceptor-ref name=”servlet-config”/>
                <interceptor-ref name=”prepare”/>
                <interceptor-ref name=”i18n”/>
                <interceptor-ref name=”chain”/>
                <interceptor-ref name=”debugging”/>
                <interceptor-ref name=”profiling”/>
                <interceptor-ref name=”scoped-model-driven”/>
                <interceptor-ref name=”model-driven”/>
                <interceptor-ref name=”fileUpload”/>
                <interceptor-ref name=”checkbox”/>
                <interceptor-ref name=”static-params”/>
                <interceptor-ref name=”params”>
                    <param name=”excludeParams”>dojo\..*</param>
                </interceptor-ref>
                <interceptor-ref name=”conversionError”/>
                <interceptor-ref name=”validation”>
                    <param name=”excludeMethods”>cancel,execute,delete,edit,list</param>
                </interceptor-ref>
                <interceptor-ref name=”workflow”>
                    <param name=”excludeMethods”>input,back,cancel,browse</param>
                </interceptor-ref>
            </interceptor-stack>
            <interceptor-stack name=”fileUploadStack”>
                <interceptor-ref name=”fileUpload”/>
                <interceptor-ref name=”defaultStack”/>
            </interceptor-stack>
        </interceptors>
        <global-results>
            <result name=”mainMenu” type=”redirect”>mainMenu.html</result>
            <result name=”dataAccessFailure”>pages/dataAccessFailure.jsp</result>
        </global-results>

        <global-exception-mappings>
            <exception-mapping exception=”org.springframework.dao.DataAccessException” result=”dataAccessFailure”/>
        </global-exception-mappings>

        <action name=”list” method=”execute” class=”personAction”>
            <result name=”success”>pages/list.jsp</result>
            <result name=”input”>pages/list.jsp</result>
        </action>

        <action name=”remove” class=”personAction” method=”remove”>
            <result name=”success”>pages/list.jsp</result>
            <result name=”input”>pages/list.jsp</result>
        </action>

        <action name=”save” class=”personAction” method=”save”>
            <result name=”success”>pages/list.jsp</result>
            <result name=”input”>pages/list.jsp</result>
        </action>

        <action name=”login” class=”personAction” method=”execute”>
            <result name=”success”>pages/list.jsp</result>
            <result name=”login”>/index.jsp</result>
        </action>

        <action name=”uploadFile”
            class=”com.mobilesoft.esales.webapp.action.FileUploadAction”>
            <interceptor-ref name=”fileUploadStack”/>
            <result name=”input”>pages/uploadForm.jsp</result>
            <result name=”success”>pages/uploadDisplay.jsp</result>
            <result name=”cancel” type=”redirect”>/index.jsp</result>
        </action>

    </package>

</struts>
 
分享到:
评论
8 楼 possibleman 2009-02-03  
你这样写就已经忘记Action层 的意义了
7 楼 possibleman 2009-02-03  
Action都像你这样写还不把人给写死啊
6 楼 bluelzx 2008-11-06  
yongtree 写道

怎样做数据库的分页处理啊

mysql limit m,n或者数据量大就写个分页的存储过程
5 楼 bluelzx 2008-11-06  
etao528 写道

最近正在学习这方面的知识,能否提供一份源代码谢谢哦!哈
etao528@163.com

好久没有上来这里了 sorry
4 楼 yongtree 2008-11-04  
怎样做数据库的分页处理啊
3 楼 etao528 2008-06-24  
最近正在学习这方面的知识,能否提供一份源代码谢谢哦!哈
etao528@163.com
2 楼 etao528 2008-06-23  
有源码吗?谢谢!正在学习中
1 楼 xianhui 2008-06-05  
初学者,照猫画虎,一直没有成功

相关推荐

    Struts2+Spring+Hibernate+Ehcache+AJAX+JQuery+Oracle 框架集成用户登录注册Demo工程

    2.Action里通过struts2-spring-plugin.jar插件自动根据名字注入。 3.Ajax无刷新异步调用Struts2,返回Json数据,以用户注册为例。 4.在服务端分页查询功能,优点:实时性:跳页才查询。数据量小:只加载当前页的记录...

    struts2+spring3+hibernate3+jquery+flexigrid+mysql实例

    Struts2、Spring3、Hibernate3、jQuery、Flexigrid 和 MySQL 是一组常见的技术栈,用于构建企业级的Java Web应用程序。以下是对这些技术及其在实际应用中的详细说明: 1. **Struts2**:Struts2 是一个基于 MVC...

    Struts2 + Spring3 + Hibernate3.5 整合(实际使用项目,version2)

    本版本全面更新了jar包,全部使用了当前最新版本的jar包,struct2.1.8 spring3 hibernate3.5,全面使用注解取代xm的l配置。 另外增加了一个ant构建脚本,支持使用hudson完成每日构建,持续集成,自动测试,代码规范...

    Struts2 + Spring 2.5 + Hibernate 3.3 整合(实际使用项目,version1)

    包含有完整的jar包和源代码,这是专门为我们实验室定制开发的,包含了架构基于s2sh技术网站的参考实现(包括了全部基础部分:如分页,缓存,文件上传,连接池等等)希望对初学JavaEE WEB开的人有所帮助。...

    jquerygrid+spring+struts+hibernate

    在IT行业中,jQuery Grid(通常简称为jGrid)是一个强大的JavaScript数据网格插件,它能够与后端的数据存储系统如Spring、Struts和Hibernate等Java框架无缝集成,以实现高效的数据展示和管理。这个名为"jquerygrid+...

    开发基于Struts Spring Hibernate Ajax的网上信息发布平台(Struts Hibernate Spring Ajax)--Chapter1

    在本章中,我们将深入探讨如何开发一个基于Struts、Spring、Hibernate和Ajax的网上信息发布平台。这四个技术是Java Web开发中的核心组件,它们各自承担着不同的职责,共同构建了一个强大而灵活的后端架构。 **...

    struts2+hibernate+Spring后台表格分页

    在这个“Struts2+Hibernate+Spring后台表格分页”项目中,我们将深入探讨如何整合这三个框架实现数据的动态展示和分页。 1. **Struts2框架**:Struts2作为MVC(模型-视图-控制器)架构的实现,主要负责处理用户的...

    Struts2 + Spring3 + Hibernate3.5 整合(集成测试配套jar包更新构建脚本使用说明)

    本版本全面更新了jar包,全部使用了当前最新版本的jar包,struct2.1.8 spring3 hibernate3.5,全面使用注解取代xm的l配置。 另外增加了一个ant构建脚本,支持使用hudson完成每日构建,持续集成,自动测试,代码规范...

    struts2+spring+hibernate+easyui

    Struts2、Spring、Hibernate和EasyUI是Java Web开发中常用的四大框架,它们结合使用可以构建高效、可维护的企业级应用程序。在这个项目中,开发者利用这些技术实现了基础的数据操作,如增删查改,以及用户登录、注册...

    jqueryUi+jqGrid+spring+hibernate+struts1.2+mysql 完美例子(带数据库文件)

    本例程,主要是,用jqgrid 实现grid及subgrid数据列表的分页,增,删,改,查,定制显示列的功能 用jqueryUI 实现 上下左的布局 数据库及源码都在上传的RAR包中 由于上传空间的问题,JAR包不做上传,大家可以自己...

    电影后台管理系统Spring+struts2+Hibernate+easyui

    电影后台管理系统是一个典型的Web应用程序,它使用了Spring、Struts2和Hibernate这三大框架,以及前端的EasyUI组件库来实现高效、稳定的管理功能。这个系统没有采用Maven作为项目构建工具,而是采取了传统的手动依赖...

    Struts2 + Spring3 + Hibernate3.5 整合(实际使用项目,version3).part1

    本版本全面更新了jar包,全部使用了当前最新版本的jar包,struct2.1.8 spring3 hibernate3.5,全面使用注解取代xm的l配置。 另外增加了一个ant构建脚本,支持使用hudson完成每日构建,持续集成,自动测试,代码规范...

    Struts2 + Spring3 + Hibernate3.5 整合(实际使用项目,version3).part3

    本版本全面更新了jar包,全部使用了当前最新版本的jar包,struct2.1.8 spring3 hibernate3.5,全面使用注解取代xm的l配置。 另外增加了一个ant构建脚本,支持使用hudson完成每日构建,持续集成,自动测试,代码规范...

    JQuery,ajax,hibernate+spring,分页查询.rar

    综合来看,这个压缩包包含了一系列关于使用JQuery实现Ajax分页和查询,以及Hibernate和Spring整合进行分页查询的教程和实例代码。对于学习和理解这些技术在实际项目中的应用,这些资源是非常宝贵的。开发者可以通过...

    DWR+jquery2.x+easyUI1.3.x开发富客户端应用

    1. **SSH+jQuery+DWR+EasyUI 实战**:本文档通过一系列的实战案例展示了如何将 Struts2、Spring3、Hibernate4 与 DWR、jquery2.x 和 easyUI1.3.x 结合起来开发实际的应用。 2. **实战案例详解**: - SSH 架构集成...

    Spring2.5+Struts2.0+hibernate3.0+Dwr+jquery+displayTag

    2 利用struts2 的LoginAction-validation.xml 3 在bean里把service包;暴露DWR,写了一个验证用户名的流程 4 采用jpa作为POJO,还是减少配置 5 加入display的分页,并且是物理分页 打开后自已建表sql.txt jdbc....

    jquery java struts2 实现分页 非常好看的分页

    本教程将详细讲解如何利用jQuery、Java和Struts2框架来实现一个美观且可自定义样式的分页功能。 首先,jQuery是一个广泛使用的JavaScript库,它简化了HTML文档遍历、事件处理、动画以及Ajax交互等任务。在分页场景...

    SSH整合(struts2+hibernate+spring)

    用SSH做的简易论坛系统,功能算丰富,CRUD,分页,表关联,完全可以做毕业设计,导入即可运行,数据库用的是oracle,前台使用了jquery,登录,注册都是弹出层。开始先要对板块插入数据,表可以用hibernate的自动创建...

    基于SSH2+EasyUI图书管理系统.zip

    后台采用技术: struts 2 + Spring + Hibernate (SSH2) 前台技术: jquery + easyui框架 所有的数据提交和查询都是通过ajax方式异步传输! 图书数据的添加和查询都是在这个js文件中实现 实现语言是javascript ...

    SSH(Spring+Struts+Hibernate)轻量级购物软件Shopping演示系统(Java Web Edition)

    SSH(Spring+Struts+Hibernate)轻量级购物软件Shopping演示系统(Java Web Edition)技术要点:Spring+Struts+Hibernate三大框架整合,Jquery-treeview、DIV+CSS3、HTML5、JavaScript、分页组件pagination、MySQL。

Global site tag (gtag.js) - Google Analytics