zoukankan      html  css  js  c++  java
  • datagrid界面,链接数据库读取数据

    1、学生列表的 HTML部分

    <script type="text/javascript">
    $(function(){
        //创建dataGrid
        $("#dg").datagrid({
            url:'StudentServlet',    //数据来源
            //冻结列
            frozenColumns:[[
            {field:'id',checkbox:true},
            {field:'sno',title:'学号',100,sortable:true} ]],        \可排列
            
            //列的定义
            columns:[[
    
                {field:'sname',title:'姓名',100},
                {field:'sclass',title:'班级',100,align:'right'},
                {field:'ssex',title:'性别',100,align:'center',hidden:false},            
                {field:'sbirthday',title:'生日',100,align:'center'    }
            ]],
            fitColumns:true, //不能和冻结列同时设置
            striped:true,//斑马线效果
            idField:'sno',//主键列,
            rownumbers:true,//显示行号
            singleSelect:false,//是否单选
            pagination:true,//显示分页栏
            pageList:[5,10,20],//每页行数选择列表
            pageSize:5,//初始页面大小
            remoteSort:false,//是否服务器端排序
            toolbar:[
                     {iconCls:'icon-edit',text:'跳到第2页',
                         handler:function(){$('#dg').datagrid('gotoPage', 2)}
                         
                     },
                     {iconCls:'icon-edit',text:'跳到第3页',
                         handler:function(){$('#dg').datagrid('gotoPage', 3)}
                         
                     }
                     ]
        });
    })
    </script>
    数据表格<br>
    <table id="dg"></table>
    </body>

     2、StudentServlet部分(数据来源)

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            
            request.setCharacterEncoding("UTF-8");
            
            response.setCharacterEncoding("UTF-8");
            response.setContentType("text/html");      \汉化
            
            String spage = request.getParameter("page");    \获取page
            String srows = request.getParameter("rows");        \获取rows
            
        
            
            if(spage!=null&&srows!=null)
            {
            int page =Integer.parseInt(spage);
            int rows =Integer.parseInt(srows);
        
            String json = new StudentService().getPageJSON(page,rows);    \调用方法
            
            response.getWriter().println(json);
            }
            else
            {
                response.getWriter().println("{'total':0,'row':[]}" );
            }
                    
        }

    3、StudentService() 类

    public class StudentService {
        
        //查询分页数据
        //返回JSON
        public String getPageJSON(int page,int rows)
        {
            String rtn =    "{"total":0,"row":[]}" ;
            
            int total  =  new StudentDAO().getTotal();
            
            if(total > 0 )
            {
                List<Student> ls = new StudentDAO().getPageList(page, rows);
                
                String ls_json = JSONArray.toJSONString(ls);
                
                rtn = "{"total":"+total+","rows":"+ls_json+"}";          \规范 json 格式
            }
            return rtn;
        }
        
    }

    4、StudentDAO()类内的 getPageList(获取分页数据集合) 和 getTotal(获取数据条数) 的方法

    //获取分页数据集合
        public List<Student> getPageList(int page, int rows)
        {
            List<Student> rtn = new ArrayList<Student>();
            
            init();
            
            rtn = se.createQuery("from Student order by sno").
                    setMaxResults(rows).setFirstResult((page-1)*rows)
                    .list();
            
            destroy();        
            
            return rtn;
            
        }
        
        
        //获取数据条数
        public int getTotal()
        {
            int rtn = 0;
            
            init();
            
            List<Object> lo = se.createQuery("select count(1) from Student").list();
                
            if(lo!=null&&lo.size()>0)
            {
                rtn = Integer.parseInt(lo.get(0).toString());
            }
            
            
            destroy();
            
            return rtn;
        }
  • 相关阅读:
    Eclipse修改JSP文件的默认编码
    RPM常用命令总结
    软链接的妙用
    多线程练习
    Spring整合struts的配置文件存放问题
    使用struts框架后的404错误
    俄罗斯方块中的编程思想
    引用类型的强制类型转换
    数据库还原的问题
    常用sql语法初级
  • 原文地址:https://www.cnblogs.com/zs6666/p/6115279.html
Copyright © 2011-2022 走看看