Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
J
java_dzienna_15_09
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Patryk Czarnik
java_dzienna_15_09
Commits
7f38e98c
Commit
7f38e98c
authored
Oct 04, 2022
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Zapamiętywanie i wyświetlanie historii działań
parent
142a0a58
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
39 additions
and
1 deletions
+39
-1
Kalkulator.java
PC29-Spring/src/main/java/com/example/demo/Kalkulator.java
+8
-1
LogikaKalkulatora.java
...ing/src/main/java/com/example/demo/LogikaKalkulatora.java
+13
-0
historia.html
PC29-Spring/src/main/resources/templates/historia.html
+15
-0
kalkulator.html
PC29-Spring/src/main/resources/templates/kalkulator.html
+3
-0
No files found.
PC29-Spring/src/main/java/com/example/demo/Kalkulator.java
View file @
7f38e98c
...
...
@@ -9,6 +9,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping
(
"/kalkulator"
)
public
class
Kalkulator
{
private
LogikaKalkulatora
logikaKalkulatora
=
new
LogikaKalkulatora
();
@GetMapping
public
String
kalkulatorGet
()
{
...
...
@@ -19,11 +20,17 @@ public class Kalkulator {
// do tej metody - wystarczy zadeklarować parametry o tej samej nazwie.
@PostMapping
public
String
kalkulatorPost
(
Model
model
,
Long
liczba1
,
Long
liczba2
,
String
operacja
)
{
LogikaKalkulatora
logikaKalkulatora
=
new
LogikaKalkulatora
();
long
wynik
=
logikaKalkulatora
.
oblicz
(
operacja
,
liczba1
,
liczba2
);
model
.
addAttribute
(
"wynik"
,
wynik
);
return
"kalkulator.html"
;
}
// Ta metoda zadziała, gdy ktoś wejdzie pod adres /kalkulator/historia
@GetMapping
(
"/historia"
)
public
String
pokazHistorie
(
Model
model
)
{
model
.
addAttribute
(
"historia"
,
logikaKalkulatora
.
getHistoriaDzialan
());
return
"historia.html"
;
}
}
PC29-Spring/src/main/java/com/example/demo/LogikaKalkulatora.java
View file @
7f38e98c
package
com
.
example
.
demo
;
import
java.util.ArrayList
;
import
java.util.Collections
;
import
java.util.List
;
public
class
LogikaKalkulatora
{
// Ponieważ ten sam obiekt listy będzie używany w różnych zapytaniach, to może być używany przez różne wątki.
// dlatego tworzymy listę "synchronizowaną", żeby np. równoległe operacje add sobie nie przeszkadzały.
private
final
List
<
String
>
historiaDzialan
=
Collections
.
synchronizedList
(
new
ArrayList
<>());
public
long
oblicz
(
String
operacja
,
long
liczba1
,
long
liczba2
)
{
long
wynik
=
switch
(
operacja
)
{
...
...
@@ -11,6 +18,12 @@ public class LogikaKalkulatora {
case
"%"
->
liczba1
%
liczba2
;
default
->
0
;
};
historiaDzialan
.
add
(
String
.
format
(
"%d %s %d = %s"
,
liczba1
,
operacja
,
liczba2
,
wynik
));
return
wynik
;
}
public
List
<
String
>
getHistoriaDzialan
()
{
return
Collections
.
unmodifiableList
(
historiaDzialan
);
// dla zwiększenia bezpieczeństwa wątkowego, można by też zwrócić kopię
}
}
PC29-Spring/src/main/resources/templates/historia.html
0 → 100644
View file @
7f38e98c
<!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>
PC29-Spring/src/main/resources/templates/kalkulator.html
View file @
7f38e98c
...
...
@@ -35,5 +35,8 @@
Straszny błąd!
</div>
<div><a
href=
"historia"
th:href=
"@{/kalkulator/historia}"
>
zobacz historię działań
</a></div>
<div><a
href=
"historia.json"
th:href=
"@{/historia.json}"
>
historia działań JSON
</a></div>
</body>
</html>
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment