java实现分页显示效果

网友投稿 269 2023-01-23


java实现分页显示效果

本文实例为大家分享了java实现页显示效果的具体代码,供大家参考,具体内容如下

效果图如下:

实现步骤:

1.创建实体User.class,参考代码如下:

public class User {

private String name;

private int age;

private String gender;

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public int getAge() {

return age;

}

public void setAge(int age) {

this.age = age;

}

public String getGender() {

return gender;

}

public void setGender(String gender) {

this.gender = gender;

}

public User(String name, int age, String gender) {

super();

this.name = name;

this.age = age;

this.gender = gender;

}

public User() {

}

}

2.创建分页模型PageBean.class,参考代码如下:

public class PageBean {

private int pageNum;

private int pageSize;

private int totalRecord;

private int totalPage;

private List list;

private int start;

private int end;

private int fromIndex;

private int toIndex;

public PageBean(int pageNum, int pageSize, int totalRecord) {

this.pageNum = pageNum;

this.pageSize = pageSize;

this.totalRecord = totalRecord;

fromIndex=(pageNum-1)*pageSize;

toIndex=pageNum*pageSize>totalRecord?totalRecord:pageNum*pageSize;

if (totalRecord % pageSize == 0) {

this.totalPage = totalRecord / pageSize;

} else {

this.totalPage = totalRecord / pageSize + 1;

}

start = 1;

end = 5;

if (totalPage <= 5) {

end = this.totalPage;

} else {

start = pageNum - 2;

end = pageNum + 2;

if (start < 1) {

start = 1;

end = 5;

}

if (end > this.totalPage) {

end = totalPage;

start = end - 5;

}

}

}

public int getPageNum() {

return pageNum;

}

public void setPageNum(int pageNum) {

this.pageNum = pageNum;

}

public int getPageSize() {

return pageSize;

}

public void setPageSize(int pageSize) {

this.pageSize = pageSize;

}

public int getTotalRecord() {

return totalRecordUkqLPQX;

}

public int getFromIndex() {

return fromIndex;

}

public void setFromIndex(int fromIndex) {

this.fromIndex = fromIndex;

}

public int getToIndex() {

return toIndex;

}

public void setToIndex(int toIndex) {

this.toIndex = toIndex;

}

public void setTotalRecord(int totalRecord) {

this.totalRecord = totalRecord;

}

public int getTotalPage() {

return totalPage;

}

public void setTotalPage(int totalPage) {

this.totalPage = totalPage;

}

public List getList() {

return list;

}

public void setList(List list) {

this.list = list.subList(fromIndex, toIndex);

}

public int getStart() {

return start;

}

public void setStart(int start) {

this.start = start;

}

public int getEnd() {

return end;

}

public void setEnd(int end) {

this.end = end;

}

}

3.创建jsp页面,参考代码如下:

/**

*index.jsp

*/

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

分页显示

/**

*main.jsp

*/

<%@ page language="java" contentType="text/html; charset=UTF-8"

pageEncoding="UTF-8"%>

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>


首页

end="${requestScope.pageBean.end}" var="i">

${i}

${i}

end="${requestScope.pageBean.end}" var="i">

${i}

${i}

href="${pageContext.request.contextPath}/main?pageNum=${requestScope.pageBean.pageNum+1}" rel="external nhttp://ofollow" rel="external nofollow" >下一页

href="${pageContext.request.contextPath}/main?pageNum=${requestScope.pageBean.pageNum+1}" rel="external nhttp://ofollow" rel="external nofollow" >下一页

test="${requestScope.pageBean.pageNum > 1 && requestScope.pageBean.pageNum < requestScope.pageBean.totalPage}">

href="${pageContext.request.contextPath}/main?pageNum=${requestScope.pageBean.pageNum-1}" rel="external nofollow" rel="external nofollow" >上一页

end="${requestScope.pageBean.end}" var="i">

${i}

test="${requestScope.pageBean.pageNum > 1 && requestScope.pageBean.pageNum < requestScope.pageBean.totalPage}">

href="${pageContext.request.contextPath}/main?pageNum=${requestScope.pageBean.pageNum-1}" rel="external nofollow" rel="external nofollow" >上一页

href="${pageContext.request.contextPath}/main?pageNum=${requestScope.pageBean.pageNum-1}" rel="external nofollow" rel="external nofollow" >上一页

end="${requestScope.pageBean.end}" var="i">

${i}

end="${requestScope.pageBean.end}" var="i">

${i}

${i}

href="${pageContext.request.contextPath}/main?pageNum=${requestScope.pageBean.pageNum+1}" rel="external nofollow" rel="external nofollow" >下一页

href="${pageContext.request.contextPath}/main?pageNum=${requestScope.pageBean.pageNum+1}" rel="external nofollow" rel="external nofollow" >下一页

test="${requestScope.pageBean.pageNum == requestScope.pageBean.totalPage}">

href="${pageContext.request.contextPath}/main?pageNum=${requestScope.pageBean.pageNum-1}" rel="external nofollow" rel="external nofollow" >上一页

end="${requestScope.pageBean.end}" var="i">

${i}

test="${requestScope.pageBean.pageNum == requestScope.pageBean.totalPage}">

href="${pageContext.request.contextPath}/main?pageNum=${requestScope.pageBean.pageNum-1}" rel="external nofollow" rel="external nofollow" >上一页

href="${pageContext.request.contextPath}/main?pageNum=${requestScope.pageBean.pageNum-1}" rel="external nofollow" rel="external nofollow" >上一页

end="${requestScope.pageBean.end}" var="i">

${i}

end="${requestScope.pageBean.end}" var="i">

${i}

${i}

href="${pageContext.request.contextPath}/main?pageNum=${requestScope.pageBean.totalPage}" rel="external nofollow" >尾页



href="${pageContext.request.contextPath}/main?pageNum=${requestScope.pageBean.totalPage}" rel="external nofollow" >尾页

跳转到

确定, 共[${requestScope.pageBean.totalPage }]页,[${requestScope.pageBean.totalRecord}]条记录

4.创建Servlets.class,参考代码如下:

@WebServlet("/main")

public class Servlets extends HttpServlet {

@Override

protected void service(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

String page = request.getParameter("pageNum");

int pageNum = Integer.parseInt((page == null ? "1" : page));

int pageSize = 5;

Service s = new Service();

PageBean pb = s.findAll(pageNum, pageSize);

request.setAttribute("pageBean", pb);

request.getRequestDispatcher("/main.jsp").forward(request, response);

}

}

5.创建Service.class,参考代码如下:

public class Service {

public PageBean findAll(int pageNum, int pageSize) {

UserDao userDao = new UserDao();

List users = userDao.findAll();

int totalRecord = users.size();

PageBean pb = new PageBean<>(pageNum, pageSize, totalRecord);

pb.setList(users);

return pb;

}

}

6.创建UserDao.class,参考代码如下:

public class UserDao {

List users=new ArrayList<>();

User user;

public List fhttp://indAll(){

for(int i=1;i<99;i++){

user=new User("name-"+i, (int)(100*Math.random()), (int)(10*Math.random())%2==1?"男":"女");

users.add(user);

}

return users;

}

}

需注意的问题:

1.需在WebContent下创建文件夹jquery,在他里面放入jquery.min.js这个文件。否则跳转功能异常。

2.index.jsp与main.jsp都在WebContent文件夹下。


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Spring Boot中如何使用断路器详解
下一篇:SpringBoot使用Spring
相关文章

 发表评论

暂时没有评论,来抢沙发吧~