Skip to content

Commit a55c673

Browse files
committed
修改分页操作页面
1 parent 0ea0afe commit a55c673

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

src/main/java/com/linkinstars/springBootTemplate/controller/UserController.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.linkinstars.springBootTemplate.controller;
22

33
import com.github.pagehelper.PageHelper;
4+
import com.github.pagehelper.PageInfo;
45
import com.linkinstars.springBootTemplate.bean.UserEntity;
56
import com.linkinstars.springBootTemplate.service.IUserService;
67
import org.springframework.beans.factory.annotation.Autowired;
@@ -23,12 +24,15 @@ public class UserController {
2324
@RequestMapping("/test")
2425
public String test(HttpServletRequest request, @RequestParam(required = false) Integer pageNum){
2526
int pageSize = 3;
26-
if (pageNum == null) {
27+
if (pageNum == null || pageNum < 1) {
2728
pageNum = 1;
2829
}
2930
PageHelper.startPage(pageNum, pageSize);
3031
List<UserEntity> userList = userService.listUser();
32+
PageInfo pageInfo = new PageInfo(userList);
3133
request.setAttribute("userList", userList);
34+
request.setAttribute("pageNum", pageNum);
35+
request.setAttribute("pageSum", pageInfo.getPages());
3236
return "index";
3337
}
3438
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
function jumpPage(pageNum, pageSum) {
2+
pageNum = parseInt(pageNum);
3+
pageSum = parseInt(pageSum);
4+
if (pageNum < 1 || pageNum > pageSum) {
5+
return "当前选择的页码没有数据";
6+
}
7+
return true;
8+
}

src/main/resources/templates/index.html

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,26 @@
1313
<td th:text="${user.val}">2</td>
1414
</tr>
1515
</table>
16+
<input type="button" value="上一页" th:onclick="'gotoPage('+${pageNum-1}+', '+${pageSum}+')'" th:if="${pageNum ne 1}"/>
17+
<span th:text="${pageNum}"></span> / <span th:text="${pageSum}"></span>
18+
<input type="button" value="下一页" th:onclick="'gotoPage('+${pageNum+1}+', '+${pageSum}+')'" th:if="${pageNum ne pageSum}"/>
19+
<br/>
20+
21+
<form th:action="@{/test}" method="post">
22+
<input type="number" th:value="${pageNum}" name="pageNum"/>
23+
<input type="submit" value="跳转"/>
24+
</form>
25+
26+
<script type="application/javascript" src="../static/js/public.js" th:src="@{/js/public.js}"/>
27+
<script type="application/javascript" >
28+
function gotoPage(pageNum, pageSum) {
29+
var result = jumpPage(pageNum, pageSum);
30+
if (result == true) {
31+
window.location.href = "?pageNum=" + pageNum;
32+
} else {
33+
alert(result);
34+
}
35+
}
36+
</script>
1637
</body>
1738
</html>

0 commit comments

Comments
 (0)