コレは何? †
サンプルプログラム †
UserTable?.java (Entity Bean) †
package com.mycompany.sampleweb;
import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;
@Entity
@Table(name = "user_table")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "UserTable.findAll", query = "SELECT u FROM UserTable u ORDER BY u.id"),
@NamedQuery(name = "UserTable.countAll", query = "SELECT COUNT(u.id) FROM UserTable u"),
@NamedQuery(name = "UserTable.findById", query = "SELECT u FROM UserTable u WHERE u.id = :id"),
@NamedQuery(name = "UserTable.findByName", query = "SELECT u FROM UserTable u WHERE u.name = :name")})
public class UserTable implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "id")
private Long id;
@Size(max = 255)
@Column(name = "name")
private String name;
public UserTable() {
}
public UserTable(Long id) {
this.id = id;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public int hashCode() {
int hash = 0;
hash += (id != null ? id.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof UserTable)) {
return false;
}
UserTable other = (UserTable) object;
if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
return false;
}
return true;
}
@Override
public String toString() {
return "com.mycompany.sampleweb.UserTable[ id=" + id + " ]";
}
}
- NamedQuery? の findAll に OrderById? を加えて、再検索した場合にも検索結果の順番が保たれるようにするのがミソ
- あと countAll を追加した
Paging.java (Servlet) †
package com.mycompany.sampleweb;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.TypedQuery;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(name = "Paging", urlPatterns = {"/Paging"})
public class Paging extends HttpServlet {
@PersistenceContext(unitName = "appPU")
private EntityManager em;
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
int start = 0;
try {
start = Integer.parseInt(request.getParameter("start"));
} catch (NumberFormatException | NullPointerException ex) {
}
// 全件検索
TypedQuery findQuery = em.createNamedQuery("UserTable.findAll", UserTable.class);
findQuery.setFirstResult(start);
findQuery.setMaxResults(20);
List<UserTable> result = findQuery.getResultList();
// 件数
Query countQuery = em.createNamedQuery("UserTable.countAll");
long count = (Long) countQuery.getSingleResult();
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
/* TODO output your page here. You may use following sample code. */
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet Paging</title>");
out.println("</head>");
out.println("<body>");
out.println("<table border=\"1\"><tbody>");
for (UserTable user : result) {
out.println(String.format("<tr><td>%s</td><td>%s</td></tr>", user.getId(), user.getName()));
}
out.println("</tbody></table>");
if (start - 20 >= 0) {
out.print(String.format("<a href=\"Paging?start=%s\"><</a>", start - 20));
}
out.print(String.format("[%s/%s]", start, count));
if (start + 20 < count) {
out.print(String.format("<a href=\"Paging?start=%s\">></a>", start + 20));
}
out.println("</body>");
out.println("</html>");
}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
Java#Glassfish