Commit 43f23650 by Patryk Czarnik

Oddzielnie GET i POST

parent 6f4ffb0e
......@@ -2,23 +2,28 @@ package alx.kalkulator;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class KalkulatorController {
@GetMapping("/kalkulator")
public String kalkulatorGet() {
return "kalkulator.html";
}
@RequestMapping("/kalkulator")
@PostMapping("/kalkulator")
public String kalkulator(Long liczba1, Long liczba2, String operacja, Model model) {
if(liczba1 != null && liczba2 != null && operacja != null) {
Long wynik = switch (operacja) {
case "+" -> liczba1 + liczba2;
case "-" -> liczba1 - liczba2;
case "*" -> liczba1 * liczba2;
case "/" -> liczba1 / liczba2;
default -> 0L;
};
model.addAttribute("wynik", wynik);
}
Long wynik = switch (operacja) {
case "+" -> liczba1 + liczba2;
case "-" -> liczba1 - liczba2;
case "*" -> liczba1 * liczba2;
case "/" -> liczba1 / liczba2;
default -> 0L;
};
model.addAttribute("wynik", wynik);
return "kalkulator.html";
}
}
......@@ -8,7 +8,7 @@
<body>
<h1>Kalkulator</h1>
<form method="get" class="kalkulator">
<form method="post" class="kalkulator">
<input type="number" name="liczba1" th:value="${param.liczba1}">
<select name="operacja">
<option value="+">+</option>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment