程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> SSh聯合Easyui完成Datagrid的分頁顯示

SSh聯合Easyui完成Datagrid的分頁顯示

編輯:關於JAVA

SSh聯合Easyui完成Datagrid的分頁顯示。本站提示廣大學習愛好者:(SSh聯合Easyui完成Datagrid的分頁顯示)文章只能為提供參考,不一定能成為您想要的結果。以下是SSh聯合Easyui完成Datagrid的分頁顯示正文


      近日進修Easyui,發明異常好用,界面很雅觀。將進修的心得在此寫下,這篇博客寫SSh聯合Easyui完成Datagrid的分頁顯示,其他的例如添加、修正、刪除、批量刪除等功效將在前面逐個寫來。

     起首看一下要完成的後果:當每頁顯示5行數據:

 當每頁顯示10行數據,後果以下:

詳細步調:

1、下載Easyui,並搭建情況。

2、搭建SSH工程,全部工程的目次構造如圖所示:

3、在Oracle數據庫中創立表Student。而且輸出上面6行數據,由於添加操作還沒有完成,所以先在數據庫表中添加數據。默許設定的值是每行5個數據,所以請至多輸出6行數據,便於分頁的測試。

4、web.xml的設置裝備擺設

<?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過濾器 -->
 <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>

 <!-- 監聽器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的設置裝備擺設

<?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中創立模子類Student.Java

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、依據Student.java生成對應的映照文件Student.hbm.xml

<?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

package com.service;

import java.util.List;

public interface StudentService {
 public List getStudentList(String page,String rows) throws Exception;//依據第幾頁獲得,每頁幾行獲得數據 
 public int getStudentTotal() throws Exception;//統計一共有若干數據
}

9、編寫接口的完成類StudentServiceImpl.java

package com.serviceImpl;

import java.util.List;

import org.hibernate.SessionFactory;

import com.service.StudentService;

public class StudentServiceImpl implements StudentService {
 private SessionFactory sessionFactory;
 
 // 依據第幾頁獲得,每頁幾行獲得數據
 public List getStudentList(String page, String rows) {
 
    //當為缺省值的時刻停止賦值
 int currentpage = Integer.parseInt((page == null || page == "0") ? "1": page);//第幾頁
 int pagesize = Integer.parseInt((rows == null || rows == "0") ? "10": rows);//每頁若干行
 
 List list = this.sessionFactory.getCurrentSession().createQuery("from Student")
      .setFirstResult((currentpage - 1) * pagesize).setMaxResults(pagesize).list();

 return list;
 }

 // 統計一共有若干數據
 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、設置裝備擺設銜接數據庫的設置裝備擺設文件applicationContext_db.xml

<?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界說數據源 -->
 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
 destroy-method="close">
 <!-- 界說數據庫驅動 -->
 <property name="driverClass">
  <value>oracle.jdbc.driver.OracleDriver</value>
 </property>
 <!-- 界說數據庫URL -->
 <property name="jdbcUrl">
  <value>jdbc:oracle:thin:@localhost:1521:orcl</value>
 </property>
 <!-- 界說數據庫的用戶名 -->
 <property name="user">
  <value>lhq</value>
 </property>
 <!-- 界說數據庫的暗碼 -->
 <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>

 <!-- 設置裝備擺設事務攔阻器 -->
 <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開首的辦法能力履行增刪改操作 -->
  <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..*.*(..))" /> <!-- 對應完成類接口的包的地位 -->
 <aop:advisor advice-ref="txAdvice" pointcut-ref="interceptorPointCuts" />
 </aop:config>

</beans>

11、在掌握層編寫StudentAction.java類型

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;// 每頁顯示的記載數
 private String page;// 以後第幾頁
 private StudentService student_services;//String依附注入
 
 //查詢出一切先生信息
 public String getAllStudent() throws Exception {
 log.info("查詢出一切先生信息"); 
 
 List list = student_services.getStudentList(page, rows);
 this.toBeJson(list,student_services.getStudentTotal());

 return null;
 }
 
 //轉化為Json格局
  public void toBeJson(List list,int total) throws Exception{
  HttpServletResponse response = ServletActionContext.getResponse();
  HttpServletRequest request = ServletActionContext.getRequest();
  
  JSONObject jobj = new JSONObject();//new一個JSON
  jobj.accumulate("total",total );//total代表一共有若干數據
  jobj.accumulate("rows", list);//row是代表顯示的頁的數據

  response.setCharacterEncoding("utf-8");//指定為utf-8
  response.getWriter().write(jobj.toString());//轉化為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設置裝備擺設文件

<?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">
 
 <!-- 營業層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設置裝備擺設文件

<?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">
 <!-- 先生信息 -->
 <action name="getAllStudentAction" class="student_action" method="getAllStudent">
  <result type="json"> </result>
 </action>
 </package>
</struts>

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

<%@ 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>數字框</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國際化--中文 -->
<script type="text/javascript" src="<%=path%>/js/easyui/locale/easyui-lang-zh_CN.js" charset="utf-8"></script>
<!-- 引入easyUi默許的CSS格局--藍色 -->
<link rel="stylesheet" type="text/css" href="<%=path%>/js/easyui/themes/default/easyui.css" />
<!-- 引入easyUi小圖標 -->
<link rel="stylesheet" type="text/css" href="<%=path%>/js/easyui/themes/icon.css" />

<script type="text/javascript">
 $(function() {
 $('#mydatagrid').datagrid({
  title : 'datagrid實例',
  iconCls : 'icon-ok',
  width : 600,
  pageSize : 5,//默許選擇的分頁是每頁5行數據
  pageList : [ 5, 10, 15, 20 ],//可以選擇的分頁聚集
  nowrap : true,//設置為true,當數據長度超越列寬時將會主動截取
  striped : true,//設置為true將瓜代顯示行配景。
  collapsible : true,//顯示可折疊按鈕
  toolbar:"#tb",//在添加 增加、刪除、修正操作的按鈕要用到這個
  url:'getAllStudentAction.action',//url挪用Action辦法
  loadMsg : '數據裝載中......',
  singleSelect:true,//為true時只能選擇單行
  fitColumns:true,//許可表格主動縮放,以順應父容器
  //sortName : 'xh',//當數據表格初始化時以哪一列來排序
  //sortOrder : 'desc',//界說排序次序,可所以'asc'或許'desc'(正序或許倒序)。
  remoteSort : false,
   frozenColumns : [ [ {
  field : 'ck',
  checkbox : true
  } ] ], 
  pagination : true,//分頁
  rownumbers : true//行數
 }); 
 
 });
 
</script> 

</head>
<body>
 <h2>
 <b>easyui的DataGrid實例</b>
 </h2>

 <table id="mydatagrid">
  <thead>
  <tr>
  <th data-options="field:'studentid',width:100,align:'center'">先生學號</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、啟動法式,輸出http://localhost:8080/easyui/index.jsp停止測試。

以上就是本文的全體內容,願望對年夜家的進修有所贊助,也願望年夜家多多支撐。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved