解決したいこと
BigDecimalで足し算だけでなく引き算もできるようにしたいのですがうまくいかずreturn Resultで複数の結果を出せるようにし足し算だけでなく引き算も出力したいです。
私はJava初学者です。マークダウンについても勉強中です。できる限り修正したのを再度投稿いたします。
EclipseでJavaでフレームワークはspringboot、テンプレートエンジンはThymeleafを使い簡単な計算機を作成中です。
既に足し算のみの計算は成功いたしました。
その成功したコードに引き算ができるようBigDecimalを追加いたしました、
ですが追加したところ(b3Result は解決できないか、フィールドではありません)となってしまします。
もし引き算が成功しましたら、掛け算、割り算も追加していく予定です。
記述にはBigDecimal以外にも書き方はありますが、今回は自分の頭がこんがらがってしましますので今回はBigDecimalを使っていきたいです。
自分なりに調べた結果calcService.Javaはもちろんcontrollerの部分に影響があるのかなと感じました。
成功画面としては、http://localhost:8080/calc
で計算するための枠を作り、送信ボタンを押すとhttp://localhost:8080/res
で答えがそれぞれ帰ってくるようにしたいです。
解決方法を教えて下さい。
発生している問題・エラー CalcService.java
return AdResult.b3Result;
1 WebCalc01Application.java
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class WebCalc01Application
{
public static void main(String[] args)
{
SpringApplication.run(WebCalc01Application.class, args);
}
}
2 CalcController.java
package com.example.demo.controllers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.example.demo.services.CalcService;
@Controller 。
public class CalcController {
@Autowired
private CalcService service;
@PostMapping("res")
public String calc(
Model model,
@RequestParam("numA")String NumA,
@RequestParam("numB")String NumB,
@RequestParam("numC")String NumC,
@RequestParam("numD")String NumD
)
{
model.addAttribute("kotae", service.calculate(NumA,NumB,NumC,NumD));
return "res";
}
}
3 IndexController
package com.example.demo.controllers;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class IndexController {
@RequestMapping()
public String index() {
return "index";
}
@RequestMapping("calc")
public String calc() {
return "calc";
}
}
4 CalcService.java 問題のエラーがあるパッケージ
package com.example.demo.services;
import java.math.BigDecimal;
import org.springframework.stereotype.Component;
@Component
public class CalcService {
public String calculate(
String NumA,
String NumB,
String NumC,
String NumD
)
{
BigDecimal AdA = new BigDecimal(NumA);
BigDecimal AdB = new BigDecimal(NumB);
BigDecimal AdR = AdA.add(AdB);
String AdResult = AdR.toString();
BigDecimal b1 = new BigDecimal(NumC);
BigDecimal b2 = new BigDecimal(NumD);
BigDecimal b3 = b1.subtract(b2);
String b3Result = b3.toString();
return AdResult.b3Result;
}
}
5 calc.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" xmlns:th="http://www.thymeleaf.org">
<title>web足し算</title>
</head>
<body>
<p>数値を入力してください!</p>
<form method="POST" action="res">
<input type="text" inputmode=decimal name="numA" value=0>
<b>+</b>
<input type="text" inputmode=decimal name="numB" value=0>
<input type=submit value="送信">
</form>
<form method="POST" action="res">
<input type="text" inputmode=decimal name="numC" value=0>
<b>-</b>
<input type="text" inputmode=decimal name="numD" value=0>
<input type=submit value="送信">
</form>
</body>
</html>
6 index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" xmlns:th="http://www.thymeleaf.org">
<title>egap xedni</title>
</head>
<body>
<h1>ここはインデックスページです。</h1>
<p>これから足し算、引き算、掛け算、割り算をしていきます。</p>
<p>下のリンクをクリックしてね!</p>
<a href="calc">calcへリンクします</a>
</body>
</html>
7 res.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<p>計算結果!</p>
<!-- 足し算表示欄 -->
<b th:text="${kotae}" class="adresult">0</b>
<b th:text="${kotae}" class="b3Result">0</b>
<!--class="adresult">
<p></p>
</body>
</html>
自分で試したこと
res.htmlの部分は
<b th:text="${kotae}" class="adresult"></b>
<b th:text="${kotae}" class="b3Result"></b>
を
<b th:text="${kotae}" class="adresult,"b3Result">
にしても問題はないのかなと感じました。
ですがそこはHTMLの部分ですからcalcService.Javaにエラーがなかったの話ですよね、、
return Resultの書き方についても調べましたが色々な情報が入って来すぎた為あまり理解できませんでした。