jfinal利用jsp实现分页功能

在我的上一篇我已经实现了如何利用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 
{
    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 StudentList(Integer number)
    {
        String sql="select * from students limit 0,";
        sql+=number;
        List 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 sqlstatement(Integer p,Integer p2)
    {
        String sql="select * from students limit ";
        sql+=p;
        sql+=","; 
        sql+=p2;
        List dao=students.students.find(sql);
        return dao;
    }
    
    public List pageNumber()
    {
        List 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 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 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 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 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 nextpage(Integer number1)
    {   
        Integer pageNumber=number1*number-number;
        List 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"%> 




Insert title here



    增加学生
    
        

学生信息

姓名 年龄 性别 备注 操作
${student.name} ${student.age} ${student.sex} ${student.remark} 修改 删除
总页数 ${pagination}/${page} 首页 上一页 下一页 尾页
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+"/";
%>



  

Insert title here


    
checked="checked"value="男"> 男
checked="checked"value="女"> 女

完美实现

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

你可能感兴趣的:(jfinal利用jsp实现分页功能)