美文网首页
jfinal利用jsp实现分页功能

jfinal利用jsp实现分页功能

作者: ccq_inori | 来源:发表于2017-10-14 20:15 被阅读0次

    在我的上一篇我已经实现了如何利用jfinal来实现增删改查,接下来是实现分页功能。在jfinal社区有demo,里面有简单的分页功能,它是Free Marke来实现的。。。因为我没怎么接触过Free Marke。所以我的分页是用jsp来实现的(显示的数据还是上一篇文章的数据)。可能还不是特别的完善,接下来会慢慢的完善.

    1.MainConfig.java
    package com.common;
    
    import com.Exception.ExceptionController;
    import com.jfinal.config.Constants;
    import com.jfinal.config.Handlers;
    import com.jfinal.config.Interceptors;
    import com.jfinal.config.JFinalConfig;
    import com.jfinal.config.Plugins;
    import com.jfinal.config.Routes;
    import com.jfinal.core.JFinal;
    import com.jfinal.ext.handler.ContextPathHandler;
    import com.jfinal.kit.PropKit;
    import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
    import com.jfinal.plugin.c3p0.C3p0Plugin;
    import com.jfinal.render.ViewType;
    import com.jfinal.template.Engine;
    import com.login.loginControler;
    import com.students.StudentController;
    import com.students.students;
    import com.teachers.teachers;
    
    public class MainConfig extends JFinalConfig {
    
        public static void main(String[] args) 
        {
            JFinal.start("WebRoot",80,"/",5);
            
        }
    
        public void configConstant(Constants me) 
        {
            me.setViewType(ViewType.JSP);
            
            PropKit.use("config.properties");
        }
    
        /**
         * 设置路径 
         */
        public void configRoute(Routes me) 
        {
            me.add("/students",StudentController.class);
    
        }
    
    
        public void configEngine(Engine me) 
        {
    
        }
    
        public void configPlugin(Plugins me) 
        {
            C3p0Plugin c3p0Plugin=new C3p0Plugin(PropKit.get("jdbcUrl"),PropKit.get("username"),PropKit.get("password"));
            ActiveRecordPlugin arp0=new ActiveRecordPlugin(c3p0Plugin);
            arp0.setShowSql(true);
            arp0.addMapping("students", students.class);
            me.add(c3p0Plugin);
            me.add(arp0);
        }
    
        public void configInterceptor(Interceptors me) 
        {
            
        }
    
        public void configHandler(Handlers me) 
        {
            
        }
    
    }
    
    2.students.java
    package com.students;
    
    import com.jfinal.plugin.activerecord.Model;
    
    public class students extends Model<students> 
    {
        public static final students students=new students();
    }
    
    3.Serivce.java
    package com.common;
    
    import java.util.List;
    
    
    import com.jfinal.plugin.activerecord.Page;
    import com.students.students;
    
    public class Serivce 
    {
        /**
         * 显示全部学生信息
         */
        public List<students> StudentList(Integer number)
        {
            String sql="select * from students limit 0,";
            sql+=number;
            List<students> dao=students.students.find(sql);
            return dao;
        }
        /**
         * 实现增加功能
         */
        public void submitMessage(students student)
        {
            System.out.println(student);
            student.save();     
        }
        /**
         * 实现删除功能
         */
        public void deleteStudent(Integer id)
        {
            students.students.deleteById(id);
        }
        public students editMessage(Integer id)
        {
            students stu=students.students.findById(id);
            return stu;
        }
        /**
         * 实现更新数据
         */
        public void updateMessage(students stu)
        {
            stu.update();
        }
        public List<students> sqlstatement(Integer p,Integer p2)
        {
            String sql="select * from students limit ";
            sql+=p;
            sql+=","; 
            sql+=p2;
            List<students> dao=students.students.find(sql);
            return dao;
        }
        
        public List<students> pageNumber()
        {
            List<students> dao=students.students.find("select * from students");
            return dao;
        }
    
    }
    
    4.StudentController.java(实现要点)
    package com.students;
    
    import java.util.List;
    
    import com.common.Serivce;
    import com.jfinal.core.Controller;
    
    public class StudentController extends Controller 
    {
        Serivce serivce=new Serivce();
        //下一页的参数
        Integer pageNumber=0;
        //上一页的参数
        Integer looktg=0;
        //当前页数
        Integer pagination=1;
        //每一页显示的个数
        Integer number=3;
        public void index()
        {
            pageNumber=0;
            setAttr("pageNumber", pageNumber);
            setAttr("student", serivce.StudentList(number));
            if(serivce.pageNumber().size()%number!=0)
            {
                setAttr("page", (serivce.pageNumber().size()/number)+1);
            }
            else
            {
                setAttr("page", serivce.pageNumber().size()/number);
            }
            setAttr("pagination", pagination);
            render("list.jsp");     
    
        }
        public void submit()
        {       
            students student=getModel(students.class,"student");
            serivce.submitMessage(student);
            if(serivce.pageNumber().size()%2==1)
            {
                setAttr("page", (serivce.pageNumber().size()/number)+1);
            }
            else
            {
                setAttr("page", serivce.pageNumber().size()/number);
            }
            redirect("/students");
        }
        public void delete()
        {
            serivce.deleteStudent(getParaToInt(0));
            index();
        }
        public void edit()
        {
            form();
        }
        public void form()
        {
            students student=serivce.editMessage(getParaToInt(0));
            setAttr("student", student);
            render("form.jsp");
        }
        public void update()
        {
            students stu=getModel(students.class,"student");
            serivce.updateMessage(stu);
            redirect("/students");
        }
        /**
         * 下一页
         */
        public void nextpage()
        {   
            pageNumber=getParaToInt(0)+number;
            System.out.println(pageNumber);
            if(pageNumber>serivce.pageNumber().size()-1)
            {
                index();
            }
            else
            {
                pagination=getParaToInt(1)+1;
                List<students> dao=serivce.sqlstatement(pageNumber,number);
                if(serivce.pageNumber().size()%number!=0)
                {
                    setAttr("page", (serivce.pageNumber().size()/number)+1);
                }
                else
                {
                    setAttr("page", serivce.pageNumber().size()/number);
                }
                setAttr("pageNumber", pageNumber);
                setAttr("student", dao);
                setAttr("pagination", pagination);
                render("list.jsp");
            }
        }
        /**
         * 上一页
         */
        public void Previouspage()
        {
            
            looktg=getParaToInt(0)-number;
            if(looktg<0)
            {
                index();
            }
            else
            {
                pagination=getParaToInt(1)-1;
                List<students> dao=serivce.sqlstatement(looktg, number);
                if(serivce.pageNumber().size()%number!=0)
                {
                    setAttr("page", (serivce.pageNumber().size()/number)+1);
                }
                else
                {
                    setAttr("page", serivce.pageNumber().size()/number);
                }
                setAttr("pageNumber", looktg);
                setAttr("student", dao);    
                setAttr("pagination", pagination);
                render("list.jsp");
            }
        }
        /**
         * 尾页
         */
        public void trailerpage()
        {
            Integer number1=0;
            if(serivce.pageNumber().size()%number==0)
            {
                number1=serivce.pageNumber().size()-number; 
            }
            else
            {
                number1=serivce.pageNumber().size()-(serivce.pageNumber().size()%number);       
            }   
            if(serivce.pageNumber().size()%number!=0)
            {
                setAttr("page", (serivce.pageNumber().size()/number)+1);
                pagination=serivce.pageNumber().size()/number+1;
            }
            else
            {
                setAttr("page", serivce.pageNumber().size()/number);
                pagination=serivce.pageNumber().size()/number;
            }
            System.out.println(number1);
            List<students> dao=serivce.sqlstatement(number1, number);
            setAttr("pageNumber", serivce.pageNumber().size()-number);
            setAttr("student", dao);
            setAttr("pagination", pagination);
            render("list.jsp");
        }
        /**
         * 跳转到指定页面
         */
        public void jumpPage()
        {       
            //获取前端值
            String pagination=getPara("number");
            //转换格式
            Integer number1=Integer.parseInt(pagination);
            //从数据库中得到指定数据
            List<students> dao=nextpage(number1);
            pageNumber=number1*number-number;
            if(serivce.pageNumber().size()%number!=0)
            {
                setAttr("page", (serivce.pageNumber().size()/number)+1);
            }
            else
            {
                setAttr("page", serivce.pageNumber().size()/number);
            }
            setAttr("pagination", pagination);
            setAttr("pageNumber", pageNumber);
            setAttr("student", dao);
            render("list.jsp");
        }
        public List<students> nextpage(Integer number1)
        {   
            Integer pageNumber=number1*number-number;
            List<students> dao=serivce.sqlstatement(pageNumber,number);
            return dao;
        }
    }
    

    接下来是前端的显示问题

    5.list.jsp
    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title>Insert title here</title>
    </head>
    
    <body>
        <a href="/students/form/">增加学生</a>
        <table border="2" width="80%">      
            <caption><h1>学生信息</h1></caption>
            <thead>
            <th>姓名</th>
            <th>年龄</th>
            <th>性别</th>
            <th>备注</th>
            <th rowspan="2">操作</th>
            </thead>
            <tbody>
            <c:forEach items="${student}" var="student">
            <tr>
            <th>${student.name}</th>
            <th>${student.age}</th>
            <th>${student.sex}</th>
            <th>${student.remark}</th>
            <th><a href="/students/edit/${student.id}">修改 </a><a href="/students/delete/${student.id}"> 删除</a></th>
            </tr>
            </c:forEach>
            </tbody>
                
            <tr>        
            <th colspan="5">总页数  ${pagination}/${page}    
            <a href="/students">首页</a>    
            <a href="/students/Previouspage/${pageNumber}-${pagination}">上一页</a>    
            <a href="/students/nextpage/${pageNumber}-${pagination}">下一页</a>    
            <a href="/students/trailerpage/">尾页</a>   
            <form action="/students/jumpPage/">
            <label>跳转到</label>
            <input type="text" name="number">
            <button type="submit">确定</button>
            </form>
            </th>
            </tr>
        </table>
        
        
          
    </body>
    </html>
    
    6.form.jsp
    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>
    <!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=ISO-8859-1">
    <title>Insert title here</title>
    </head>
    <body>
        <form action="${student==null?'/students/submit':'/students/update'}" method="post">
        <input type="hidden" name="student.id" value="${student.id}">
        <label>姓名</label>
        <input type="text" name="student.name" value="${student.name}">
        <label>年龄</label>
        <input type="text" name="student.age" value="${student.age}">
        <div >
        <label>性别</label>
        <div>
        <input type="radio"  name="student.sex" <c:if test="${student.sex=='男'}">checked="checked"</c:if>value="男"> 男
        </div>
        <div>
        <input type="radio"   name="student.sex" <c:if test="${student.sex=='女'}">checked="checked"</c:if>value="女"> 女
        </div>
        </div>
        <label>备注</label>
        <textarea rows="10" cols="20" name="student.remark">${student.remark}</textarea>
        <button type="submit">提交</button>
        </form>
    </body>
    </html>
    

    完美实现

    搜狗截图17年10月12日2008_1.png
    这是我第一次实现分页功能,实现的时候挺兴奋的,因为在网上找了许多demo,好像都没有用jfinal和jsp来实现分页的。想着没有的话,就自己做一个出来,在网上看了许多分页的原理之后,我觉得我的应该是可以做出来的,就尝试着做了一个试试看,没想到居然做出来的。一开始是写死代码,让它能够显示固定的分页,然后就慢慢的调整改善,最后做出来我这个的分页功能,只用修改显示个数就可以了,以上.

    相关文章

      网友评论

          本文标题:jfinal利用jsp实现分页功能

          本文链接:https://www.haomeiwen.com/subject/jzboyxtx.html