現在プログラミングの課題として日報管理システムを作成しているのですが、下記のようなViewのフォームをJSPにて作成し、始業時間start_timeと終業時間end_timeをHTML上で<input="time"/>
の形式で入力するようにし、その値をサーブレットでデータベースに保存するということをやりたいのですが、IllegalArgumentException
などのエラーが出てしまい、どのようにすればよいか困っております。
JSP
1<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> 2<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 3<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> 4<c:import url="/WEB-INF/views/layout/app.jsp"> 5 <c:param name="content"> 6 <c:choose> 7 <c:when test="${report != null}"> 8 <h2>日報 新規登録ページ</h2> 9 <form method="POST" action="<c:url value='/reports/create' />"> 10 <c:if test="${errors != null}"> 11 <div id="flush_error"> 12 入力内容にエラーがあります。<br /> 13 <c:forEach var="error" items="${errors}"> 14 ・<c:out value="${error}" /><br /> 15 </c:forEach> 16 17 </div> 18 </c:if> 19 <label for="report_date">日付</label><br /> 20 <input type="date" name="report_date" value="<fmt:formatDate value='${report.report_date}' pattern='yyyy-MM-dd' />" /> 21 <br /><br /> 22 23 <label for="name">氏名</label><br /> 24 <c:out value="${sessionScope.login_employee.name}" /> 25 <br /><br /> 26 27 <label for="title">タイトル</label><br /> 28 <input type="text" name="title" value="${report.title}" /> 29 <br /><br /> 30 31 <label for="title">始業時間</label><br /> 32 <input type="time" name="start_time" value="${report.start_time}" step="60" /> 33 <br /><br /> 34 35 <label for="title">終業時間</label><br /> 36 <input type="time" name="end_time" value="${report.end_time}" step="60" /> 37 <br /><br /> 38 39 <label for="content">内容</label><br /> 40 <textarea name="content" rows="10" cols="50">${report.content}</textarea> 41 <br /><br /> 42 43 <input type="hidden" name="_token" value="${_token}" /> 44 <button type="submit">投稿</button> 45 </form> 46 </c:when> 47 <c:otherwise> 48 <h2>お探しのデータは見つかりませんでした。</h2> 49 </c:otherwise> 50 </c:choose> 51 52 <p> 53 <a href="<c:url value='/reports/index' />">一覧に戻る</a> 54 </p> 55 </c:param> 56</c:import>
Model層は下記のように定義いたしました。
Java
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@Table(name = "reports") 19@NamedQueries({ 20 @NamedQuery( 21 name = "getAllReports", 22 query = "SELECT r FROM Report AS r ORDER BY r.id DESC" 23 ), 24 @NamedQuery( 25 name = "getReportsCount", 26 query = "SELECT COUNT(r) FROM Report AS r" 27 ), 28 @NamedQuery( 29 name = "getMyAllReports", 30 query = "SELECT r FROM Report AS r WHERE r.employee = :employee ORDER BY r.id DESC" 31 ), 32 @NamedQuery( 33 name = "getMyReportsCount", 34 query = "SELECT COUNT(r) FROM Report AS r WHERE r.employee = :employee" 35 ), 36 @NamedQuery( 37 name = "checkReportDate", 38 query = "SELECT COUNT(r) FROM Report AS r WHERE r.report_date = :report_date" 39 ) 40}) 41@Entity 42public class Report { 43 @Id 44 @Column(name = "id") 45 @GeneratedValue(strategy = GenerationType.IDENTITY) 46 private Integer id; 47 48 @ManyToOne 49 @JoinColumn(name = "employee_id", nullable = false) 50 private Employee employee; 51 52 @Column(name = "report_date", nullable = false, unique = true) 53 private Date report_date; 54 55 @Column(name = "title", length = 255, nullable = false) 56 private String title; 57 58 @Lob 59 @Column(name = "content", nullable = false) 60 private String content; 61 62 @Column(name = "start_time", nullable = true) 63 private Date start_time; 64 65 @Column(name = "end_time", nullable = true) 66 private Date end_time; 67 68 @Column(name = "created_at", nullable = false) 69 private Timestamp created_at; 70 71 @Column(name = "updated_at", nullable = false) 72 private Timestamp updated_at; 73 74 public Integer getId() { 75 return id; 76 } 77 78 public void setId(Integer id) { 79 this.id = id; 80 } 81 82 public Employee getEmployee() { 83 return employee; 84 } 85 86 public void setEmployee(Employee employee) { 87 this.employee = employee; 88 } 89 90 public Date getReport_date() { 91 return report_date; 92 } 93 94 public void setReport_date(Date report_date) { 95 this.report_date = report_date; 96 } 97 98 public String getTitle() { 99 return title; 100 } 101 102 public void setTitle(String title) { 103 this.title = title; 104 } 105 106 public String getContent() { 107 return content; 108 } 109 110 public void setContent(String content) { 111 this.content = content; 112 } 113 114 public Date getStart_time() { 115 return start_time; 116 } 117 118 public void setStart_time(Date start_time) { 119 this.start_time = start_time; 120 } 121 122 public Date getEnd_time() { 123 return end_time; 124 } 125 126 public void setEnd_time(Date end_time) { 127 this.end_time = end_time; 128 } 129 130 public Timestamp getCreated_at() { 131 return created_at; 132 } 133 134 public void setCreated_at(Timestamp created_at) { 135 this.created_at = created_at; 136 } 137 138 public Timestamp getUpdated_at() { 139 return updated_at; 140 } 141 142 public void setUpdated_at(Timestamp updated_at) { 143 this.updated_at = updated_at; 144 } 145 146} 147
新規日報作成時のControllerは以下になります。
Java
1package controllers.reports; 2 3import java.io.IOException; 4import java.sql.Date; 5import java.sql.Timestamp; 6import java.util.List; 7 8import javax.persistence.EntityManager; 9import javax.servlet.RequestDispatcher; 10import javax.servlet.ServletException; 11import javax.servlet.annotation.WebServlet; 12import javax.servlet.http.HttpServlet; 13import javax.servlet.http.HttpServletRequest; 14import javax.servlet.http.HttpServletResponse; 15 16import models.Employee; 17import models.Report; 18import models.validators.ReportValidator; 19import utils.DBUtil; 20 21/** 22 * Servlet implementation class ReportsCreateServlet 23 */ 24@WebServlet("/reports/create") 25public class ReportsCreateServlet extends HttpServlet { 26 private static final long serialVersionUID = 1L; 27 28 /** 29 * @see HttpServlet#HttpServlet() 30 */ 31 public ReportsCreateServlet() { 32 super(); 33 // TODO Auto-generated constructor stub 34 } 35 36 /** 37 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) 38 */ 39 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 40 String _token = (String)request.getParameter("_token"); 41 if(_token != null && _token.equals(request.getSession().getId())) { 42 EntityManager em = DBUtil.createEntityManager(); 43 44 Report r = new Report(); 45 46 r.setEmployee((Employee)request.getSession().getAttribute("login_employee")); 47 48 Date report_date = new Date(System.currentTimeMillis()); 49 String rd_str = request.getParameter("report_date"); 50 51 if(rd_str != null && !rd_str.equals("")) { 52 report_date = Date.valueOf(request.getParameter("report_date")); 53 } 54 r.setReport_date(report_date); 55 56 r.setTitle(request.getParameter("title")); 57 r.setContent(request.getParameter("content")); 58 59 r.setStart_time(Date.valueOf(request.getParameter("start_time"))); 60 r.setEnd_time(Date.valueOf(request.getParameter("end_time"))); 61 62 Timestamp currentTime = new Timestamp(System.currentTimeMillis()); 63 r.setCreated_at(currentTime); 64 r.setUpdated_at(currentTime); 65 66 List<String> errors = ReportValidator.validate(r, true); 67 if(errors.size() > 0) { 68 em.close(); 69 70 request.setAttribute("_token", request.getSession().getId()); 71 request.setAttribute("report", r); 72 request.setAttribute("errors", errors); 73 74 RequestDispatcher rd = request.getRequestDispatcher("/WEB-INF/views/reports/new.jsp"); 75 rd.forward(request, response); 76 } else { 77 em.getTransaction().begin(); 78 em.persist(r); 79 em.getTransaction().commit(); 80 em.close(); 81 request.getSession().setAttribute("flush", "登録が完了しました。"); 82 83 response.sendRedirect(request.getContextPath() + "/reports/index"); 84 } 85 } 86 } 87 88} 89
ご存じの方、ぜひともご教授いただけると幸いです。
回答1件
あなたの回答
tips
プレビュー