前提・実現したいこと
現在、車両の1日毎の仕事を管理するWebアプリケーションを製作しています。
formで出発点、到着点や時刻などを入力し、DBに登録してからServletで取り出してJSPに表示させたいと思っています
(↑表現が間違ってるかもしれません)
予めtableに日付がn月1日~31日という風に表示されており、その列に各車両に応じたその日の仕事内容を順番に表示させたいです。
そのため、formで入力した日付と一致する日付の場所に情報を表示させようと思い、
<c:ifを使ってコードを書いてみたのですが、
期待した結果が得られなかったので、<c:if文の部分のコードを記載しますのでアドバイスを頂きたいです。
###追記内容
JSP、以前の内容から色々書き直しているのでコード修正しました。
試した内容
index.jsp
JSP
1<div class="Truck_table_wrapper"> 2 <table class="Truck_table"> 3 <thead> 4 <tr> 5 <th class="space1" rowspan="2" colspan="4"></th> 6<c:forEach begin="1" end="31" varStatus="stts"> 7 <th class="date" colspan="4">1月<c:out value="${stts.count}"></c:out>日</th></c:forEach> 8 </tr> 9 <tr> 10 <c:forEach begin="1" end="31"> 11 <th class="task_type">宵積卸地</th> 12 <th class="task_type">積地</th> 13 <th class="task_type">卸地</th> 14 <th class="task_type">宵積</th> 15 </c:forEach> 16 </tr> 17 </thead> 18 <c:if test="${value == null}" var="flg"> 19 <c:forEach var="truck" items="${trucks}" varStatus="status"> 20 <tbody> 21 <tr> 22 <td class="office_name" rowspan="4"><c:out value="${truck.office_name}"></c:out></td> 23 <td class ="truck_code" colspan="2" rowspan="4"><c:out value="${truck.truck_code}"></c:out></td> 24 <td class ="truck_type" rowspan="4"><c:out value="${truck.truck_type}"></c:out></td> 25 26 <td class ="time"><c:out value="${truck.mw_time}"></c:out></td> 27 <td class ="time"><c:out value="${truck.sa_time}"></c:out></td> 28 <td class ="time"><c:out value="${truck.wa_time}"></c:out></td> 29 <td class ="time"><c:out value="${truck.es_time}"></c:out></td> 30 </tr> 31 <c:if test="${flg}"> 32 <tr> 33 <td class ="area"><c:out value="${truck.m_wholesale_area}"></c:out></td> 34 <td class ="area"><c:out value="${truck.stack_area}"></c:out></td> 35 <td class ="area"><c:out value="${truck.wholesale_area}"></c:out></td> 36 <td class ="area"><c:out value="${truck.e_stack_area}"></c:out></td> 37 </tr> 38 </c:if> 39 <c:if test="${!flg}"> 40 <tr> 41 <td class ="area"><c:out value="${truck.m_wholesale_area}"></c:out></td> 42 <td class ="area"><c:out value="${truck.stack_area}"></c:out></td> 43 <td class ="area"><c:out value="${truck.wholesale_area}"></c:out></td> 44 <td class ="area"><c:out value="${truck.e_stack_area}"></c:out></td> 45 </tr> 46 </c:if> 47 <tr> 48 <td class ="time"></td> 49 <td class ="time"><c:out value="${truck.sa_time2}"></c:out></td> 50 <td class ="time"><c:out value="${truck.wa_time2}"></c:out></td> 51 <td class ="time"></td> 52 </tr> 53 <tr> 54 <td class ="area" id="3">ボタン</td> 55 <td class ="area" id="3"><c:out value="${truck.stack_area2}"></c:out></td> 56 <td class ="area" id="3"><c:out value="${truck.wholesale_area2}"></c:out></td> 57 <td class ="area" id="3">ボタン</td> 58 </tr> 59 </tbody> 60 </c:forEach> 61 </c:if> 62 </table> 63 </div>
DispatchIndexServlet.java
Java
1 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 2 EntityManager em = DBUtil.createEntityManager(); 3 4 List<Trucks> trucks = em.createNamedQuery("getAllTrucks",Trucks.class) 5 .getResultList(); 6 7 long trucks_count = (long)em.createNamedQuery("getTrucksCount", Long.class) 8 .getSingleResult(); 9 10 em.close(); 11 12 13 14 15 request.setAttribute("trucks", trucks); 16 request.setAttribute("trucks_count", trucks_count); 17 if(request.getSession().getAttribute("flush") != null) { 18 request.setAttribute("flush", request.getSession().getAttribute("flush")); 19 request.getSession().removeAttribute("flush"); 20 } 21 RequestDispatcher rd = request.getRequestDispatcher("/WEB-INF/views/dispatch/index.jsp"); 22 rd.forward(request, response); 23 } 24} 25
_form.jsp
JSP
1<p> 2<form method="GET" action = "DispatchCreateServlet.java"> 3 <label for="task_date">日付</label> 4 <br /> 5<input type="date" name="task_date" value="<fmt:formatDate value='${Trucks.task_date}' pattern='M月d日' />" /> 6(出発地、到着地、時刻などのformのコードなので中略) 7<input type="hidden" name="_token" value="${_token}" /> 8<button type="submit">登録</button> 9</form> 10
DispatchCreateServlet.java
Java
1 2 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 3 String _token = (String)request.getParameter("_token"); 4 if(_token != null && _token.equals(request.getSession().getId())) { 5 EntityManager em = DBUtil.createEntityManager(); 6 7 Trucks t = new Trucks(); 8 9 String sa_select = request.getParameter("sa_select"); 10 String sa_text = request.getParameter("sa_text"); 11 String stack_area = sa_select + sa_text; 12 String wa_select = request.getParameter("wa_select"); 13 String wa_text = request.getParameter("wa_text"); 14 String wholesale_area = wa_select + wa_text; 15 String office_name = request.getParameter("office_name"); 16 String wa_time = request.getParameter("wa_time"); 17 String sa_time = request.getParameter("sa_time"); 18 String wa_time2 = request.getParameter("wa_time2"); 19 String sa_time2 = request.getParameter("sa_time2"); 20 String es_select = request.getParameter("es_select"); 21 String es_text = request.getParameter("es_text"); 22 String e_stack_area = es_select +es_text; 23 String mw_time = request.getParameter("mw_time"); 24 String es_time = request.getParameter("es_time"); 25 String mw_select = request.getParameter("mw_select"); 26 String mw_text = request.getParameter("mw_text"); 27 String m_wholesale_area = mw_select + mw_text; 28 String sa_select2 = request.getParameter("sa_select2"); 29 String sa_text2 = request.getParameter("sa_text2"); 30 String stack_area2 = sa_select2 + sa_text2; 31 String wa_select2 = request.getParameter("wa_select2"); 32 String wa_text2 = request.getParameter("wa_text2"); 33 String wholesale_area2 = wa_select2 + wa_text2; 34 35 Date task_date = new Date(System.currentTimeMillis()); 36 String rd_str = request.getParameter("task_date"); 37 if(rd_str != null && !rd_str.equals("")){ 38 task_date = Date.valueOf(request.getParameter("task_date")); 39 40 } 41 t.setTask_date(task_date); 42 t.setStack_area(stack_area); 43 t.setWholesale_area(wholesale_area); 44 t.setOffice_name(office_name); 45 t.setWa_time(wa_time); 46 t.setSa_time(sa_time); 47 t.setWa_time2(wa_time2); 48 t.setSa_time2(sa_time2); 49 t.setM_wholesale_area(m_wholesale_area); 50 t.setE_stack_area(e_stack_area); 51 t.setMw_time(mw_time); 52 t.setEs_time(es_time); 53 t.setStack_area2(stack_area2); 54 t.setWholesale_area2(wholesale_area2); 55 56 List<String> errors = DispatchValidator.validate(t); 57 if(errors.size() > 0) { 58 em.close(); 59 60 request.setAttribute("errors", errors); 61 request.setAttribute("_token", request.getSession().getId()); 62 request.setAttribute("stack_area",stack_area); 63 request.setAttribute("wholesale_area",wholesale_area); 64 request.setAttribute("office_name",office_name); 65 request.setAttribute("wa_time",wa_time); 66 request.setAttribute("sa_time",sa_time); 67 request.setAttribute("wa_time2",wa_time2); 68 request.setAttribute("sa_time2",sa_time2); 69 request.setAttribute("e_stack_area",e_stack_area); 70 request.setAttribute("m_wholesale_area",m_wholesale_area); 71 request.setAttribute("mw_time",mw_time); 72 request.setAttribute("es_time",es_time); 73 request.setAttribute("stack_area2",stack_area2); 74 request.setAttribute("wholesale_area2",wholesale_area2); 75 76 RequestDispatcher rd = request.getRequestDispatcher("/WEB-INF/views/dispatch/new.jsp"); 77 rd.forward(request, response); 78 79 } else { 80 em.getTransaction().begin(); 81 em.persist(t); 82 em.getTransaction().commit(); 83 em.close(); 84 request.getSession().setAttribute("flush", "登録が完了しました。"); 85 86 response.sendRedirect(request.getContextPath() + "/dispatch/index"); 87 } 88 89 } 90 } 91} 92 93
回答1件
あなたの回答
tips
プレビュー