栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SSh结合Easyui实现Datagrid的分页显示

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SSh结合Easyui实现Datagrid的分页显示

      近日学习Easyui,发现非常好用,界面很美观。将学习的心得在此写下,这篇博客写SSh结合Easyui实现Datagrid的分页显示,其他的例如添加、修改、删除、批量删除等功能将在后面一一写来。

     首先看一下要实现的效果:当每页显示5行数据:

 当每页显示10行数据,效果如下:

具体步骤:

1、下载Easyui,并搭建环境。

2、搭建SSH工程,整个工程的目录结构如图所示:

3、在Oracle数据库中创建表Student。并且输入下面6行数据,因为添加操作还没有实现,所以先在数据库表中添加数据。默认设定的值是每行5个数据,所以请至少输入6行数据,便于分页的测试。

4、web.xml的配置




 
 
 struts2
 org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
 
 
 struts2
 /*
 

 
 
 org.springframework.web.context.ContextLoaderListener
 

 
 
 contextConfigLocation
 classpath:applicationContext.xml
 



5、applicationContext.xml的配置









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





  
    
      
      
    
    
      
    
    
      
    
    
      
    
  

 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





 
 
 
 
  oracle.jdbc.driver.OracleDriver
 
 
 
  jdbc:oracle:thin:@localhost:1521:orcl
 
 
 
  lhq
 
 
 
  lhq
 
 
  1
 
 
  40
 
 
  1800
 
 
  2
 
 
  0
 
 
  2
 
 
  1800
 
 
  30
 
 
  true
 
 
  false
 

 

 
 
 
 
  
 
 
 
  
  
   org.hibernate.dialect.Oracle10gDialect
  
  
 

 
 
  
  com/model/Student.hbm.xml
  
 
 

 
 
 
 

 
 
  
  
  
  
 
 

 
  
 
 



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配置文件



 
 
 
  
  
 
 

 
 
 
  
 
 
 


13、编写struts.xml配置文件




 
 
 
   
 
 

14、编写JSP----index.jsp

<%@ page language="java" pageEncoding="utf-8" isELIgnored="false"%>
<%
 String path = request.getContextPath();
%>
<%@ taglib prefix="s" uri="/struts-tags"%>




数字框











 



 
 easyui的DataGrid实例
 

 
学生学号 姓名 性别 年龄

 15、启动程序,输入http://localhost:8080/easyui/index.jsp进行测试。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/149876.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号