Use detailed steps paging query in the javaweb

First, in the original database entity bean in the establishment of a paging entity bean

/ **
* Javabean paged data for displaying the object
* @author the Lenovo
*
* /
public class PageenationBean {

Private currPage Integer; // Pages

private Integer totalPage; // total number of pages

private List <UserBean> dataList; // data table

public Integer getCurrPage() {
return currPage;
}

public void setCurrPage(Integer currPage) {
this.currPage = currPage;
}

Public Integer given numerous PAGE () {
return total PAGE;
}

public void setTotalPage (Integer total page) {
this.totalPage = overall page;
}

public List<UserBean> getDataList() {
return dataList;
}

public void setDataList(List<UserBean> dataList) {
this.dataList = dataList;
}
}

Then writing system paging query in the business layer and persistence layer

Persistence:

public interface IUserDao {

/ **
* Get the total number of
* @return
* /
public int getTotalCount ();
/ *
* get the data start index
* @param the StartIndex
* @return
* /
public List <the UserBean> getUserListByStartIndex (int the StartIndex);

Business Layer:

public interface IUserService {


/ **
* Get the total number of pages
* @return
* /
public int getTotalPage ();

/ *
* get the data start index
* @param the StartIndex
* @return
* /
public List <the UserBean> getUserListByCurrPage (int currPage);

Then implement the interface method

Persistent class interface:

public class UserDaoImpl extends BaseDao implements IUserDao{

@Override
public int getTotalCount() {
this.setConnection();
int totalCount = 0;
try {
ps = con.prepareStatement("select count(*) from stuscore");
rs = ps.executeQuery();
if(rs.next()) {
totalCount =rs.getInt(1);
}
} catch (SQLException e) {
e.printStackTrace();
}
this.closeConnection();
return totalCount;
}

@Override
public List<UserBean> getUserListByStartIndex(int StartIndex) {
List<UserBean> list = new ArrayList<UserBean>();
this.setConnection();
try {
ps = con.prepareStatement("select * from stuscore limit ?,5");
ps.setInt(1, StartIndex);
rs=ps.executeQuery();
while(rs.next()) {
UserBean user=new UserBean();
user.setId(rs.getInt("id"));
user.setName(rs.getString("name"));
user.setGrade(rs.getString("grade"));
user.setSex(rs.getString("sex"));
user.setAge(rs.getInt("age"));
user.setScore(rs.getInt("score"));
list.add(user);
}

} catch (Exception e) {
e.printStackTrace();
}finally {
this.closeConnection();
}
return list;
}

Business class interface:

public class UserServiceImpl implements IUserService{

DAO new new UserDaoImpl IUserDao = Private ();
@Override
public int getTotalPage () {
// get the total number of pieces of data
int dao.getTotalCount The totalCount = ();
// calculate the total number of pages formula: totalPage = (totalCount + pageSize -1 ) / the pageSize
int TotalPage = (The totalCount + 5-1) /. 5;
return TotalPage;
}

@Override
public List <the UserBean> getUserListByCurrPage (int currPage) {
// this page by calculating the starting index
int startIndex = (. 1-currPage) *. 5;
// query the data start index
List <UserBean> userList = dao.getUserListByStartIndex (startIndex);
return userList;
}

Then write servlet method

The @WebServlet ( "/ ShowUserListServlets")
public class ShowUserListServlets the extends HttpServlet {

protected void doGet (Request the HttpServletRequest, HttpServletResponse the Response) throws ServletException, IOException {
// Get the current dynamic first few pages and do the calculation
String operation = request.getParameter ( "operation" );
String currPageStr = request.getParameter ( "currPage");
int = currPage. 1;
// query data retrieval service layer
IUserService new new UserServiceImpl-service = ();
// Get the total number of pages
int totalPage = service.getTotalPage ();
IF (currPageStr == null) {
currPage =. 1;
} the else IF ( "Home" .equals (Operation)) {
currPage =. 1;
} the else IF ( "Previous" .equals (Operation)) {
currPage = Integer.parseInt(currPageStr)-1;
if(currPage <= 0) {
currPage = 1;
}
}else if("下一页".equals(operation)) {
currPage = Integer.parseInt(currPageStr)+1;
if(currPage >= totalPage) {
currPage = totalPage;
}
}

List<UserBean> userList = service.getUserListByCurrPage(currPage);

//构建javabean对象-PagenationBean
PageenationBean pagenationBean = new PageenationBean();
pagenationBean.setCurrPage(currPage);
pagenationBean.setTotalPage(totalPage);
pagenationBean.setDataList(userList);

//放入作用域
request.setAttribute("page", pagenationBean);
request.getRequestDispatcher("userList.jsp").forward(request, response);
}


protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}

Finally, as the display will jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<!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="2" style="color:red,border-collapse: collapse;">
<tr><C: forEach items = "$ {} page.dataList "var =" u "></ TR><th> Age </ TH><th> Gender </ TH><th> Name </ TH>
<th> Student ID </ TH>





<tr>
<td>${u.id }</td>
<td>${u.name }</td>
<td>${u.sex}</td>
<td>${u.age}</td>
</tr>
</c:forEach>
</table>
<h3>
第${page.currPage }页/共${page.totalPage }页
</h3>
<br>
<form action="ShowUserListServlets" method="get">
<input type="submit" value="首页" name="operation">
<input type="submit" value="上一页" name="operation">
<input type="submit" value="下一页" name="operation">
<input type="submit" value="尾页" name="Operation "> Class: <input type = "text" name<HR><INPUT type =" hidden "name =" currPage "value =" $ {} page.currPage ">
<% - hidden form field effect, the current record is the first few pages -%>



性别:<input type="text" name="sex"><br>
成绩段:
<input type="text" name="min">至
<input type="text" name="max"><br>
<input type="submit" value="查询">
</form>
</body>
</html>

Guess you like

Origin www.cnblogs.com/wind-copy1234/p/11839378.html