春のブーツは初めてです。
でJSPのテーブルをページ分割することは可能ですか、という質問がJpaRepository<T, I>
あります。インターネットを2日間検索してきましたが、見つかりませんでした。クエリの結果は主にThymeleafに関するものでしたが、私はthymeleafを使用したくありません。私は中にページ付けする方法を知っているJSP使用しJdbctemplate
ますが、そのために、私は、クエリと手動でページ数を書くことをしました。私はすでにスプリングブートとJSPコードを記述しました。
EmployeeRepository:
public interface EmployeeRepository extends JpaRepository<Emp, Integer> {}
EmployeeController:
@Controller
public class EmployeeController {
@Autowired
private EmployeeRepository repo;
@GetMapping("/")
public String showPaginate(Model m, @RequestParam(defaultValue = "0") int page) {
m.addAttribute("data", repo.findAll(new PageRequest(page, 4)));
return "index";
}
}
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 -->
はい、可能です。しかし、最初にPagingAndSortingRepository<T, I>
リポジトリとコントローラーのインターフェースを作成する必要があります。あなたの場合、私にはわかりません。ページ、サイズ、要素、STCの属性を追加する必要があります。これによりPagingAndSortingRepository<T, I>
、JSPページが知るようになります。ページの属性など、${page}
ページ番号を表示するように使用できます。私はこのリポジトリを参照しています。
従業員リポジトリ:
public interface EmployeeRepository extends PagingAndSortingRepository<Employee, Integer> {}
にPagingAndSortingRepository<T, I>
は、findAll(page)
ページデータに従ってオブジェクトを渡すメソッドがあります。
従業員Dao:
@Service
public class EmpDao {
@Autowired
private EmpRepository repo;
public Page<Emp> getPage(Pageable pageable){
return repo.findAll(pageable);
}
ページに従ってオブジェクトが必要な場合、Page<T>
ページはオブジェクトのリストのサブリストであるため、インターフェイスを使用する必要があります。含まれるリスト全体での位置に関する情報を取得できます。
@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";
}
}
Pageable
ページネーション情報の抽象インタフェースであり、
getNumber()
-現在のスライスの番号を返します。常に非負です。getTotalPages()
-総ページ数を返します。getTotalElements()
-要素の合計量を返します。getSize()
-スライスのサイズを返します。getContent()
-ページのコンテンツをリストとして返します。あなたがすでにデータを格納していると仮定したJSPページ:
これをあなたのテーブル本体に書いてください。 <tbody>
<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>
あなたは行ってもいいです。
それが役に立てば幸い..
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加