前提・実現したいこと
プログラミング初心者です。
現在、https://qiita.com/mi0/items/7e63f3212091c3786370 のサイトを見て、MVCを用いた簡易的な計算アプリを作成しています。
・作成したサンプルを実行したのですが、アプリの裏(?)で出現したエラーを削除することができません。
・システム自体は問題なく動作します。
・CalculateController.javaにおけるmodel.addAttribute("form", new Form());から、index.htmlのth:object="${form}"への値の受け渡しがうまくいっていないのでしょうか?
何卒、ご教授お願い致します。
また、エラーが発生しているにも関わらず、システムが動作している理由もご教授いただければ幸いです。
発生している問題・エラーメッセージ
ビルド時にはコンソールにエラー等は表示されませんが、画面を読み込みすると、以下のエラーが出て内部エラー500になります。
java.lang.IllegalStateException:
Neither BindingResult nor plain target object for bean name 'form' available as request attribute
該当のソースコード
Java
1// Form.java 2package com.example.demo.model; 3 4public class Form { 5 6 private int num1 = 0; 7 private int num2 = 0; 8 private int total = 0; 9 10 public int getNum1() { 11 return num1; 12 } 13 14 public void setNum1(int num1) { 15 this.num1 = num1; 16 } 17 18 public int getNum2() { 19 return num2; 20 } 21 22 public void setNum2(int num2) { 23 this.num2 = num2; 24 } 25 26 public int getTotal() { 27 return total; 28 } 29 30 public void setTotal(int total) { 31 this.total = total; 32 } 33 34 public void sumTotal() { 35 total = this.num1 + this.num2; 36 setTotal(total); 37 } 38}
Java
1//CalculateController.java 2package com.example.demo.controller; 3 4import org.springframework.stereotype.Controller; 5import org.springframework.ui.Model; 6import org.springframework.web.bind.annotation.ModelAttribute; 7import org.springframework.web.bind.annotation.RequestMapping; 8import org.springframework.web.bind.annotation.RequestMethod; 9 10import com.example.demo.model.Form; 11 12@Controller 13public class CalculateController { 14 @RequestMapping("/calculate") 15 public String form(Model model) { 16 model.addAttribute("form", new Form()); 17 return "index"; 18 } 19 20 @RequestMapping(value = "/result", method = RequestMethod.POST) 21 public String result(@ModelAttribute Form form, Model model) { 22 form.sumTotal(); 23 model.addAttribute("form", form); 24 return "result"; 25 } 26}
HTML
1//index.html 2<!DOCTYPE html> 3<html xmlns:th="http://www.thymeleaf.org"> 4<head> 5<title>Index</title> 6<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 7</head> 8<body> 9 <h1>Form</h1> 10 <form action="#" th:action="@{/result}" th:object="${form}" method="post"> 11 <p>first number: <input type="number" th:field="*{num1}" /></p> 12 <p>Second number: <input type="number" th:field="*{num2}" /></p> 13 <p><input type="submit" value="calculate" /></p> 14 </form> 15</body> 16</html>
HTML
1//result.html 2<!DOCTYPE HTML> 3<html xmlns:th="http://www.thymeleaf.org"> 4<head> 5<title>Result</title> 6<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 7</head> 8<body> 9 <h1>Result</h1> 10 <p th:text="'Answer: ' + ${form.getTotal()}" /> 11 <a href="/calculate">Calculate Another number</a> 12</body> 13</html>
試したこと
・index.html, CalculateControllerにおけるオブジェクト名のチェック
・//CalculateControllerにおけるアノテーションチェック
補足情報(FW/ツールのバージョンなど)
開発環境:eclipse
javaVersion = 17
springBootVersion = 2.7.4
あなたの回答
tips
プレビュー