前提
JavaのSpringを使いデータベースから取得した値を
JSPに渡して画面に表示させたいです。
java.lang.NumberFormatExceptionエラーとなり、jspでの表示ができません。
プロパティ名の付け方がおかしいのでしょうか…
どうかご解答をお願い致します。
実現したいこと
- "LAST_NAME"というテーブルから"LAST_NAME"というカラムのデータを1件取得し、"input.jsp"にJSTLで表示させたいです。
- 学習用のため「STUDENTデータ作成画面」なるものを作っています。
- 現在は動きの確認のために、LAST_NAMEのみ取得していますが、sql文は変更して複数のデータを取得します。
発生している問題・エラーメッセージ
エラーメッセージ HTTPステータス 500 - Internal Server Error Type Exception Report メッセージ java.lang.NumberFormatException: For input string: "studentName" 説明 The server encountered an unexpected condition that prevented it from fulfilling the request. 例外 org.apache.jasper.JasperException: java.lang.NumberFormatException: For input string: "studentName" org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:560) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:476) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340) javax.servlet.http.HttpServlet.service(HttpServlet.java:729) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:170) org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:314) org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1325) org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1069) org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1008) org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:925) org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:978) org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:870) javax.servlet.http.HttpServlet.service(HttpServlet.java:622) org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:855) javax.servlet.http.HttpServlet.service(HttpServlet.java:729) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200) org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
該当のソースコード
package jp.co.----.controller; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; import jp.co.----.dto.Student; import jp.co.----.service.CreateStudentService; //http://localhost:8080/createStudent/inputPage.html @Controller public class CreateController { @Autowired private CreateStudentService createStudentService; @RequestMapping("/inputPage") public String show() { return "input"; } @RequestMapping("/getStudent") public ModelAndView getStudentInfo() { List<Student> studentInfo = createStudentService.createStudent(); return new ModelAndView("input", "studentInfo", studentInfo); } }
package jp.co.----.service; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import jp.co.----.dao.StudentDao; import jp.co.----.dto.Student; @Service public class CreateStudentService { @Autowired private StudentDao studentDao; public List<Student> createStudent() { List<Student> studentInfo = studentDao.getRandom(); return studentInfo; } }
package jp.co.----.dao; import java.sql.ResultSet; import java.sql.SQLException; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.BeanPropertyRowMapper; import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; import org.springframework.stereotype.Repository; import jp.co.----.dto.Student; @Repository public class StudentDao { @Autowired private NamedParameterJdbcTemplate temp; private class StudentRowMapper extends BeanPropertyRowMapper<Student>{ @Override public Student mapRow(ResultSet rs, int rowNum) throws SQLException{ Student student = new Student(); student.setStudentName(rs.getString("LAST_NAME")); return student; } } //ランダムでDBから取得する public List<Student> getRandom(){ String sql = "select * from LAST_NAME "; List<Student> studentInfo = this.temp.query(sql, new StudentRowMapper()); return studentInfo; } }
package jp.co.----.dto; public class Student { private String studentName; public String getStudentName() { return studentName; } public void setStudentName(String studentName) { this.studentName = studentName; } }
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>入力ページ</title> </head> <body> <form action="" method="post"> STUDENT_NO <input type="text"> <br> NAME <input type="text" name="name" value="<c:out value="${studentInfo.studentName}"/>"> <br> AGE <input type="text" name="age" value=""> <br> ADDRESS <input type="text" name="address" value=""> <br> CLASS_NO <input type="text" name="class" value=""> <br> <input type="submit" value="登録"> <a href="/createStudent/getStudent.html">ランダム取得</a> </form> </body> </html>
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。
回答1件
あなたの回答
tips
プレビュー