1.创建web项目,导入jfinal,mysql,jsp相关依赖
2.项目结构
3.在res中添加jdbc.txt
修改为自己的数据库
jdbcUrl = jdbc:mysql://localhost/test?characterEncoding=utf8
user = root
password = admin
devMode = true
showSql = true
我的数据库如下,这里可以自己定义
4.创建主配置类
DemoConfig.java
public class DemoConfig extends JFinalConfig{
@Override
public void configConstant(Constants arg0) {
arg0.setViewType(ViewType.JSP);
loadPropertyFile("jdbc.txt");
}
@Override
public void configHandler(Handlers arg0) {
//项目根路径
arg0.add(new ContextPathHandler("baseUrl"));
}
@Override
public void configInterceptor(Interceptors arg0) {
}
@Override
public void configPlugin(Plugins arg0) {
C3p0Plugin c3p0=new C3p0Plugin(getProperty("jdbcUrl"),
getProperty("user"),
getProperty("password"));
arg0.add(c3p0);
ActiveRecordPlugin activeRecord=new ActiveRecordPlugin(c3p0);
activeRecord.addMapping("user",User.class);
arg0.add(activeRecord);
}
@Override
public void configRoute(Routes arg0) {
arg0.add("page",PageController.class,"view");
//对应第八步
}
public static void main(String[] args) {
JFinal.start("WebContent", 80, "/", 5);
}
}
5.修改web.xml,加入jfinal过滤器(servlet)
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>jfinal_demo</display-name>
<filter>
<filter-name>jfinal</filter-name>
<filter-class>com.jfinal.core.JFinalFilter</filter-class>
<init-param>
<param-name>configClass</param-name>
<param-value>com.cjq.config.DemoConfig</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>jfinal</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
6.在com.cjq.model包下创建User.java
public class User extends Model<User>{
public static final User dao=new User();
}
7.在com.cjq.service中创建UserService.java
public class UserService {
/**
*
* @param pageNum页号
* @param pageSize每页几条记录
* @return
*/
public static Page<User> pagInate(int pageNum,int pageSize){
return User.dao.paginate(pageNum, pageSize,"select *","from user");
}
}
8.在com.cjq.controller创建PageController.java
public class PageController extends Controller {
final int PAGE_SIZE=2;//默认每页2条记录
/**
* 初始页为第一页
*/
public void index(){
System.out.println("--------------");
List<User> list=UserService.pagInate(1,PAGE_SIZE).getList();
//renderJson(list);
setAttr("list", list);
setAttr("pageNum",UserService.pagInate(1,PAGE_SIZE).getPageNumber());
setAttr("totalPage",UserService.pagInate(1,PAGE_SIZE).getTotalPage());
render("page.jsp");
}
/**
* 上一页
*/
public void getBefore(){
int pageNum=getParaToInt();
List<User> list=UserService.pagInate(pageNum,PAGE_SIZE).getList();
setAttr("list", list);
//获取当前页号
setAttr("pageNum",UserService.pagInate(pageNum,PAGE_SIZE).getPageNumber());
//获取总页数
setAttr("totalPage",UserService.pagInate(pageNum,PAGE_SIZE).getTotalPage());
render("page.jsp");
}
/**
* 下一页
*/
public void getAfter(){
int pageNum=getParaToInt();
List<User> list=UserService.pagInate(pageNum,PAGE_SIZE).getList();
setAttr("list", list);
setAttr("pageNum",UserService.pagInate(pageNum,PAGE_SIZE).getPageNumber());
setAttr("totalPage",UserService.pagInate(pageNum,PAGE_SIZE).getTotalPage());
render("page.jsp");
}
}
9.在view目录下新建page.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!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>Insert title here</title>
</head>
<body>
<table border="1px">
<tr >
<th>id</th>
<th>姓名</th>
<th>年龄</th>
<th>密码</th>
</tr>
<c:forEach items="${list}" var="l">
<tr >
<td>${l.id}</td>
<td>${l.name}</td>
<td>${l.age}</td>
<td>${l.password}</td>
</tr>
</c:forEach>
</table>
<div class="page">
<a class="prev" href="#">上一页</a> <input type="text" id="pageNum"
value="${pageNum}" readonly="true" /> <a class="next" href="#">下一页</a>
共<input type="text" id="totalPage" value="${totalPage}"
readonly="true">页
</div>
<script type="text/javascript" src="${baseUrl}/js/jquery.js"></script>
<script type="text/javascript">
$(function() {
/**
点击上一页触发
**/
$(".prev").click(function() {
var pageNum = $("#pageNum").val();
pageNum = parseInt(pageNum);
if (pageNum == 1) {
alert("当前已经是第一页");
} else {
pageNum--;
window.location.href = "${baseUrl}/page/getBefore/"+ pageNum;
}
});
/**
点击下一页触发
**/
$(".next").click(function() {
var pageNum = $("#pageNum").val();
var totalPage = $("#totalPage").val();
pageNum = parseInt(pageNum);
if (pageNum == totalPage) {
alert("当前已经是最后一页");
} else {
pageNum++;
window.location.href = "${baseUrl}/page/getAfter/"+ pageNum;
}
});
})
</script>
</body>
</html>
10.运行DemoConfig.java类中main方法
11.页面有些难看,在这里不展示,可以自行调节样式