前提・実現したいこと
【実現したいこと】
テーブルに格納されている個人の生年月日から、Calculationクラスで年齢を算出し、表示する。
【現状】
テーブルには、氏名と生年月日(yyyymmdd)がある。
【理想手順】
①フォームに年齢の下限と上限を入力。
②入力された値を引数に検索をかけるため、取得する。
③テーブルに格納されている生徒の生年月日を基に、生徒1人1人の年齢を算出(Calculationクラスを利用)
④②で取得した値を引数に、生徒の年齢がその下限値と上限値の間かどうか検索をかける。
⑤検索にかかった、全生徒の氏名と年齢を表示。
発生している問題
・年齢を新しく持ったListを作ろうとしている。
しかし、他のサイトを参考にしてみても上手く作れていない。
そもそも、一人ずつの年齢の算出方法でつまずいている。
該当のソースコード
Controller
1package com.tuyano.springboot; 2 3@Controller 4public class AgeController { 5 6 @Autowired 7 private AgeService service; 8 9 @PersistenceContext 10 EntityManager entityManager; 11 12// ダミーデータの作成 13 @PostConstruct 14 public void init() { 15 Age A1 = new Age(); 16 E1.setStudentName("鈴木"); 17 E1.setBirthYMD(20070802); 18 Age A2 = new Age(); 19 E2.setStudentName("佐藤"); 20 E2.setBirthYMD(20080506); 21 Age A3 = new Age(); 22 E3.setStudentName("青木"); 23 E3.setBirthYMD(20071109); 24 } 25 26 @RequestMapping(value = "/search", method = RequestMethod.GET) 27 public ModelAndView age(ModelAndView mav) { 28 mav.setViewName("search"); 29 mav.addObject("msg", "生徒検索画面です!"); 30 mav.addObject("value", ""); 31 return mav; 32 } 33 34 @RequestMapping(value = "/search", method = RequestMethod.POST) 35// 検索機能 36 public ModelAndView search(HttpServletRequest request, 37 ModelAndView mav) { 38 mav.setViewName("search"); 39 mav.addObject("msg", "検索結果です!"); 40 41// 年齢下限・上限の引数を取得 42 int downageparam = Integer.parseInt(request.getParameter("downage")); 43 int upageparam = Integer.parseInt(request.getParameter("upage")); 44 45// 入力値を引数にDBへ検索をかける 46 List<Age> list = service.find(downageparam, upageparam ); 47 mav.addObject("datalist", list); 48 49 return mav; 50 51}
AgeService
1package com.tuyano.springboot; 2 3@Service 4public class AgeService { 5 6 @PersistenceContext 7 private EntityManager entityManager; 8 9 @SuppressWarnings("unchecked") 10 public List<Age>find(int downage, int upage) { 11 List<Age> list = null; 12 String qstr = "from Age where " 13 + " age >= :downageparam and age <= :upageparam "; 14 Query queryName = entityManager.createQuery(qstr) 15 .setParameter("downageParam", downage) 16 .setParameter("upagePara,", upage); 17 list = queryName.getResultList(); 18 return list; 19 } 20 21}
HTML
1<!DOCTYPE html> 2<html xmlns:th="http://www.thymeleaf.org"> 3<head> 4 <title>top page</title> 5 <meta http-equiv="Content-Type" 6 content="text/html; charset=UTF-8" /> 7</head> 8<body> 9 <h1 th:text="#{content.title}">Student Search</h1> 10 <p th:text="${msg}">生徒検索画面です。</p> 11 <table> 12 <form action="/search" method="post" > 13 <tr><td><label for="age">年齢</label></td> 14 <td> 15 <input type="text" name="downage" size="2" /> 16 </td> 17 <td>~</td> 18 <td> 19 <input type="text" name="upage" size="2" />歳 20 </td></tr> 21 <td> 22 <input type="submit" name="kensaku" value="検索" /> 23 </td> 24 </tr> 25 </form> 26 </table> 27 <table> 28 <tr><th>生徒名</th><th>年齢</th></tr> 29 <tr th:each="obj : ${datalist}"> 30 <td th:text="${obj.StudentName}"></td> 31 <td th:text="${obj.old}"></td> 32 </tr> 33 <tr> 34 <td> 35 <input type="submit" name="return" value="戻る" /> 36 </td> 37 </tr> 38 </table> 39</body> 40</html>
Calculation
1package tuyano.calculation; 2 3@Service 4public class Calculation{ 5 6// 現在の年度を算出 7 int nendo = LocalDate.now().minusMonths(3).getYear(); 8 9// 年度計算を実施し、年齢をリターン 10 public int getAge(int birthYMD) { 11 int age = 0; 12 age = nendo - birthYMD; 13 return age; 14 } 15 16}
試したこと
・年齢を配列で取得しようとした
→List内のデータと一致させられずに、表示まで出来なかった。