1.在StudentController下添加增删改查
@Controller
public class StudentController {
@Autowired
StudentDAO studentDAO;
@RequestMapping("/listStudent")
public String listStudent(Model m, @RequestParam(value = "start", defaultValue = "0") int start,
@RequestParam(value = "size", defaultValue = "5") int size)throws Exception{
start = start<0?0:start;
Sort sort = Sort.by(Sort.Direction.DESC, "stuId");
Pageable pageable = PageRequest.of(start, size, sort);
Page<Student> page = studentDAO.findAll(pageable);
m.addAttribute("page", page);
return "listStudent";
}
@RequestMapping("/addStudent")
public String addStudent(Student s) throws Exception{
studentDAO.save(s);
return "redirect:listStudent";
}
@RequestMapping("/deleteStudent")
public String deleteStudent(Student s) throws Exception {
studentDAO.delete(s);
return "redirect:listStudent";
}
@RequestMapping("/updateStudent")
public String updateStudent(Student s) throws Exception {
studentDAO.save(s);
return "redirect:listStudent";
}
@RequestMapping("/editStudent")
public String editStudent(int stuId,Model m) throws Exception {
Student s= studentDAO.getOne(stuId);
m.addAttribute("s", s);
return "editStudent";
}
}
2.listStudent.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<div align="center">
</div>
<div style="500px;margin:20px auto;text-align: center">
<table align='center' border='1' cellspacing='0'>
<tr>
<td>stuId</td>
<td>name</td>
<td>sex</td>
<td>编辑</td>
<td>删除</td>
</tr>
<c:forEach items="${page.content}" var="s" varStatus="abc">
<tr>
<td>${s.stuId}</td>
<td>${s.name}</td>
<td>${s.sex}</td>
<td><a href="editStudent?stuId=${s.stuId}">编辑</a></td>
<td><a href="deleteStudent?stuId=${s.stuId}">删除</a></td>
</tr>
</c:forEach>
</table>
<br>
<div>
<a href="?start=0">[首 页]</a>
<a href="?start=${page.number-1}">[上一页]</a>
<a href="?start=${page.number+1}">[下一页]</a>
<a href="?start=${page.totalPages-1}">[末 页]</a>
</div>
<br>
<form action="addStudent" method="post">
name: <input name="name"> <br>
sex: <input name="sex"><br>
<button type="submit">提交</button>
</form>
</div>
3.editStudent.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" isELIgnored="false"%>
<div style="margin:0px auto; 500px">
<form action="/updateStudent" method="post">
name: <input name="name" value="${s.name}"> <br>
sex: <input name="sex" value="${s.sex}"><br>
<input name="stuId" type="hidden" value="${s.stuId}">
<button type="submit">提交</button>
</form>
</div>
问题:
1.Spring-Data-Jpa的Sort排序时遇到的问题 has private access in 'org.springframework.data.domain.Sort',
springboot2.2.1(含)以上的版本Sort已经不能再实例化了,构造方法已经是私有的了!我们可以改用Sort.by获得Sort对象
https://blog.csdn.net/qq_35953966/article/details/104061854
2.Pageable pageable = new PageRequest(start, size, sort) 不能使用了。改成如下
Pageable pageable = PageRequest.of(start, size, sort);
https://blog.csdn.net/weixin_44900565/article/details/105614972