現在、java、mysqlを使用してWEBアプリケーションを作成しています。このポートフォリオでは、まず、ログイン画面が表示されます。ログインすると、イベントが複数表示されます。イベント1つ1つに”気になる”、”参加する”ボタン(以降リアクションボタンとする)を設けています。
そこで、ボタンを1回押すと、2回目は押せないような制御を作成したいのですが、なかなかできません。ログイン⇨リアクションボタンを押す⇨2回目は押せないという制御は可能ですが、ログイン直後の表示画面でリアクションボタンを制御することができない状況です。
package models; @Table(name = "events") @NamedQueries({ @NamedQuery( name = "getAllEvents", query = "select e from Event as e order by e.id desc" ), @NamedQuery( name = "getEventsCount", query = "select count(e) from Event as e" ) }) @Entity public class Event { @Id @Column(name = "id") @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer id; @Column(name = "restaurant", nullable = false) private String restaurant; @ManyToOne @JoinColumn(name = "user_id", nullable = false) private User user_id; @Column(name ="title", nullable = false) private String title; @Lob @Column(name = "content", nullable = false) private String content; @Column(name = "meeting_time", nullable = false) private String meeting_time; @Column(name = "place", nullable = false) private String place; @Column(name = "reaction_c") private Long reaction_c = (long)0; @Column(name = "reaction_j") private Long reaction_j = (long)0; @OneToMany(mappedBy = "event") private List<Reaction> reactionList = new ArrayList<Reaction>();
package models; @Table(name = "reactions") @NamedQueries({ @NamedQuery( name = "getReaction_events", query = "select r from Reaction as r where r.reaction_flag = :reaction_flag and r.user = :user order by r.event desc" ), @NamedQuery( name = "getReaction", query = "select r from Reaction as r where r.reaction_flag = :reaction_flag and r.user = :user and r.event = :event" ), @NamedQuery( name = "getReactions", query = "select r from Reaction as r order by r.event desc" ), @NamedQuery( name = "getReaction_sum", query = "select count(r) from Reaction as r where r.event = :event and r.reaction_flag = :reaction_flag" ), @NamedQuery( name = "getAllReactions", query = "select r from Reaction as r order by r.event desc" ), @NamedQuery( name = "reaction_control", query = "select r from Reaction as r where r.e_id = :e_id and r.reaction_flag = :reaction_flag and r.user = :user" ) }) @Entity public class Reaction { @Id @Column(name = "id") @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer id; @Column(name = "reaction_flag") private Integer reaction_flag; @ManyToOne @JoinColumn(name = "user_id") private User user; @ManyToOne @JoinColumn(name = "event_id") private Event event; @Column(name = "e_id") private Integer e_id;
package controllers.events; @WebServlet("/events/index") protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { EntityManager em = DBUtil.createEntityManager(); int page; try{ page = Integer.parseInt(request.getParameter("page")); }catch(NumberFormatException e ){ page = 1; } List<Event> events = em.createNamedQuery("getAllEvents", Event.class) .setFirstResult(6 * (page - 1)) .setMaxResults(6) .getResultList(); long events_count = em.createNamedQuery("getEventsCount", Long.class) .getSingleResult(); List<Event> popularEvents = em.createNamedQuery("getPopularEvents", Event.class) .setMaxResults(5) .getResultList(); Iterator<Event> iterator = events.iterator(); List<Reaction> re_crl = new ArrayList<>(); while(iterator.hasNext()){ Event e = (Event)iterator.next(); try{ Reaction reaction_control = em.createNamedQuery("reaction_control", Reaction.class) .setParameter("e_id", e.getId()) .setParameter("user", (User)request.getSession().getAttribute("login_user")) .setParameter("reaction_flag", 1) .getSingleResult(); if(reaction_control != null){ if(Arrays.asList(e.getReactionList()).contains(reaction_control)){ re_crl.add(reaction_control); request.setAttribute("re_cs", re_crl); } } }catch(Exception ex){} } em.close(); request.setAttribute("events", events); request.setAttribute("events_count", events_count); request.setAttribute("page", page); request.setAttribute("popularEvents", popularEvents); RequestDispatcher rd = request.getRequestDispatcher("/WEB-INF/views/events/index.jsp"); rd.forward(request, response); } }
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <c:import url="/WEB-INF/views/layout/app.jsp"> <c:param name="content"> <div id="all_content"> <div id="content_body"> <div id="contents-inner"> <c:forEach var="event" items="${events}"> <div id="event_content"> <div id="title"> <h2><c:out value="${event.title}"></c:out></h2> </div> <div id="restaurant"> <c:out value="${event.restaurant}"></c:out> </div> <div id="content"> <pre><c:out value="${event.content}" /></pre> </div> <div id="meeting_time"> 集合時間 : <c:out value="${event.meeting_time}" /> </div> <div id="place"> 都道府県 : <c:out value="${event.place}"></c:out> </div> <div id="reaction"> <div id="concern"> <c:forEach var="re_c" items="${re_cs}"> <c:choose> <c:when test="${re_c.event.id != event.id}"> <form method="POST" action="<c:url value='/reactions/create?reaction_flag=1&id=${event.id} '/>"> <button type="submit">気になる:<c:out value="${event.reaction_c}" /></button> </form> </c:when> <c:otherwise> <button type="submit">気になる:<c:out value="${event.reaction_c}" /></button> </c:otherwise> </c:choose> </c:forEach> </div> <div id="join"> <form method="POST" action="<c:url value='/reactions/create?reaction_flag=2&id=${event.id} '/>"> <button type="submit">参加する:<c:out value="${event.reaction_j}" /></button> </form> </div> </div> </div> </c:forEach> <div id="pagenation"> (全 ${events_count} 件) <c:forEach var="i" begin="1" end="${((events_count - 1) / 6) + 1}" step="1"> <c:choose> <c:when test="${i == page}"> <c:out value="${i}" /> </c:when> <c:otherwise> <a href="<c:url value='/events/index?page=${i}' />"><c:out value="${i}" /></a> </c:otherwise> </c:choose> </c:forEach> </div> <div id="reaction"> </div> </div>
あなたの回答
tips
プレビュー