Commit eb1a7b45 by Patryk Czarnik

Historia działań

parent a48016ea
...@@ -13,9 +13,8 @@ import org.springframework.web.bind.annotation.RequestMapping; ...@@ -13,9 +13,8 @@ import org.springframework.web.bind.annotation.RequestMapping;
// Gdyby w którejś metodzie też był podany adres, byłoby to traktowane jako dalszy ciąg adresu, np. /kalkulator/historia // Gdyby w którejś metodzie też był podany adres, byłoby to traktowane jako dalszy ciąg adresu, np. /kalkulator/historia
public class Kalkulator { public class Kalkulator {
/* Wstrzykiwanie zależności (dependency injection). /* Wstrzykiwanie zależności (dependency injection).
W jednym komponencie chcemy uzywać innego komponentu. W jednym komponencie chcemy używać innego komponentu.
Zamiast samodzielnie tworzyć obiekty i je ze sobą wiązać (co w większych aplikacjach jest żmudne), Zamiast samodzielnie tworzyć obiekty i je ze sobą wiązać (co w większych aplikacjach jest żmudne), możemy "poprosić" o to Springa.
możemy "poprosić" o to Springa.
Do pola w tej klasie Spring automatycznie wpisze referencję do obiektu tamtej klasy. Do pola w tej klasie Spring automatycznie wpisze referencję do obiektu tamtej klasy.
Dowiązania z różnych miejsc aplikacji domyślnie prowadzą do jednego wspólnego obiektu (tego singletona / beana / komponentu). Dowiązania z różnych miejsc aplikacji domyślnie prowadzą do jednego wspólnego obiektu (tego singletona / beana / komponentu).
...@@ -38,5 +37,12 @@ public class Kalkulator { ...@@ -38,5 +37,12 @@ public class Kalkulator {
return "kalkulator.html"; return "kalkulator.html";
} }
// Ta metoda będzie działać pod adresem /kalkulator/historia
@GetMapping("/historia")
public String wyswietlHistorie(Model model) {
model.addAttribute("historia", logika.getHistoriaDzialan());
return "historia.html";
}
} }
package com.example.demo; package com.example.demo;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
/* LogikaKalkulatora jest przykładem "komponentu". /* LogikaKalkulatora jest przykładem "komponentu".
...@@ -12,14 +16,21 @@ import org.springframework.stereotype.Component; ...@@ -12,14 +16,21 @@ import org.springframework.stereotype.Component;
@Component @Component
public class LogikaKalkulatora { public class LogikaKalkulatora {
private final List<String> historiaDzialan = Collections.synchronizedList(new ArrayList<>());
public long oblicz(long liczba1, long liczba2, String operacja) { public long oblicz(long liczba1, long liczba2, String operacja) {
switch(operacja) { long wynik = switch(operacja) {
case "+": return liczba1 + liczba2; case "+" -> liczba1 + liczba2;
case "-": return liczba1 - liczba2; case "-" -> liczba1 - liczba2;
case "*": return liczba1 * liczba2; case "*" -> liczba1 * liczba2;
case "/": return liczba1 / liczba2; case "/" -> liczba1 / liczba2;
default : return 0; default -> 0;
} };
historiaDzialan.add(String.format("%d %s %d = %d", liczba1, operacja, liczba2, wynik));
return wynik;
}
public List<String> getHistoriaDzialan() {
return Collections.unmodifiableList(historiaDzialan);
} }
} }
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Historia działań</title>
<link rel="stylesheet" type="text/css" th:href="@{/styl.css}" href="../static/styl.css">
</head>
<body>
<h1>Historia działań kalkulatora</h1>
<ul>
<li th:each="element: ${historia}" th:text="${element}">2 + 2 = 4</li>
</ul>
<p><a th:href="@{/kalkulator}">wróć do kalkulatora</a></p>
</body>
</html>
...@@ -29,5 +29,7 @@ https://www.baeldung.com/thymeleaf-select-option ...@@ -29,5 +29,7 @@ https://www.baeldung.com/thymeleaf-select-option
[[${param.liczba1}]] [[${param.operacja}]] [[${param.liczba2}]] = <strong>[[${wynik}]]</strong> [[${param.liczba1}]] [[${param.operacja}]] [[${param.liczba2}]] = <strong>[[${wynik}]]</strong>
</div> </div>
<div><a th:href="@{/kalkulator/historia}">historia działań</a></div>
</body> </body>
</html> </html>
\ No newline at end of file
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