ITeye博客

用eXtremeComponents做分页-简单方便

junner · 2015-05-12 16:38 · eXtremeComponents · Web前端 · Markdown

eXtremeComponents是提供更高级数据显示的开源JSP标签,用于以表格的形式来显示数据,它的功能强大且使用简单,常用的功能包括排序、分页、导出Excel与pdf等。使用ExtremeComponents列表组件,你需要去http://sourceforge.net/projects/extremecomp/下载发布的压缩包文件。不过现在更推荐使用javaEye里的GT-Grid ( 原名叫EC Side ), 地址:http://ecside.group.iteye.com/ ,大家可以去那里学习与讨论。

 

EC Side也是一个开源的列表组件,他是源自于开源列表组件 eXtremeComponents,不过已经早已脱离了eXtremeComponents而独立发展(里面仍有部分代码是来自 eXtremeComponents)。青出于蓝而胜于蓝,EC Side比eXtremeComponents的功能更强大。一般我们建议先学习和使用一下eXtremeComponents,再去学习和使用EC Side.

 

以下是一个eXtremeComponents使用的示例,用来演示一下ExtremeComponents列表组件进行数据库数据分页显示的使用,更多跟深入的学习,您可以参考http://www.blogjava.net/lucky/articles/33380.html 或者http://blog.chinaunix.net/u/7893/showart_426623.html。

 

完成下面的示例,需要按照如下的步骤进行:
先把一些准备工作做好,在MyEclipse新建web工程,在下载的ExtremeComponents压缩包中找到

commons-beanutils 1.7
commons-collections 3.0
commons-lang 2.0   
commons-logging 1.0.4
standard 1.0.2
extremecomponents-1.0.1.jar

将以上jar包和extremecomponents.tld文件拷贝到WEB-INF/lib目录下,并且把images整个文件夹拷贝到WebRoot下,以上的所有的jar包和文件都在下载的压缩包里可以找到,至此准备工作就完成了 。

 

1、建立数据库数据:
create database page;

use page;

create table student
(
stu_id integer auto_increment,
stuName varchar(255) not null,
address varchar(255) not null,
stuPhone varchar(255)not null,
primary key(stu_id)
);

insert into student(stuName,address,stuPhone) values('杨老板,'长沙','13787825190');

insert into student(stuName,address,stuPhone) values('王老板,'大连','13782251560');

这里可以复制上面的insert语句多插入几行不同的数据.

 

2、写一个类StudDAO.java来操作数据库,从数据库中取出数据,该类的代码如下:

Java代码  收藏代码

  1. package org.hnylj.eXtreme;
  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. import java.sql.PreparedStatement;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.sql.Statement;
  8. import java.util.ArrayList;
  9. import java.util.HashMap;
  10. import java.util.List;
  11. import java.util.Map;
  12. public class StudDAO {
  13. private Connection conn = null ;
  14. private Statement stmt = null ;
  15. private PreparedStatement pstmt = null ;
  16. private ResultSet rs = null ;
  17. private static final String DRIVER = "com.mysql.jdbc.Driver" ;
  18. private static final String URL = "jdbc:mysql://localhost:3306/page" ;
  19. private static final String USERNAME = "root" ;
  20. private static final String PASSWORD = "123" ;
  21. private List<Map<String,String>> list = new ArrayList<Map<String,String>>() ;
  22. private Map<String,String> map = null ;
  23. public StudDAO () {
  24. }
  25. //数据库连接
  26. public synchronized Connection getConnection () {
  27. try {
  28. Class.forName (DRIVER) ;
  29. conn = DriverManager.getConnection (URL,USERNAME,PASSWORD) ;
  30. } catch (ClassNotFoundException e) {
  31. e.printStackTrace () ;
  32. return null ;
  33. } catch (SQLException e) {
  34. e.printStackTrace () ;
  35. return null ;
  36. }
  37. return conn ;
  38. }
  39. public List<Map<String,String>> query () {
  40. try {
  41. if (this.getConnection()!=null) {
  42. pstmt = this.getConnection().prepareStatement("select * from student") ;
  43. rs = pstmt.executeQuery () ;
  44. while (rs.next()) {
  45. map = new HashMap<String,String>() ;
  46. map.put("stuName", rs.getString("stuName")) ;
  47. map.put("address", rs.getString("address")) ;
  48. map.put("stuPhone", rs.getString("stuPhone")) ;
  49. list.add(map) ;
  50. }
  51. }
  52. } catch(SQLException e) {
  53. e.printStackTrace() ;
  54. }
  55. return list ;
  56. }
  57. }

3、编写一个jsp页面,用来显示数据,其代码如下:

Html代码  收藏代码

  1. <%@ page language="java" import="java.util.*,org.hnylj.eXtreme.*"
  2. pageEncoding="gb2312"%>
  3. <%@ taglib uri="WEB-INF/extremecomponents.tld" prefix="ec"%>
  4. <html>
  5. <head>
  6. <title>用extremecomponents分页</title>
  7. <style type="text/css">
  8. .eXtremeTable {
  9. margin: 0;
  10. padding: 0;
  11. }
  12. .eXtremeTable select {
  13. font-family: Verdana;
  14. font-size: 9px;
  15. border: solid 1px #EEE;
  16. width: 75px;
  17. }
  18. .eXtremeTable .tableRegion {
  19. border: 1px solid silver;
  20. padding: 2px;
  21. font-family: Verdana;
  22. font-size: 10px;
  23. margin-top: 7px;
  24. }
  25. .eXtremeTable .filter {
  26. background-color: #efefef;
  27. }
  28. .eXtremeTable .filter input {
  29. font-family: Verdana;
  30. font-size: 10px;
  31. width: 100%;
  32. }
  33. .eXtremeTable .filter select {
  34. font-family: Verdana;
  35. font-size: 9px;
  36. border: solid 1px #EEE;
  37. width: 100%;
  38. }
  39. .eXtremeTable .tableHeader {
  40. background-color: #308dbb;
  41. color: white;
  42. font-family: Verdana;
  43. font-size: 11px;
  44. font-weight: bold;
  45. text-align: left;
  46. padding-right: 3px;
  47. padding-left: 3px;
  48. padding-top: 4;
  49. padding-bottom: 4;
  50. margin: 0;
  51. border-right-style: solid;
  52. border-right-width: 1px;
  53. border-color: white;
  54. }
  55. .eXtremeTable .tableHeaderSort {
  56. background-color: #3a95c2;
  57. color: white;
  58. font-family: Verdana;
  59. font-size: 11px;
  60. font-weight: bold;
  61. text-align: left;
  62. padding-right: 3px;
  63. padding-left: 3px;
  64. padding-top: 4;
  65. padding-bottom: 4;
  66. border-right-style: solid;
  67. border-right-width: 1px;
  68. border-color: white;
  69. }
  70. .eXtremeTable .odd a,.even a {
  71. color: Black;
  72. font-size: 10px;
  73. }
  74. .eXtremeTable .odd td,.eXtremeTable .even td {
  75. padding-top: 2px;
  76. padding-right: 3px;
  77. padding-bottom: 2px;
  78. padding-left: 3px;
  79. vertical-align: middle;
  80. font-family: Verdana;
  81. font-size: 10px;
  82. }
  83. .eXtremeTable .odd {
  84. background-color: #FFFFFF;
  85. }
  86. .eXtremeTable .even {
  87. background-color: #dfe4e8;
  88. }
  89. .eXtremeTable .highlight td {
  90. color: black;
  91. font-size: 10px;
  92. padding-top: 2px;
  93. padding-right: 3px;
  94. padding-bottom: 2px;
  95. padding-left: 3px;
  96. vertical-align: middle;
  97. background-color: #fdecae;
  98. }
  99. .eXtremeTable .highlight a,.highlight a {
  100. color: black;
  101. font-size: 10px;
  102. }
  103. .eXtremeTable .toolbar {
  104. background-color: #F4F4F4;
  105. font-family: Verdana;
  106. font-size: 9px;
  107. margin-right: 1px;
  108. border-right: 1px solid silver;
  109. border-left: 1px solid silver;
  110. border-top: 1px solid silver;
  111. border-bottom: 1px solid silver;
  112. }
  113. .eXtremeTable .toolbar td {
  114. color: #444444;
  115. padding: 0px 3px 0px 3px;
  116. text-align: center;
  117. }
  118. .eXtremeTable .separator {
  119. width: 7px;
  120. }
  121. .eXtremeTable .statusBar {
  122. background-color: #F4F4F4;
  123. font-family: Verdana;
  124. font-size: 10px;
  125. }
  126. .eXtremeTable .filterButtons {
  127. background-color: #efefef;
  128. text-align: right;
  129. }
  130. .eXtremeTable .title {
  131. color: #444444;
  132. font-weight: bold;
  133. font-family: Verdana;
  134. font-size: 15px;
  135. vertical-align: middle;
  136. }
  137. .eXtremeTable .title span {
  138. margin-left: 7px;
  139. }
  140. .eXtremeTable .formButtons {
  141. display: block;
  142. margin-top: 10px;
  143. margin-left: 5px;
  144. }
  145. .eXtremeTable .formButton {
  146. cursor: pointer;
  147. font-family: Verdana;
  148. font-size: 10px;
  149. font-weight: bold;
  150. background-color: #308dbb;
  151. color: white;
  152. margin-top: 5px;
  153. border: outset 1px #333;
  154. vertical-align: middle;
  155. }
  156. .eXtremeTable .tableTotal {
  157. background-color: #FFFFFF;
  158. border-top: solid 1px Silver;
  159. }
  160. .eXtremeTable .tableTotalEmpty {
  161. background-color: #FFFFFF;
  162. }
  163. </style>
  164. </head>
  165. <body style="margin: 25px;">
  166. <%
  167. StudDAO studDAO = new StudDAO() ;
  168. List<Map<String,String>> list = studDAO.query() ;
  169. request.setAttribute("studList", list) ;
  170. %>
  171. <ec:table items="studList"
  172. action="${pageContext.request.contextPath}/result.jsp"
  173. imagePath="${pageContext.request.contextPath}/images/table/*.gif"
  174. title="学员信息表" width="60%" rowsDisplayed="5">
  175. <ec:row>
  176. <ec:column property="stuName" />
  177. <ec:column property="address" />
  178. <ec:column property="stuPhone" />
  179. </ec:row>
  180. </ec:table>
  181. </body>
  182. </html>

4、部署整个web工程到Tomcat下,启动Tomcat,在浏览器中输入:

http://localhost:8080/eXtremeDemo/result.jsp

至此,就可以在页面中看到从数据库中查询出来的数据,而且自动进行了分页显示,有上一页,下一页,首页,末页,这样给我们剩去了很多进行分页显示的代码,在不做其他考虑的情况下,使用该组件大大提高了工作的效率。

(只是一个举例,代码写得有点乱,重在了解如何使用该组件)

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。