SSH分页实现
来源:互联网 发布:淘宝卖家图片轮播尺寸 编辑:程序博客网 时间:2024/04/28 01:19
请大家伙多多指教:
邮箱:weimingweicom@sina.com
请关注:ailiandeziwei
近日学习Easyui,发现非常好用,界面很美观。将学习的心得在此写下,这篇博客写SSh结合Easyui实现Datagrid的分页显示,其他的例如添加、修改、删除、批量删除等功能将在后面的博客一一写来。
首先看一下要实现的效果:当每页显示5行数据:
当每页显示10行数据,效果如下:
具体步骤:
1、下载Easyui,并搭建环境。可参照博客 http://blog.csdn.net/lhq13400526230/article/details/9148299
2、搭建SSH工程,整个工程的目录结构如图所示:
3、在Oracle数据库中创建表Student。并且输入下面6行数据,因为添加操作还没有实现,所以先在数据库表中添加数据。默认设定的值是每行5个数据,所以请至少输入6行数据,便于分页的测试。
4、web.xml的配置
[html] view plaincopy
1 <?xml version="1.0" encoding="UTF-8"?>
2 <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
5 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
6
7 <!-- Sttuts2过滤器 -->
8 <filter>
9 <filter-name>struts2</filter-name>
10 <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
11 </filter>
12 <filter-mapping>
13 <filter-name>struts2</filter-name>
14 <url-pattern>/*</url-pattern>
15 </filter-mapping>
16
17 <!-- 监听器Spring -->
18 <listener>
19 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
20 </listener>
21
22 <!-- 定位applicationContext.xml的物理位置 -->
23 <context-param>
24 <param-name>contextConfigLocation</param-name>
25 <param-value>classpath:applicationContext.xml</param-value>
26 </context-param>
27
28 </web-app>
5、applicationContext.xml的配置
[html] view plaincopy
29 <?xml version="1.0" encoding="UTF-8"?>
30 <beans xmlns="http://www.springframework.org/schema/beans"
31 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
32 xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
33 xsi:schemaLocation="http://www.springframework.org/schema/beans
34 http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
35 http://www.springframework.org/schema/context
36 http://www.springframework.org/schema/context/spring-context-2.5.xsd
37 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
38 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
39
40
41
42 <import resource="applicationContext_bean.xml"/>
43 <import resource="applicationContext_db.xml"/>
44 </beans>
6、在com.model中创建模型类Student.java
[java] view plaincopy
45 package com.model;
46
47 public class Student {
48 String studentid;// 主键
49 String name;// 姓名
50 String gender;// 性别
51 String age;// 年龄
52
53 public String getStudentid() {
54 return studentid;
55 }
56
57 public void setStudentid(String studentid) {
58 this.studentid = studentid;
59 }
60
61 public String getName() {
62 return name;
63 }
64
65 public void setName(String name) {
66 this.name = name;
67 }
68
69 public String getGender() {
70 return gender;
71 }
72
73 public void setGender(String gender) {
74 this.gender = gender;
75 }
76
77 public String getAge() {
78 return age;
79 }
80
81 public void setAge(String age) {
82 this.age = age;
83 }
84
85 }
7、根据Student.java生成对应的映射文件Student.hbm.xml
[html] view plaincopy
86 <?xml version="1.0"?>
87 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
88 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
89 <!-- Generated 2013-6-23 23:31:47 by Hibernate Tools 3.4.0.CR1 -->
90 <hibernate-mapping>
91 <class name="com.model.Student" table="STUDENT">
92 <id name="studentid" type="java.lang.String">
93 <column name="STUDENTID" />
94 <generator class="assigned" />
95 </id>
96 <property name="name" type="java.lang.String">
97 <column name="NAME" />
98 </property>
99 <property name="gender" type="java.lang.String">
100 <column name="GENDER" />
101 </property>
102 <property name="age" type="java.lang.String">
103 <column name="AGE" />
104 </property>
105 </class>
106 </hibernate-mapping>
8、编写接口StudentService.java
[java] view plaincopy
107 package com.service;
108
109 import java.util.List;
110
111 public interface StudentService {
112 public List getStudentList(String page,String rows) throws Exception;//根据第几页获取,每页几行获取数据
113 public int getStudentTotal() throws Exception;//统计一共有多少数据
114 }
9、编写接口的实现类StudentServiceImpl.java
[java] view plaincopy
115 package com.serviceImpl;
116
117 import java.util.List;
118
119 import org.hibernate.SessionFactory;
120
121 import com.service.StudentService;
122
123 public class StudentServiceImpl implements StudentService {
124 private SessionFactory sessionFactory;
125
126 // 根据第几页获取,每页几行获取数据
127 public List getStudentList(String page, String rows) {
128
129 //当为缺省值的时候进行赋值
130 int currentpage = Integer.parseInt((page == null || page == "0") ? "1": page);//第几页
131 int pagesize = Integer.parseInt((rows == null || rows == "0") ? "10": rows);//每页多少行
132
133 List list = this.sessionFactory.getCurrentSession().createQuery("from Student")
134 .setFirstResult((currentpage - 1) * pagesize).setMaxResults(pagesize).list();
135
136 return list;
137 }
138
139 // 统计一共有多少数据
140 public int getStudentTotal() throws Exception {
141 return this.sessionFactory.getCurrentSession().find("from Student").size();
142 }
143
144 public SessionFactory getSessionFactory() {
145 return sessionFactory;
146 }
147
148 public void setSessionFactory(SessionFactory sessionFactory) {
149 this.sessionFactory = sessionFactory;
150 }
151
152
153 }
10、配置连接数据库的配置文件applicationContext_db.xml
[html] view plaincopy
154 <?xml version="1.0" encoding="UTF-8"?>
155 <beans xmlns="http://www.springframework.org/schema/beans"
156 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
157 xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
158 xsi:schemaLocation="http://www.springframework.org/schema/beans
159 http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
160 http://www.springframework.org/schema/context
161 http://www.springframework.org/schema/context/spring-context-2.5.xsd
162 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
163 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
164
165
166 <!-- 用Bean定义数据源 -->
167 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
168 destroy-method="close">
169 <!-- 定义数据库驱动 -->
170 <property name="driverClass">
171 <value>oracle.jdbc.driver.OracleDriver</value>
172 </property>
173 <!-- 定义数据库URL -->
174 <property name="jdbcUrl">
175 <value>jdbc:oracle:thin:@localhost:1521:orcl</value>
176 </property>
177 <!-- 定义数据库的用户名 -->
178 <property name="user">
179 <value>lhq</value>
180 </property>
181 <!-- 定义数据库的密码 -->
182 <property name="password">
183 <value>lhq</value>
184 </property>
185 <property name="minPoolSize">
186 <value>1</value>
187 </property>
188 <property name="maxPoolSize">
189 <value>40</value>
190 </property>
191 <property name="maxIdleTime">
192 <value>1800</value>
193 </property>
194 <property name="acquireIncrement">
195 <value>2</value>
196 </property>
197 <property name="maxStatements">
198 <value>0</value>
199 </property>
200 <property name="initialPoolSize">
201 <value>2</value>
202 </property>
203 <property name="idleConnectionTestPeriod">
204 <value>1800</value>
205 </property>
206 <property name="acquireRetryAttempts">
207 <value>30</value>
208 </property>
209 <property name="breakAfterAcquireFailure">
210 <value>true</value>
211 </property>
212 <property name="testConnectionOnCheckout">
213 <value>false</value>
214 </property>
215
216 </bean>
217
218 <!--定义Hibernate的SessionFactory -->
219 <bean id="sessionFactory"
220 class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
221 <!-- 定义SessionFactory必须注入dataSource -->
222 <property name="dataSource">
223 <ref bean="dataSource" />
224 </property>
225 <!-- 定义Hibernate的SessionFactory属性 -->
226 <property name="hibernateProperties">
227 <props>
228 <prop key="hibernate.dialect">
229 org.hibernate.dialect.Oracle10gDialect
230 </prop>
231 </props>
232 </property>
233
234 <!-- 定义POJO的映射文件 -->
235 <property name="mappingResources">
236 <list>
237 <value>com/model/Student.hbm.xml</value>
238 </list>
239 </property>
240 </bean>
241
242 <!-- 配置事务拦截器 -->
243 <bean id="transactionManager"
244 class="org.springframework.orm.hibernate3.HibernateTransactionManager">
245 <property name="sessionFactory" ref="sessionFactory" />
246 </bean>
247
248 <tx:advice id="txAdvice" transaction-manager="transactionManager">
249 <tx:attributes>
250 <tx:method name="save*" propagation="REQUIRED" /><!-- 只有一save、delete、update开头的方法才能执行增删改操作 -->
251 <tx:method name="delete*" propagation="REQUIRED" />
252 <tx:method name="update*" propagation="REQUIRED" />
253 <tx:method name="*" propagation="SUPPORTS" read-only="true" /><!-- 其他方法为只读方法 -->
254 </tx:attributes>
255 </tx:advice>
256
257 <aop:config>
258 <aop:pointcut id="interceptorPointCuts" expression="execution(* com.serviceImpl..*.*(..))" /> <!-- 对应实现类接口的包的位置 -->
259 <aop:advisor advice-ref="txAdvice" pointcut-ref="interceptorPointCuts" />
260 </aop:config>
261
262 </beans>
11、在控制层编写StudentAction.java类型
[java] view plaincopy
263 package com.action;
264
265 import java.util.List;
266
267 import javax.servlet.http.HttpServletRequest;
268 import javax.servlet.http.HttpServletResponse;
269
270 import net.sf.json.JSONObject;
271
272 import org.apache.log4j.Logger;
273 import org.apache.struts2.ServletActionContext;
274
275 import com.service.StudentService;
276
277 public class StudentAction {
278 static Logger log = Logger.getLogger(StudentAction.class);
279 private JSONObject jsonObj;
280 private String rows;// 每页显示的记录数
281 private String page;// 当前第几页
282 private StudentService student_services;//String依赖注入
283
284 //查询出所有学生信息
285 public String getAllStudent() throws Exception {
286 log.info("查询出所有学生信息");
287
288 List list = student_services.getStudentList(page, rows);
289 this.toBeJson(list,student_services.getStudentTotal());
290
291 return null;
292 }
293
294 //转化为Json格式
295 public void toBeJson(List list,int total) throws Exception{
296 HttpServletResponse response = ServletActionContext.getResponse();
297 HttpServletRequest request = ServletActionContext.getRequest();
298
299 JSONObject jobj = new JSONObject();//new一个JSON
300 jobj.accumulate("total",total );//total代表一共有多少数据
301 jobj.accumulate("rows", list);//row是代表显示的页的数据
302
303 response.setCharacterEncoding("utf-8");//指定为utf-8
304 response.getWriter().write(jobj.toString());//转化为JSOn格式
305
306 log.info(jobj.toString());
307 }
308
309
310 public StudentService getStudent_services() {
311 return student_services;
312 }
313
314 public void setStudent_services(StudentService student_services) {
315 this.student_services = student_services;
316 }
317
318 public void setJsonObj(JSONObject jsonObj) {
319 this.jsonObj = jsonObj;
320 }
321
322 public void setRows(String rows) {
323 this.rows = rows;
324 }
325
326 public void setPage(String page) {
327 this.page = page;
328 }
329
330
331
332 }
12、编写Spring的依赖注入applicationContext_bean.xml配置文件
[html] view plaincopy
333 <?xml version="1.0" encoding="UTF-8"?>
334 <beans xmlns="http://www.springframework.org/schema/beans"
335 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
336 xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
337 xsi:schemaLocation="http://www.springframework.org/schema/beans
338 http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
339 http://www.springframework.org/schema/context
340 http://www.springframework.org/schema/context/spring-context-2.5.xsd
341 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
342 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
343
344 <!-- 业务层Service -->
345 <bean id="student_service" class="com.serviceImpl.StudentServiceImpl">
346 <property name="sessionFactory">
347 <ref bean="sessionFactory"></ref>
348 </property>
349 </bean>
350
351 <!-- 控制层Action -->
352 <bean id="student_action" class="com.action.StudentAction">
353 <property name="student_services">
354 <ref bean="student_service" />
355 </property>
356 </bean>
357
358 </beans>
13、编写struts.xml配置文件
[html] view plaincopy
359 <?xml version="1.0" encoding="UTF-8"?>
360 <!DOCTYPE struts PUBLIC
361 "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
362 "http://struts.apache.org/dtds/struts-2.0.dtd">
363 <struts>
364 <package name="Easyui" extends="json-default">
365 <!-- 学生信息 -->
366 <action name="getAllStudentAction" class="student_action" method="getAllStudent">
367 <result type="json"> </result>
368 </action>
369 </package>
370 </struts>
14、编写JSP----index.jsp
[html] view plaincopy
371 <%@ page language="java" pageEncoding="utf-8" isELIgnored="false"%>
372 <%
373 String path = request.getContextPath();
374 %>
375 <%@ taglib prefix="s" uri="/struts-tags"%>
376 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
377 <html>
378 <head>
379 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
380 <title>数字框</title>
381 <!-- 引入Jquery -->
382 <script type="text/javascript" src="<%=path%>/js/easyui/jquery-1.8.0.min.js" charset="utf-8"></script>
383 <!-- 引入Jquery_easyui -->
384 <script type="text/javascript" src="<%=path%>/js/easyui/jquery.easyui.min.js" charset="utf-8"></script>
385 <!-- 引入easyUi国际化--中文 -->
386 <script type="text/javascript" src="<%=path%>/js/easyui/locale/easyui-lang-zh_CN.js" charset="utf-8"></script>
387 <!-- 引入easyUi默认的CSS格式--蓝色 -->
388 <link rel="stylesheet" type="text/css" href="<%=path%>/js/easyui/themes/default/easyui.css" />
389 <!-- 引入easyUi小图标 -->
390 <link rel="stylesheet" type="text/css" href="<%=path%>/js/easyui/themes/icon.css" />
391
392 <script type="text/javascript">
393 $(function() {
394 $('#mydatagrid').datagrid({
395 title : 'datagrid实例',
396 iconCls : 'icon-ok',
397 width : 600,
398 pageSize : 5,//默认选择的分页是每页5行数据
399 pageList : [ 5, 10, 15, 20 ],//可以选择的分页集合
400 nowrap : true,//设置为true,当数据长度超出列宽时将会自动截取
401 striped : true,//设置为true将交替显示行背景。
402 collapsible : true,//显示可折叠按钮
403 toolbar:"#tb",//在添加 增添、删除、修改操作的按钮要用到这个
404 url:'getAllStudentAction.action',//url调用Action方法
405 loadMsg : '数据装载中......',
406 singleSelect:true,//为true时只能选择单行
407 fitColumns:true,//允许表格自动缩放,以适应父容器
408 //sortName : 'xh',//当数据表格初始化时以哪一列来排序
409 //sortOrder : 'desc',//定义排序顺序,可以是'asc'或者'desc'(正序或者倒序)。
410 remoteSort : false,
411 frozenColumns : [ [ {
412 field : 'ck',
413 checkbox : true
414 } ] ],
415 pagination : true,//分页
416 rownumbers : true//行数
417 });
418
419 });
420
421 </script>
422
423 </head>
424 <body>
425 <h2>
426 <b>easyui的DataGrid实例</b>
427 </h2>
428
429 <table id="mydatagrid">
430 <thead>
431 <tr>
432 <th data-options="field:'studentid',width:100,align:'center'">学生学号</th>
433 <th data-options="field:'name',width:100,align:'center'">姓名</th>
434 <th data-options="field:'gender',width:100,align:'center'">性别</th>
435 <th data-options="field:'age',width:100,align:'center'">年龄</th>
436 </tr>
437 </thead>
438 </table>
439
440 </body>
441 </html>
- SSH分页技术实现
- ssh实现分页显示
- SSH的分页实现
- SSH分页实现
- ssh实现查询分页
- SSH实现分页功能
- ssh实现分页查询
- ssh实现分页功能
- ssh分页的实现
- SSH:分页实现
- SSH分页(Hibernate实现简单分页查询)
- SSH分页(Hibernate实现简单分页查询)
- SSH分页(Hibernate实现简单分页查询)
- SSH中分页的实现
- SSH框架里面实现分页
- SSH整合之分页实现
- SSH 完美实现分页显示
- ssh实现简单的分页
- 使用c3p0-0.9.2实现数据库连接池
- android分享到腾讯微博
- 经典SQL语句
- 安卓对话框之----多选框(CheckBox)
- 附录A 使用OPENTOOLS API的Delphi扩展示例
- SSH分页实现
- 周一
- UNIX环境高级编程读书笔记(十)—信号 (4)
- 命名管道
- 用命令管道来实现简单的本地聊天小程序
- 用dup或dup2完成ls -l|cat功能
- dup()与dup2()
- fork与vfork
- 内核-->from gnu