・実現したいこと
リスト化したDB内に保存済のデータからidをランダムに取得し、そのidのデータのプロパティを呼び出したい
Eclipseを使用してます。
Javaで、DBに登録したメニューをランダムに表示させる機能をサーブレットで作成しています。
エラーメッセージは出ていないのですが、データ内容が表示されない状態です。
メニュー名の下にDB内のmenu_name(メニュー名)がランダムに表示されるようにしたいです。
以下ソースコードです。よろしくお願いします。
model
1package models; 2 3import java.sql.Date; 4import java.sql.Timestamp; 5 6import javax.persistence.Column; 7import javax.persistence.Entity; 8import javax.persistence.GeneratedValue; 9import javax.persistence.GenerationType; 10import javax.persistence.Id; 11import javax.persistence.JoinColumn; 12import javax.persistence.Lob; 13import javax.persistence.ManyToOne; 14import javax.persistence.NamedQueries; 15import javax.persistence.NamedQuery; 16import javax.persistence.Table; 17 18@Entity 19@Table(name = "menus") 20@NamedQueries({ 21 @NamedQuery(name = "getAllMenus", query = "SELECT m FROM Menu AS m ORDER BY m.id DESC"), 22 @NamedQuery(name = "getMenusCount", query = "SELECT COUNT(m) FROM Menu AS m") 23}) 24public class Menu { 25 @Id 26 @GeneratedValue(strategy = GenerationType.IDENTITY) 27 @Column(name = "id") 28 private Integer id; 29 30 @ManyToOne 31 @JoinColumn(name = "user_id", nullable = false) 32 private User user; 33 34 @Column(name = "menu_date", nullable = false) 35 private Date menu_date; 36 37 @Column(name = "menu_name", length = 255, nullable = false) 38 private String menu_name; 39 40 @Lob 41 @Column(name = "content", nullable = false) 42 private String content; 43 44 @Column(name = "created_at", nullable = false) 45 private Timestamp created_at; 46 47 @Column(name = "updated_at", nullable = false) 48 private Timestamp updated_at; 49 50 public Integer getId() { 51 return id; 52 } 53 54 public void setId(Integer id) { 55 this.id = id; 56 } 57 58 public User getUser() { 59 return user; 60 } 61 62 public void setUser(User user) { 63 this.user = user; 64 } 65 66 public Date getMenu_date() { 67 return menu_date; 68 } 69 70 public void setMenu_date(Date menu_date) { 71 this.menu_date = menu_date; 72 } 73 74 public String getMenu_name() { 75 return menu_name; 76 } 77 78 public void setMenu_name(String menu_name) { 79 this.menu_name = menu_name; 80 } 81 82 public String getContent() { 83 return content; 84 } 85 86 public void setContent(String content) { 87 this.content = content; 88 } 89 90 public Timestamp getCreated_at() { 91 return created_at; 92 } 93 94 public void setCreated_at(Timestamp created_at) { 95 this.created_at = created_at; 96 } 97 98 public Timestamp getUpdated_at() { 99 return updated_at; 100 } 101 102 public void setUpdated_at(Timestamp updated_at) { 103 this.updated_at = updated_at; 104 } 105 106}
Servlet
1package controllers.menus; 2 3import java.io.IOException; 4import java.util.List; 5import java.util.Random; 6 7import javax.persistence.EntityManager; 8import javax.servlet.RequestDispatcher; 9import javax.servlet.ServletException; 10import javax.servlet.annotation.WebServlet; 11import javax.servlet.http.HttpServlet; 12import javax.servlet.http.HttpServletRequest; 13import javax.servlet.http.HttpServletResponse; 14 15import models.Menu; 16import utils.DBUtil; 17 18/** 19 * Servlet implementation class MenusSearchServlet 20 */ 21@WebServlet("/menus/search") 22public class MenusSearchServlet extends HttpServlet { 23 private static final long serialVersionUID = 1L; 24 25 /** 26 * @see HttpServlet#HttpServlet() 27 */ 28 public MenusSearchServlet() { 29 super(); 30 // TODO Auto-generated constructor stub 31 } 32 33 /** 34 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) 35 */ 36 protected void doGet(HttpServletRequest request, HttpServletResponse response) 37 throws ServletException, IOException { 38 39 EntityManager em = DBUtil.createEntityManager(); 40//データを全件取得 41 long menus_count = (long) em.createNamedQuery("getMenusCount", Long.class) 42 .getSingleResult(); 43//ランダムの整数を件数から取得 44 Random rnd = new Random(); 45 int mrnd = rnd.nextInt((int) menus_count); 46 47//ランダム整数に対してのデータを取得 48 List<Menu> today_options = em.createNamedQuery("getAllMenus", Menu.class) 49 .setFirstResult(mrnd) 50 .setMaxResults(1) 51 .getResultList(); 52 53 em.close(); 54 55 request.setAttribute("today_options", today_options); 56 request.setAttribute("_token", request.getSession().getId()); 57 58 RequestDispatcher rd = request.getRequestDispatcher("/WEB-INF/views/menus/search.jsp"); 59 rd.forward(request, response); 60 61 } 62
JSP
1<%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 4<c:import url="../layout/app.jsp"> 5 <c:param name="content"> 6 7 <h2>今日のメニュー</h2> 8 <table id="today_menu_list"> 9 <tbody> 10 <tr> 11 <th class="menu_name">メニュー名</th> 12 <th class="menu_action">操作</th> 13 </tr> 14 15 <c:forEach var="today_option" items="{today_options}" varStatus="status"> 16 <tr class="row${status.count % 2}"> 17 18 <td class="menu_name"><c:out value="${menu.menu_name}" /></td> 19 <td class="menu_action"><a 20 href="<c:url value='/menus/show?id=${menu.id}'/>">詳細</a></td> 21 22 </tr> 23 </c:forEach> 24 </tbody> 25 </table> 26 27 </c:param> 28</c:import>
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。