一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語(yǔ)言|JavaScript|易語(yǔ)言|vb.net|

服務(wù)器之家 - 編程語(yǔ)言 - Java教程 - SSh結(jié)合Easyui實(shí)現(xiàn)Datagrid的分頁(yè)顯示

SSh結(jié)合Easyui實(shí)現(xiàn)Datagrid的分頁(yè)顯示

2020-05-14 10:41海潛 Java教程

這篇文章主要為大家詳細(xì)介紹了SSh結(jié)合Easyui實(shí)現(xiàn)Datagrid的分頁(yè)顯示的相關(guān)資料,感興趣的小伙伴們可以參考一下

      近日學(xué)習(xí)Easyui,發(fā)現(xiàn)非常好用,界面很美觀。將學(xué)習(xí)的心得在此寫下,這篇博客寫SSh結(jié)合Easyui實(shí)現(xiàn)Datagrid的分頁(yè)顯示,其他的例如添加、修改、刪除、批量刪除等功能將在后面一一寫來(lái)。

     首先看一下要實(shí)現(xiàn)的效果:當(dāng)每頁(yè)顯示5行數(shù)據(jù):

SSh結(jié)合Easyui實(shí)現(xiàn)Datagrid的分頁(yè)顯示

SSh結(jié)合Easyui實(shí)現(xiàn)Datagrid的分頁(yè)顯示

 當(dāng)每頁(yè)顯示10行數(shù)據(jù),效果如下:

SSh結(jié)合Easyui實(shí)現(xiàn)Datagrid的分頁(yè)顯示

具體步驟:

1、下載Easyui,并搭建環(huán)境。

2、搭建SSH工程,整個(gè)工程的目錄結(jié)構(gòu)如圖所示:

SSh結(jié)合Easyui實(shí)現(xiàn)Datagrid的分頁(yè)顯示

3、在Oracle數(shù)據(jù)庫(kù)中創(chuàng)建表Student。并且輸入下面6行數(shù)據(jù),因?yàn)樘砑硬僮鬟€沒(méi)有實(shí)現(xiàn),所以先在數(shù)據(jù)庫(kù)表中添加數(shù)據(jù)。默認(rèn)設(shè)定的值是每行5個(gè)數(shù)據(jù),所以請(qǐng)至少輸入6行數(shù)據(jù),便于分頁(yè)的測(cè)試。

SSh結(jié)合Easyui實(shí)現(xiàn)Datagrid的分頁(yè)顯示

4、web.xml的配置

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 
 <!-- Sttuts2過(guò)濾器 -->
 <filter>
 <filter-name>struts2</filter-name>
 <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
 </filter>
 <filter-mapping>
 <filter-name>struts2</filter-name>
 <url-pattern>/*</url-pattern>
 </filter-mapping>
 
 <!-- 監(jiān)聽(tīng)器Spring -->
 <listener>
 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>
 
 <!-- 定位applicationContext.xml的物理位置 -->
 <context-param>
 <param-name>contextConfigLocation</param-name>
 <param-value>classpath:applicationContext.xml</param-value>
 </context-param>
 
</web-app>

5、applicationContext.xml的配置

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
 xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context-2.5.xsd
      http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
 
 
<import resource="applicationContext_bean.xml"/>
<import resource="applicationContext_db.xml"/>
</beans>

6、在com.model中創(chuàng)建模型類Student.Java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package com.model;
 
public class Student {
 String studentid;// 主鍵
 String name;// 姓名
 String gender;// 性別
 String age;// 年齡
 
 public String getStudentid() {
 return studentid;
 }
 
 public void setStudentid(String studentid) {
 this.studentid = studentid;
 }
 
 public String getName() {
 return name;
 }
 
 public void setName(String name) {
 this.name = name;
 }
 
 public String getGender() {
 return gender;
 }
 
 public void setGender(String gender) {
 this.gender = gender;
 }
 
 public String getAge() {
 return age;
 }
 
 public void setAge(String age) {
 this.age = age;
 }
 
}

 7、根據(jù)Student.java生成對(duì)應(yīng)的映射文件Student.hbm.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2013-6-23 23:31:47 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
  <class name="com.model.Student" table="STUDENT">
    <id name="studentid" type="java.lang.String">
      <column name="STUDENTID" />
      <generator class="assigned" />
    </id>
    <property name="name" type="java.lang.String">
      <column name="NAME" />
    </property>
    <property name="gender" type="java.lang.String">
      <column name="GENDER" />
    </property>
    <property name="age" type="java.lang.String">
      <column name="AGE" />
    </property>
  </class>
</hibernate-mapping>

 8、編寫接口StudentService.java

?
1
2
3
4
5
6
7
8
package com.service;
 
import java.util.List;
 
public interface StudentService {
 public List getStudentList(String page,String rows) throws Exception;//根據(jù)第幾頁(yè)獲取,每頁(yè)幾行獲取數(shù)據(jù)
 public int getStudentTotal() throws Exception;//統(tǒng)計(jì)一共有多少數(shù)據(jù)
}

9、編寫接口的實(shí)現(xiàn)類StudentServiceImpl.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package com.serviceImpl;
 
import java.util.List;
 
import org.hibernate.SessionFactory;
 
import com.service.StudentService;
 
public class StudentServiceImpl implements StudentService {
 private SessionFactory sessionFactory;
 
 // 根據(jù)第幾頁(yè)獲取,每頁(yè)幾行獲取數(shù)據(jù)
 public List getStudentList(String page, String rows) {
 
    //當(dāng)為缺省值的時(shí)候進(jìn)行賦值
 int currentpage = Integer.parseInt((page == null || page == "0") ? "1": page);//第幾頁(yè)
 int pagesize = Integer.parseInt((rows == null || rows == "0") ? "10": rows);//每頁(yè)多少行
 
 List list = this.sessionFactory.getCurrentSession().createQuery("from Student")
      .setFirstResult((currentpage - 1) * pagesize).setMaxResults(pagesize).list();
 
 return list;
 }
 
 // 統(tǒng)計(jì)一共有多少數(shù)據(jù)
 public int getStudentTotal() throws Exception {
 return this.sessionFactory.getCurrentSession().find("from Student").size();
 }
 
 public SessionFactory getSessionFactory() {
 return sessionFactory;
 }
 
 public void setSessionFactory(SessionFactory sessionFactory) {
 this.sessionFactory = sessionFactory;
 }
 
 
}

 10、配置連接數(shù)據(jù)庫(kù)的配置文件applicationContext_db.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
 xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context-2.5.xsd
      http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
 
 
 <!-- 用Bean定義數(shù)據(jù)源 -->
 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
 destroy-method="close">
 <!-- 定義數(shù)據(jù)庫(kù)驅(qū)動(dòng) -->
 <property name="driverClass">
  <value>oracle.jdbc.driver.OracleDriver</value>
 </property>
 <!-- 定義數(shù)據(jù)庫(kù)URL -->
 <property name="jdbcUrl">
  <value>jdbc:oracle:thin:@localhost:1521:orcl</value>
 </property>
 <!-- 定義數(shù)據(jù)庫(kù)的用戶名 -->
 <property name="user">
  <value>lhq</value>
 </property>
 <!-- 定義數(shù)據(jù)庫(kù)的密碼 -->
 <property name="password">
  <value>lhq</value>
 </property>
 <property name="minPoolSize">
  <value>1</value>
 </property>
 <property name="maxPoolSize">
  <value>40</value>
 </property>
 <property name="maxIdleTime">
  <value>1800</value>
 </property>
 <property name="acquireIncrement">
  <value>2</value>
 </property>
 <property name="maxStatements">
  <value>0</value>
 </property>
 <property name="initialPoolSize">
  <value>2</value>
 </property>
 <property name="idleConnectionTestPeriod">
  <value>1800</value>
 </property>
 <property name="acquireRetryAttempts">
  <value>30</value>
 </property>
 <property name="breakAfterAcquireFailure">
  <value>true</value>
 </property>
 <property name="testConnectionOnCheckout">
  <value>false</value>
 </property>
 
 </bean>
 
 <!--定義Hibernate的SessionFactory -->
 <bean id="sessionFactory"
 class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
 <!-- 定義SessionFactory必須注入dataSource -->
 <property name="dataSource">
  <ref bean="dataSource" />
 </property>
 <!-- 定義Hibernate的SessionFactory屬性 -->
 <property name="hibernateProperties">
  <props>
  <prop key="hibernate.dialect">
   org.hibernate.dialect.Oracle10gDialect
  </prop>
  </props>
 </property>
 
 <!-- 定義POJO的映射文件 -->
 <property name="mappingResources">
  <list>
  <value>com/model/Student.hbm.xml</value>
  </list>
 </property>
 </bean>
 
 <!-- 配置事務(wù)攔截器 -->
 <bean id="transactionManager"
 class="org.springframework.orm.hibernate3.HibernateTransactionManager">
 <property name="sessionFactory" ref="sessionFactory" />
 </bean>
 
 <tx:advice id="txAdvice" transaction-manager="transactionManager">
 <tx:attributes>
  <tx:method name="save*" propagation="REQUIRED" /><!-- 只有一save、delete、update開(kāi)頭的方法才能執(zhí)行增刪改操作 -->
  <tx:method name="delete*" propagation="REQUIRED" />
  <tx:method name="update*" propagation="REQUIRED" />
  <tx:method name="*" propagation="SUPPORTS" read-only="true" /><!-- 其他方法為只讀方法 -->
 </tx:attributes>
 </tx:advice>
 
 <aop:config>
 <aop:pointcut id="interceptorPointCuts" expression="execution(* com.serviceImpl..*.*(..))" /> <!-- 對(duì)應(yīng)實(shí)現(xiàn)類接口的包的位置 -->
 <aop:advisor advice-ref="txAdvice" pointcut-ref="interceptorPointCuts" />
 </aop:config>
 
</beans>

11、在控制層編寫StudentAction.java類型

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package com.action;
 
import java.util.List;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import net.sf.json.JSONObject;
 
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
 
import com.service.StudentService;
 
public class StudentAction {
 static Logger log = Logger.getLogger(StudentAction.class);
 private JSONObject jsonObj;
 private String rows;// 每頁(yè)顯示的記錄數(shù)
 private String page;// 當(dāng)前第幾頁(yè)
 private StudentService student_services;//String依賴注入
 
 //查詢出所有學(xué)生信息
 public String getAllStudent() throws Exception {
 log.info("查詢出所有學(xué)生信息");
 
 List list = student_services.getStudentList(page, rows);
 this.toBeJson(list,student_services.getStudentTotal());
 
 return null;
 }
 
 //轉(zhuǎn)化為Json格式
  public void toBeJson(List list,int total) throws Exception{
  HttpServletResponse response = ServletActionContext.getResponse();
  HttpServletRequest request = ServletActionContext.getRequest();
  
  JSONObject jobj = new JSONObject();//new一個(gè)JSON
  jobj.accumulate("total",total );//total代表一共有多少數(shù)據(jù)
  jobj.accumulate("rows", list);//row是代表顯示的頁(yè)的數(shù)據(jù)
 
  response.setCharacterEncoding("utf-8");//指定為utf-8
  response.getWriter().write(jobj.toString());//轉(zhuǎn)化為JSOn格式
  
  log.info(jobj.toString());
  }
  
 
 public StudentService getStudent_services() {
 return student_services;
 }
 
 public void setStudent_services(StudentService student_services) {
 this.student_services = student_services;
 }
 
 public void setJsonObj(JSONObject jsonObj) {
 this.jsonObj = jsonObj;
 }
 
 public void setRows(String rows) {
 this.rows = rows;
 }
 
 public void setPage(String page) {
 this.page = page;
 }
  
  
 
}

12、編寫spring的依賴注入applicationContext_bean.xml配置文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
 xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context-2.5.xsd
      http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
 
 <!-- 業(yè)務(wù)層Service -->
 <bean id="student_service" class="com.serviceImpl.StudentServiceImpl">
  <property name="sessionFactory">
  <ref bean="sessionFactory"></ref>
 </property>
 </bean>
 
 <!-- 控制層Action -->
 <bean id="student_action" class="com.action.StudentAction">
 <property name="student_services">
  <ref bean="student_service" />
 </property>
 </bean>
 
</beans>

13、編寫struts.xml配置文件

?
1
2
3
4
5
6
7
8
9
10
11
12
<?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>
 <package name="Easyui" extends="json-default">
 <!-- 學(xué)生信息 -->
 <action name="getAllStudentAction" class="student_action" method="getAllStudent">
  <result type="json"> </result>
 </action>
 </package>
</struts>

14、編寫JSP----index.jsp

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<%@ page language="java" pageEncoding="utf-8" isELIgnored="false"%>
<%
 String path = request.getContextPath();
%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!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=utf-8">
<title>數(shù)字框</title>
<!-- 引入Jquery -->
<script type="text/javascript" src="<%=path%>/js/easyui/jquery-1.8.0.min.js" charset="utf-8"></script>
<!-- 引入Jquery_easyui -->
<script type="text/javascript" src="<%=path%>/js/easyui/jquery.easyui.min.js" charset="utf-8"></script>
<!-- 引入easyUi國(guó)際化--中文 -->
<script type="text/javascript" src="<%=path%>/js/easyui/locale/easyui-lang-zh_CN.js" charset="utf-8"></script>
<!-- 引入easyUi默認(rèn)的CSS格式--藍(lán)色 -->
<link rel="stylesheet" type="text/css" href="<%=path%>/js/easyui/themes/default/easyui.css" />
<!-- 引入easyUi小圖標(biāo) -->
<link rel="stylesheet" type="text/css" href="<%=path%>/js/easyui/themes/icon.css" />
 
<script type="text/javascript">
 $(function() {
 $('#mydatagrid').datagrid({
  title : 'datagrid實(shí)例',
  iconCls : 'icon-ok',
  width : 600,
  pageSize : 5,//默認(rèn)選擇的分頁(yè)是每頁(yè)5行數(shù)據(jù)
  pageList : [ 5, 10, 15, 20 ],//可以選擇的分頁(yè)集合
  nowrap : true,//設(shè)置為true,當(dāng)數(shù)據(jù)長(zhǎng)度超出列寬時(shí)將會(huì)自動(dòng)截取
  striped : true,//設(shè)置為true將交替顯示行背景。
  collapsible : true,//顯示可折疊按鈕
  toolbar:"#tb",//在添加 增添、刪除、修改操作的按鈕要用到這個(gè)
  url:'getAllStudentAction.action',//url調(diào)用Action方法
  loadMsg : '數(shù)據(jù)裝載中......',
  singleSelect:true,//為true時(shí)只能選擇單行
  fitColumns:true,//允許表格自動(dòng)縮放,以適應(yīng)父容器
  //sortName : 'xh',//當(dāng)數(shù)據(jù)表格初始化時(shí)以哪一列來(lái)排序
  //sortOrder : 'desc',//定義排序順序,可以是'asc'或者'desc'(正序或者倒序)。
  remoteSort : false,
   frozenColumns : [ [ {
  field : 'ck',
  checkbox : true
  } ] ],
  pagination : true,//分頁(yè)
  rownumbers : true//行數(shù)
 });
 
 });
 
</script>
 
</head>
<body>
 <h2>
 <b>easyui的DataGrid實(shí)例</b>
 </h2>
 
 <table id="mydatagrid">
  <thead>
  <tr>
  <th data-options="field:'studentid',width:100,align:'center'">學(xué)生學(xué)號(hào)</th>
  <th data-options="field:'name',width:100,align:'center'">姓名</th>
  <th data-options="field:'gender',width:100,align:'center'">性別</th>
  <th data-options="field:'age',width:100,align:'center'">年齡</th>
  </tr>
 </thead>
 </table>
  
</body>
</html>

 15、啟動(dòng)程序,輸入http://localhost:8080/easyui/index.jsp進(jìn)行測(cè)試。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 青草娱乐极品免费视频 | 恩不要好大好硬好爽3p | 日韩在线观看免费 | 日韩精品欧美 | 午夜一级影院 | 91在线 一区 二区三区 | 日韩成人一级 | 欧美日韩成人在线视频 | 丝瓜秋葵番茄绿巨人在线观看 | 亚洲国产精品婷婷久久久久 | ova巨公主催眠1在线观看 | 我和黑色丝袜班主任 | 青青热久免费精品视频网站 | 91综合精品网站久久 | 关晓彤被草| 全黄h全肉细节文在线观看 全彩成人18h漫画 | ffee性xxⅹ另类老妇hd | 波多野结衣中文字幕在线 | 欧美亚洲国产精品久久第一页 | 日韩视频一区二区三区 | 亚洲精品第一国产综合高清 | 国产3p绿奴在线视频 | 男人天堂网www | 精品亚洲欧美中文字幕在线看 | 国产精品久久久久影院色老大 | 国产一卡 | 精品无码国产AV一区二区三区 | 兽皇日本 | 脱了白丝校花的内裤猛烈进入 | 秋霞一级毛片 | 忘忧草研究院一二三 | 腿交hd | 桃乃木香奈ipx在线播放 | 成年人在线免费看 | 亚洲高清一区二区三区四区 | 日韩美一区二区三区 | 极品奶妈漫画 | 国产成人精品三级在线 | 亚洲第一网色综合久久 | 欧美国产日本精品一区二区三区 | 毛片一级毛片 |