Commit 6f4ffb0e by Patryk Czarnik

Kalkulator - wersja GET

parent b0126174
package alx.kalkulator; package alx.kalkulator;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
@Controller @Controller
public class KalkulatorController { public class KalkulatorController {
@RequestMapping("/kalkulator") @RequestMapping("/kalkulator")
public String 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);
}
return "kalkulator.html"; return "kalkulator.html";
} }
} }
...@@ -21,6 +21,14 @@ ...@@ -21,6 +21,14 @@
<button>Oblicz</button> <button>Oblicz</button>
</form> </form>
<div th:if="${wynik != null}" class="wynik">
<span th:text="${param.liczba1}">2</span>
<span th:text="${param.operacja}">+</span>
<span th:text="${param.liczba2}">3</span>
=
<strong th:text="${wynik}">5</strong>
</div>
<div><a th:href="@{/}">wróć do strony głównej</a></div> <div><a th:href="@{/}">wróć do strony głównej</a></div>
</body> </body>
......
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