Action
package action;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import java.util.List;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import entity.User;
import service.Userservice;
public class Useraction extends ActionSupport {
private Userservice userservice;
public void setUserservice(Userservice userservice) {
this.userservice = userservice;
}
public String userlist(){
int pageNos; //当前页数
int count=userservice.usercount();//总条数
int countPage;//总页数
//取决于每页的数目奇数还是偶数,现在为偶数
if(count%2==0) {
countPage=count/2;//每页两条数目的总页数
}else {
countPage=count/2+1;
}
/*
* 取决于每页的数目奇数还是偶数,现在为奇数
if(count%2==0) {
countPage=count/3+1;//每页三条数目的总页数
}else {
countPage=count/3;
}
*/
List<User> list=userservice.userlist();
HttpServletRequest request = ServletActionContext.getRequest();
if(request.getParameter("pageNos")==null || Integer.parseInt(request.getParameter("pageNos"))<1) {
pageNos=1;
}else {
pageNos=Integer.parseInt(request.getParameter("pageNos"));
}
ActionContext.getContext().getSession().put("pageNos", pageNos);
ActionContext.getContext().getSession().put("list", list);
ActionContext.getContext().getSession().put("countPage", countPage);
return "ok";
}
}
Service
package service;
import java.util.List;
import org.springframework.transaction.annotation.Transactional;
import dao.Userdao;
import entity.User;
@Transactional
public class Userservice {
private Userdao userdao;
public void setUserdao(Userdao userdao) {
this.userdao = userdao;
}
public List<User> userlist() {
return userdao.userlist();
}
public int usercount() {
return userdao.usercount();
}
}
Dao
package dao;
import java.util.List;
import entity.User;
public interface Userdao {
public List<User> userlist();
public int usercount();
}
DaoImpl
package dao;
import java.util.List;
import org.hibernate.Query;
import org.springframework.orm.hibernate5.HibernateTemplate;
import com.opensymphony.xwork2.ActionContext;
import entity.User;
public class Userdaoimpl implements Userdao {
private HibernateTemplate hibernateTemplate;
public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
this.hibernateTemplate = hibernateTemplate;
}
@Override
public List<User> userlist() {
List<User> list =(List<User>) hibernateTemplate.find("from User");
return list;
}
@Override
public int usercount() {
List<User> list =(List<User>) hibernateTemplate.find("from User");
int count = list.size();
return count;
}
}
Struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="demo1" extends="struts-default" namespace="/">
<action name="userAction" class="userAction" method="userlist">
<result name="ok">Userlist.jsp</result>
</action>
</package>
</struts>
前端
Userhome.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!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>
<a href="userAction.action">用户列表</a>
</body>
</html>
Userlist.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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<table border="1px" width="500px">
<tr>
<td>序号</td>
<td>用户名</td>
<td>密码</td>
</tr>
<c:forEach items="${list}" var="user" begin="${(pageNos-1)*2}" end="${pageNos*2-1}">
<tr>
<td>${user.uid}</td>
<td>${user.username}</td>
<td>${user.password}</td>
</tr>
</c:forEach>
<tr>
<td colspan="3" align="center">
<c:if test="${pageNos>1}">
<a href="userAction?pageNos=1">首页</a>
<a href="userAction?pageNos=${pageNos-1 }">上一页</a>
</c:if>
<c:if test="${pageNos<countPage}">
<a href="userAction?pageNos=${pageNos+1 }">下一页</a>
<a href="userAction?pageNos=${countPage }">末页</a>
</c:if>
<form action="userAction.action" method="post">
共${countPage}页
跳转到第几<input type="text" name="pageNos" >页
<input type="submit" value="go">
</form>
</td>
</tr>
</table>
</body>
</html>
部分代码参考:https://www.cnblogs.com/xwlych/p/6017833.html