Java 是否可以使用JpaRepository在jsp中分页<;T、 I>;
我是新来的春靴 我有一个问题,是否可以使用Java 是否可以使用JpaRepository在jsp中分页<;T、 I>;,java,spring-boot,jsp,pagination,Java,Spring Boot,Jsp,Pagination,我是新来的春靴 我有一个问题,是否可以使用JpaRepository在JSP中对表进行分页,我在internet上搜索了两天,但没有找到一个。查询结果主要是针对Thymeleaf,但我不想使用。我知道如何使用Jdbctemplate在JSP中分页,但为此,我必须手动编写查询和页数。我已经编写了一个springboot&JSP代码 雇员住所: public interface EmployeeRepository extends JpaRepository<Emp, Integer>
JpaRepository
在JSP中对表进行分页,我在internet上搜索了两天,但没有找到一个。查询结果主要是针对Thymeleaf,但我不想使用。我知道如何使用Jdbctemplate
在JSP中分页,但为此,我必须手动编写查询和页数。我已经编写了一个springboot&JSP代码
雇员住所:
public interface EmployeeRepository extends JpaRepository<Emp, Integer> {}
public interface EmployeeRepository extends PagingAndSortingRepository<Employee, Integer> {}
index.jsp
<table border="2" width="70%" cellpadding="3"
class="table">
<thead class="thead-dark">
<tr align="center">
<th scope="col">Id</th>
<th scope="col">Name</th>
<th scope="col">Designation</th>
<th scope="col">Edit</th>
<th scope="col">Delete</th>
</tr>
</thead>
<c:forEach var="emp" items="${list}">
<tbody>
<tr align="center">
<td>${emp.id}</td>
<td>${emp.name}</td>
<td>${emp.designation}</td>
<td><a href="editemp/${emp.id}" class="btn btn-outline-info">Edit</a></td>
<td><a href="deleteemp/${emp.id}" class="btn btn-outline-danger">Delete</a></td>
</tr>
</tbody>
</c:forEach>
</table>
<hr>
<!-- Pagination code will come here -->
身份证件
名称
任命
编辑
删除
${emp.id}
${emp.name}
${emp.designation}
尝试以下操作:使用分页和排序存储库扩展EmployeeRepository。然后更改findall()方法类型PageRequest findall(PageRequest PageRequest)
公共接口EmployeeRepository扩展了分页和排序存储库{
PageRequest findAll(PageRequest PageRequest)
}
是的,这是可能的。但是首先你必须创建一个PagingAndSortingRepository
repository和一个控制器的接口,在你的例子中,我没有看到一个,你必须添加一个page、size、elements和stc的属性,这个属性由PagingAndSortingRepository
返回,这样你的jsp页面就可以知道页面的属性,etc,您可以像${page}
一样使用它来显示页码。我指的是这个
员工存储库:
public interface EmployeeRepository extends JpaRepository<Emp, Integer> {}
public interface EmployeeRepository extends PagingAndSortingRepository<Employee, Integer> {}
如果您希望根据页面创建对象,则必须使用page
接口,因为页面是对象列表的子列表。它允许获取有关其在包含整个列表中的位置的信息
@Controller
public class EmpController {
@Autowired
private EmpDao dao;
@RequestMapping("/viewemp")
public String viewemp(Model m, Pageable pageable){
Page<Emp> pages=dao.getPage(pageable);
m.addAttribute("number", pages.getNumber());
m.addAttribute("totalPages", pages.getTotalPages());
m.addAttribute("totalElements", pages.getTotalElements());
m.addAttribute("size", pages.getSize());
m.addAttribute("data",pages.getContent());
return "viewemp";
}
}
你可以走了
希望对您有所帮助。通过此示例可以确定一些方法
public class StudentRepository {
@PersistenceContext
private EntityManager entityManager;
public List<StudentEntity> listAll() {
Query query = entityManager.createQuery("from StudentEntity");
return query.getResultList();
}
}
public class StudentRepository{
@持久上下文
私人实体管理者实体管理者;
公共列表listAll(){
Query Query=entityManager.createQuery(“来自StudentEntity”);
返回query.getResultList();
}
}
那么,我要在jsp中为paginateJpaRepository编写什么呢?它扩展了PagingAndSortingRepository
<tbody id="myTable">
<c:choose>
<c:when test="${data.size() > 0 }">
<c:forEach var="emp" items="${data}">
<tr align="center">
<td>${emp.id}</td>
<td>${emp.name}</td>
<td>${emp.designation}</td>
<td><a href="editemp/${emp.id}" class="btn btn-outline-info">Edit</a></td>
<td><a href="deleteemp/${emp.id}" class="btn btn-outline-danger">Delete</a></td>
</tr>
</c:forEach>
</c:when>
<c:otherwise>
<tr align="center">
<td colspan="5">No Users available</td>
</tr>
</c:otherwise>
</c:choose>
<c:if test="${data.size() > 0 }">
<div class="panel-footer">
Showing ${number+1} of ${size+1} of ${totalElements}
<ul class="pagination">
<c:forEach begin="0" end="${totalPages-1}" var="page">
<li class="page-item">
<a href="viewemp?page=${page}&size=${size}" class="page-link">${page+1}</a>
</li>
</c:forEach>
</ul>
</div>
</c:if>
</tbody>
public class StudentRepository {
@PersistenceContext
private EntityManager entityManager;
public List<StudentEntity> listAll() {
Query query = entityManager.createQuery("from StudentEntity");
return query.getResultList();
}
}