Spring Bootを使って、入力フォーム(info-form.html)で車の速度を受け取り、その速度によって出力結果が変わる出力フォーム(judge.html)を作成したいです。
しかし、入力を受け取ってから実際の出力ページに移動すると500とエラーが発生しました。
実現したいこと
・入力フォームで車の速度を受け取る。(問題なし)
・出力フォームで、受け取った速度に応じて出力結果を変える。(出力フォームまでいかず)
発生している問題・エラーメッセージ
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Thu Jan 26 23:20:56 JST 2023
There was an unexpected error (type=Internal Server Error, status=500).
Unresolved compilation problem: The method setSpeed(int) in the type Car is not applicable for the arguments (String)
java.lang.Error: Unresolved compilation problem:
The method setSpeed(int) in the type Car is not applicable for the arguments (String)
該当のソースコード
html
1~info-form.html~ 2<!DOCTYPE html> 3<html lang="ja" xmlns:th="http://www.thymeleaf.org"> 4 5<head> 6 <meta charset="utf-8"> 7 <title> Insert title here </title> 8</head> 9 10<body> 11 速度を入力してください<br> 12 <form action="if-unless.html" th:action="@{/receive/receive-info}" method="post"> 13 <input type="text" name="speed"><br> 14 <button>送信</button> 15 <form /> 16</body> 17 18</html> 19 20 21~judge .html~ 22<!DOCTYPE html> 23<html lang="ja" xmlns:th="http://www.thymeleaf.org"> 24 25<head> 26 <meta charset="utf-8"> 27 <title> Insert title here </title> 28</head> 29 30<body> 31 <span th:if="${car.speed <= 60}" th:text="法定速度です">dummy</span> 32 33 <span th:unless="${car.speed <= 60}" th;text="スピード違反です">dummy</span> 34 35</html>
java
1~JudgeController.java~ 2package com.example.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; 8 9import com.example.Form.ReceiveForm; 10import com.example.domain.Car; 11 12@Controller 13@RequestMapping("/receive") 14public class JudgeController { 15 16 @ModelAttribute 17 public ReceiveForm setUpForm() { 18 return new ReceiveForm(); 19 } 20 21 @RequestMapping("") 22 public String index() { 23 return "info-form"; 24 } 25 26 @RequestMapping("/receive-info") 27 public String inputSpeed(String speed,Model model) { 28 Car car = new Car(); 29 30 int speed = 0; 31 32 model.addAttribute("speed",speed); 33 34 car.setSpeed(speed); 35 36 return "judge"; 37 } 38 39} 40 41 42 43~ReceiveForm.java~ 44package com.example.Form; 45 46public class ReceiveForm { 47 private String name; 48 private String speed; 49 50 public String getName() { 51 return name; 52 } 53 public void setName(String name) { 54 this.name = name; 55 } 56 public String getSpeed() { 57 return speed; 58 } 59 public void setSpeed(String speed) { 60 this.speed = speed; 61 } 62 63 @Override 64 public String toString() { 65 return "ReceiveForm [name=" + name + ", speed=" + speed + "]"; 66 } 67 68} 69 70 71~Car.java~ 72package com.example.domain; 73 74public class Car { 75 private String name; 76 private int speed; 77 78 public String getName() { 79 return name; 80 } 81 82 public void setName(String name) { 83 this.name = name; 84 } 85 86 public int getSpeed() { 87 return speed; 88 } 89 90 public void setSpeed(int speed) { 91 this.speed = speed; 92 } 93 94}
試したこと
・各ファイルの格納場所は問題ないと考えております。
・恐らく、JudgeControllerのコード(inputSpeedメソッド)が違うと思うのですが、
考えても分からないため、何卒ご教示願います。
補足情報(FW/ツールのバージョンなど)
Springのバージョンは3.0
javaのバージョンは17です。
あなたの回答
tips
プレビュー