前提・実現したいこと
プログラミング初心者です。
オンラインスクールで学んだ時に作ったアプリケーションを改良して、アプリケーションを作っているのですが、
その過程で下記のエラーが発生したのですが、
ソースコードに
<c:forEach var="trucks" items="${trucks}" varStatus="status"><tbody></tbody></c:forEach>
としたところ、エラー自体は消えてindex画面が表示されました。
このエラーは文字列を数値型に変換した際に数値以外が含まれていると起こるエラーということは調べてわかったのですが、
自分で調べていても文字列を数値に変換する処理を発見出来ず、
また、なぜ上記のようにすると例外を投げなくなったのかが分かりません。
###<c:forEach>を入れた理由
DBに登録した内容を車両毎に1日づつ表示する、という処理を31日分ループさせる意図で追加しました。
(実際には一つの内容を31日分ループ表示する処理になっていました)
発生している問題・エラーメッセージ
java.lang.NumberFormatException: For input string: "office_name"
該当のソースコード
index.jsp
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 5 6<c:import url="/WEB-INF/views/layout/app.jsp"> 7 <c:param name="content"> 8 <c:if test="${flush != null} }"> 9 <div id="flush_success"> 10 <c:out value="${flush}"></c:out> 11 </div> 12 </c:if> 13 <h2>車両 一覧</h2> 14 15 <p><a href="<c:url value='/dispatch/new' />">新規登録</a></p> 16 <p><a href="<c:url value='/dispatch/edit' />">登録情報変更</a></p> 17 <table class="Truck_table"> 18 <thead> 19 <tr> 20 <th class="space1" rowspan="2" colspan="4"></th> 21 <% 22 for (int i =0; i<31; i++){ 23 out.println("<th class='date' colspan='4'>" + "1月" +(i+1)+"日" + "</th>"); 24 } 25 %> 26 </tr> 27 <tr> 28 <th>宵積卸地</th> 29 <th>積地</th> 30 <th>卸地</th> 31 <th>宵積</th> 32 </tr> 33 </thead> 34 <c:forEach var="trucks" items="${trucks}" varStatus="status"><!--このループ処理を入れると例外を投げなくなる--> 35 <tbody class ="scroll_body"> 36 <tr> 37 <td class="office_name" rowspan="4"><c:out value="${trucks.office_name}"></c:out></td> 38 <td class ="truck_code" colspan="2" rowspan="4"><c:out value="${trucks.truck_code}"></c:out></td> 39 <td class ="truck_type" rowspan="4"><c:out value="${trucks.truck_type}"></c:out></td> 40 41 <td class ="time"><c:out value="${trucks.mw_time}"></c:out></td> 42 <td class ="time"><c:out value="${trucks.sa_time}"></c:out></td> 43 <td class ="time"><c:out value="${trucks.wa_time}"></c:out></td> 44 <td class ="time"><c:out value="${trucks.es_time}"></c:out></td> 45 46 </tr> 47 <tr> 48 <c:forEach begin="1" end="31"> 49 <td class ="area"><c:out value="${trucks.m_wholesale_area}"></c:out></td> 50 <td class ="area"><c:out value="${trucks.stack_area}"></c:out></td> 51 <td class ="area"><c:out value="${trucks.wholesale_area}"></c:out></td> 52 <td class ="area"><c:out value="${trucks.e_stack_area}"></c:out></td> 53 </c:forEach> 54 </tr> 55 <tr> 56 57 <td class ="time"><td> 58 <td class ="time"><td> 59 <td class ="time"><td> 60 <td class ="time"><td> 61 62 </tr> 63 <tr> 64 <td class ="area"></td> 65 <td class ="area"></td> 66 <td class ="area"></td> 67 <td class ="area"></td> 68 69 </tr> 70 </tbody> 71 </c:forEach> 72 </table> 73 </c:param> 74</c:import>
DispatchIndexServlet.java
Java
1package controllers.dispatch; 2 3 4import java.io.IOException; 5import java.util.List; 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.Trucks; 16import utils.DBUtil; 17 18/** 19 * Servlet implementation class DispatchIndexServlet 20 */ 21@WebServlet("/dispatch/index") 22public class DispatchIndexServlet extends HttpServlet { 23 private static final long serialVersionUID = 1L; 24 25 /** 26 * @see HttpServlet#HttpServlet() 27 */ 28 public DispatchIndexServlet() { 29 super(); 30 } 31 /** 32 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) 33 */ 34 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 35 EntityManager em = DBUtil.createEntityManager(); 36 37 38 39 List<Trucks> trucks = em.createNamedQuery("getAllTrucks",Trucks.class) 40 .getResultList(); 41 42 long trucks_count = (long)em.createNamedQuery("getTrucksCount", Long.class) 43 .getSingleResult(); 44 45 em.close(); 46 47 48 49 50 request.setAttribute("trucks", trucks); 51 request.setAttribute("trucks_count", trucks_count); 52 if(request.getSession().getAttribute("flush") != null) { 53 request.setAttribute("flush", request.getSession().getAttribute("flush")); 54 request.getSession().removeAttribute("flush"); 55 } 56 RequestDispatcher rd = request.getRequestDispatcher("/WEB-INF/views/dispatch/index.jsp"); 57 rd.forward(request, response); 58 } 59}
models.Trucks.java
Java
1package models; 2import java.sql.Date; 3import java.sql.Time; 4 5import javax.persistence.Column; 6import javax.persistence.Entity; 7import javax.persistence.GeneratedValue; 8import javax.persistence.GenerationType; 9import javax.persistence.Id; 10import javax.persistence.NamedQueries; 11import javax.persistence.NamedQuery; 12import javax.persistence.Table; 13 14@Table(name = "trucks") 15@NamedQueries({ 16 @NamedQuery( 17 name = "getAllTrucks", 18 query = "SELECT t FROM Trucks AS t" 19 ), 20 @NamedQuery( 21 name = "getTrucksCount", 22 query = "SELECT COUNT(t) FROM Trucks AS t" 23 ), 24}) 25@Entity 26public class Trucks{ 27 @Id 28 @Column(name = "id") 29 @GeneratedValue(strategy = GenerationType.IDENTITY) 30 private Integer id; 31 32 @Column(name = "task_date", nullable = true) 33 private Date task_date; 34 35 @Column(name = "task_time", nullable = true) 36 private Time task_time; 37 38 @Column(name = "office_name", nullable = true) 39 private String office_name; 40 41 @Column(name = "truck_code", nullable = true) 42 private String truck_code; 43 44 @Column(name = "stack_area", nullable = true) 45 private String stack_area; 46 47 @Column(name = "wholesale_area", nullable = true) 48 private String wholesale_area; 49 50 @Column(name = "truck_type", nullable = true) 51 private String truck_type; 52 53 @Column(name = "wa_time", nullable = true) 54 private String wa_time; 55 56 57 @Column(name = "sa_time", nullable = true) 58 private String sa_time; 59 60 @Column(name = "e_stack_area", nullable = true) 61 private String e_stack_area; 62 63 @Column(name = "m_wholesale_area", nullable = true) 64 private String m_wholesale_area; 65 66 @Column(name = "mw_time", nullable = true) 67 private String mw_time; 68 69 70 @Column(name = "es_time", nullable = true) 71 private String es_time; 72 73 @Column(name = "mw_text", nullable = true) 74 private String mw_text; 75 76 @Column(name = "mw_select", nullable = true) 77 private String mw_select; 78 79 @Column(name = "sa_text", nullable = true) 80 private String sa_text; 81 82 @Column(name = "sa_select", nullable = true) 83 private String sa_select; 84 85 @Column(name = "wa_text", nullable = true) 86 private String wa_text; 87 88 @Column(name = "wa_select", nullable = true) 89 private String wa_select; 90 91 @Column(name = "es_text", nullable = true) 92 private String es_text; 93 94 @Column(name = "es_select", nullable = true) 95 private String es_select; 96 97 @Column(name = "wa_time2", nullable = true) 98 private String wa_time2; 99 100 @Column(name = "sa_time2", nullable = true) 101 private String sa_time2; 102 103 @Column(name = "stack_area2", nullable = true) 104 private String stack_area2; 105 106 @Column(name = "wholesale_area2", nullable = true) 107 private String wholesale_area2; 108 109 @Column(name = "sa_text2", nullable = true) 110 private String sa_text2; 111 112 @Column(name = "sa_select2", nullable = true) 113 private String sa_select2; 114 115 @Column(name = "wa_text2", nullable = true) 116 private String wa_text2; 117 118 @Column(name = "wa_select2", nullable = true) 119 private String wa_select2; 120 121 122 123 public Integer getId() { 124 return id; 125 } 126 127 public void setId(Integer id) { 128 this.id = id; 129 } 130<!--(以降、カラム毎に上記と同じ処理を繰り返すため省略)--> 131}
補足情報(FW/ツールのバージョンなど)
MySQL、Eclipse4.6Neon
回答3件
あなたの回答
tips
プレビュー