前提・実現したいこと
JavaのオブジェクトクラスをListにキャストしたい
発生している問題・エラーメッセージ
Stacktrace: org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:626) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:515) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:385) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329) javax.servlet.http.HttpServlet.service(HttpServlet.java:741) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) jp.topgate.gourmetshibuya.servlet.IndexServlet.doGet(IndexServlet.java:41) javax.servlet.http.HttpServlet.service(HttpServlet.java:634) javax.servlet.http.HttpServlet.service(HttpServlet.java:741) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) 根本原因 java.lang.ClassCastException: class beanList.RestaurantList cannot be cast to class java.util.List (beanList.RestaurantList is in unnamed module of loader org.apache.catalina.loader.ParallelWebappClassLoader @7bc10d84; java.util.List is in module java.base of loader 'bootstrap') org.apache.jsp.index_jsp._jspService(index_jsp.java:272) org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:71) javax.servlet.http.HttpServlet.service(HttpServlet.java:741) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:477) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:385) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329) javax.servlet.http.HttpServlet.service(HttpServlet.java:741) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) jp.topgate.gourmetshibuya.servlet.IndexServlet.doGet(IndexServlet.java:41) javax.servlet.http.HttpServlet.service(HttpServlet.java:634) javax.servlet.http.HttpServlet.service(HttpServlet.java:741) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
該当のソースコード
index.jsp
1<section id="portfolio"> 2 <div class="container-fluid p-0"> 3 <div class="row no-gutters"> 4 エラー箇所<%List<RestaurantBean> reList = (List<RestaurantBean>)request.getAttribute("RestaurantInfo"); 5 Iterator<RestaurantBean> iterator = reList.iterator(); 6 RestaurantBean rb = new RestaurantBean(); 7 while(iterator.hasNext()){ 8 rb = iterator.next();%> 9 <div class="col-lg-4 col-sm-6"> 10 <a class="portfolio-box" href="assets/img/portfolio/fullsize/1.jpg"><img 11 class="img-fluid" src="assets/img/portfolio/thumbnails/1.jpg" alt="" /> 12 <div class="portfolio-box-caption"> 13 <div class="project-name"><%=rb.getName() %></div> 14 </div></a> 15 </div> 16 <%} %>
IndexServlet.jsp
1protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 2 // TODO Auto-generated method stub 3 //response.getWriter().append("Served at: ").append(request.getContextPath()); 4 RestaurantDao rdao = new RestaurantDao(); 5 RestaurantList rl = new RestaurantList(); 6 rl = rdao.getResutaurantLimit6(); 7 request.setAttribute("RestaurantInfo", rl); 8 RequestDispatcher rd = request.getRequestDispatcher("index.jsp"); 9 rd.forward(request, response); 10 }
RestaurantList.Java
1package beanList; 2 3import java.util.ArrayList; 4import java.util.List; 5 6import jp.topgate.gourmetshibuya.beans.RestaurantBean; 7 8public class RestaurantList { 9 private List<RestaurantBean> resutaurantList; 10 11 public RestaurantList() { 12 // TODO 自動生成されたコンストラクター・スタブ 13 resutaurantList = new ArrayList<RestaurantBean>(); 14 } 15 16 public List<RestaurantBean> getResutaurantList() { 17 return resutaurantList; 18 } 19 20 public void addResutaurantList(RestaurantBean rb) { 21 resutaurantList.add(rb); 22 } 23} 24
RestaurantBean.Java
1package jp.topgate.gourmetshibuya.beans; 2 3public class RestaurantBean { 4 private int restaurant_id; 5 private String name; 6 private String place; 7 private String phonenum; 8 private String genres; 9 private String photo; 10 private int evaluation; 11 private String business_hours; 12 private String url; 13 private String regular_holiday; 14 private String content; 15 16 public int getRestaurant_id() { 17 return restaurant_id; 18 } 19 public void setRestaurant_id(int restaurant_id) { 20 this.restaurant_id = restaurant_id; 21 } 22 public String getName() { 23 return name; 24 } 25 public void setName(String name) { 26 this.name = name; 27 } 28 public String getPlace() { 29 return place; 30 } 31 public void setPlace(String place) { 32 this.place = place; 33 } 34 public String getPhonenum() { 35 return phonenum; 36 } 37 public void setPhonenum(String phonenum) { 38 this.phonenum = phonenum; 39 } 40 public String getGenres() { 41 return genres; 42 } 43 public void setGenres(String genres) { 44 this.genres = genres; 45 } 46 public String getPhoto() { 47 return photo; 48 } 49 public void setPhoto(String photo) { 50 this.photo = photo; 51 } 52 public int getEvaluation() { 53 return evaluation; 54 } 55 public void setEvaluation(int evaluation) { 56 this.evaluation = evaluation; 57 } 58 public String getBusiness_hours() { 59 return business_hours; 60 } 61 public void setBusiness_hours(String business_hours) { 62 this.business_hours = business_hours; 63 } 64 public String getUrl() { 65 return url; 66 } 67 public void setUrl(String url) { 68 this.url = url; 69 } 70 public String getRegular_holiday() { 71 return regular_holiday; 72 } 73 public void setRegular_holiday(String regular_holiday) { 74 this.regular_holiday = regular_holiday; 75 } 76 public String getContent() { 77 return content; 78 } 79 public void setContent(String content) { 80 this.content = content; 81 } 82} 83
補足情報(FW/ツールのバージョンなど)
Java11 Eclipse MYSQL
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/05/25 09:25 編集
2020/05/25 14:57
2020/05/26 01:07 編集