[Lehr] how to rely only Jsp and Servlet implementation site paging function

analyse problem

First, we must define the data requirements for each section:
Here Insert Picture Description

Jsp

Input: total number of pages, page content

总页码:We know the total page click on the link to print out a corresponding jump page.

该页的内容:... This course know

Output: Jump Page

跳转页码:Servlet tell you where to jump page, the back-end in order to provide the corresponding data.

Servlet

Enter: Jump Page

跳转页码:By jump page, execution logic service, the service content corresponding to the extracted database.

Output: The total page number, page content

总页码:Let go front-end printing.

该页的内容:Let go front-end printing.

database

Enter: the number of articles per page, jump page

每页的文章数:Extract data

跳转页码:Extract data

Output: the content of the page, the total volume of articles

该页的内容:And then to the front end from the rear end

总文章量:Used to calculate the total number of pages of

Realize part of Servlet

Code


		int nowPage = 1;      //当前页码默认为第一页
		if(request.getParameter("nowPage")!=null)
		{
			nowPage = Integer.valueOf(request.getParameter("nowPage"));
		}      //如果前端传入了页码,那么就改为前端传入的
		int eachRow = 3;    //这里规定每页显示3篇文章
		int totalRow = 0;   //总的文章数
		int totalPage = 0;  //总的页数 
		
		//得到文章总数
		totalRow = dao.getTotalRow();
		//得到本页的文章内容
		List<Article> list = dao.getList(nowPage, eachRow);
		
		//文章放入session传给前端		
		request.getSession().setAttribute("List", list);
		
		//计算总页码
		if(totalRow%eachRow==0)   //如果刚好整除,就是那么多了
		{
			totalPage = totalRow/eachRow;
		}
		else      //如果每页还单独多了几页,那就多生成一页
		{
			totalPage = totalRow/eachRow+1;
		}
		
		//把页码数传给前端
		request.getSession().setAttribute("totalPage", totalPage);
		
		//跳转回到主页
		response.sendRedirect("View.jsp");
	}

Explanation

Here we need to have four parameters:
nowPage, eachRow, totalRows, TotalPage

Wherein nowPage obtained by the front end, the database incorporated eachRow filter (later speaks). TotalRow and the database is based on totalPage acquired, for calculating the total page number.

Realize part of the database

principle

From the above code found in, we need to get the contents of the article and the total number of the current page from the database.
So the next respectively explain a key part of these two statements. (JDBC the code is too big I will not put up, just write a sql statement XD)

Get the number of articles

SELECT COUNT(*) AS NUM FROM XXX

Gets the content of the current page

SELECT * FROM ARTICLE ORDER BY DATE DESC LIMIT ?, ?

I want to take this on to the next ...

In order to obtain the contents of the specified page, the need to use Limit
Limit followed by two parameters, respectively 从X行开始,读取X条数据

Obviously, 从X行开始that is the position we want to start the data, for example, we now look at the third page, (each of which has three data), then the first message is the third page of article 7, isPages -1MultipliedArticle per pagePlus one. (Drawn on a draft of this you will know XD).

读取X条数据In fact, the number of pieces per our provisions.

So, the question mark gatherers should supplement is so

			st.setInt(1,(nowPage-1)*eachRow);
			st.setInt(2,eachRow);

Jsp realize part of

Code

Of JSTL with EL expressions, the front end portion of the jump bar code can be written simply:

<div>	
	<ul>
		<c:forEach begin="1" end="${sessionScope.totalPage }" var = "i" >
				<li>
					<a href="ShowServlet?nowPage=${i}">${i}</a>
				</li>
			</c:forEach>
		</ul>	
	</div>	

Explanation

Briefly explain, first received from the back-end total number of pages (totalPage), then printed one by one, dynamically generated corresponding hyperlink. Where each hyperlink will transfer their own pages corresponding to the back-end.

that's it

Gone!

Achieve results

Here Insert Picture Description

Published 33 original articles · won praise 26 · views 2623

Guess you like

Origin blog.csdn.net/qq_43948583/article/details/89601965