Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
2
20240528-BJava
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
20240528-BJava
Commits
a6cd2156
Commit
a6cd2156
authored
Jun 26, 2024
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Time controller - etap pośredni
parent
a82b1902
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
78 additions
and
1 deletions
+78
-1
RootController.java
PC32-Spring/src/main/java/alx/aplikacja/RootController.java
+1
-1
TimeController.java
PC32-Spring/src/main/java/alx/aplikacja/TimeController.java
+65
-0
wyswietl_czas5.html
PC32-Spring/src/main/resources/templates/wyswietl_czas5.html
+12
-0
No files found.
PC32-Spring/src/main/java/alx/aplikacja/RootController.java
View file @
a6cd2156
...
...
@@ -20,7 +20,7 @@ public class RootController {
return
"Hello World!"
;
}
//
d
opisz metodę, która zwraca bieżący czas, pod innym adresem, np. /czas
//
D
opisz metodę, która zwraca bieżący czas, pod innym adresem, np. /czas
@GetMapping
(
"/czas"
)
@ResponseBody
public
LocalDateTime
czas
()
{
...
...
PC32-Spring/src/main/java/alx/aplikacja/TimeController.java
0 → 100644
View file @
a6cd2156
package
alx
.
aplikacja
;
import
org.springframework.stereotype.Controller
;
import
org.springframework.web.bind.annotation.GetMapping
;
import
org.springframework.web.bind.annotation.ResponseBody
;
import
java.time.LocalDateTime
;
import
java.time.LocalTime
;
import
java.time.format.DateTimeFormatter
;
import
java.util.Locale
;
@Controller
public
class
TimeController
{
@GetMapping
(
"/time1"
)
@ResponseBody
public
LocalTime
time1
()
{
// Gdy metoda zwraca jakiś obiekt (klasy innej niż String i niektóre inne "magiczne" klasy samego Springa),
// a metoda posiada adnotację @ResponseBody, to dane odsyłane są w odpowiedzi w formacie JSON.
return
LocalTime
.
now
();
}
@GetMapping
(
"/time2"
)
@ResponseBody
public
String
time2
()
{
// Gdy metoda zwraca String i posiada adnotację @ResponseBody, to dane odsyłane są w odpowiedzi jako HTML.
// (tzn. są oznaczone jako text/html, ale nie są automatycznie formatowane).
return
LocalTime
.
now
().
toString
();
}
private
static
final
DateTimeFormatter
FORMAT_DATY
=
DateTimeFormatter
.
ofPattern
(
"EEEE, dd.MM.yyyy, 'godzina' HH:mm:ss"
,
Locale
.
of
(
"pl"
,
"PL"
));
// Aby powiedzieć wprost, jaki jest format (content-type) odpowiedzi zwracanej przez metodę, można użyć parametru produces
@GetMapping
(
path
=
"/time3"
,
produces
=
"text/plain"
)
@ResponseBody
public
String
time3
()
{
return
LocalDateTime
.
now
().
format
(
FORMAT_DATY
);
}
// Jak wysłać w odpowiedzi HTML?
// 1. Utworzyć bezpośrednio w kodzie Javy... - słabe
@GetMapping
(
path
=
"/time4"
,
produces
=
"text/html"
)
@ResponseBody
public
String
time4
()
{
LocalDateTime
dt
=
LocalDateTime
.
now
();
return
String
.
format
(
"""
<html><body>
<h1>Data i czas</h1>
<p>Teraz jest godzina <strong style='color:purple'>%s</strong></p>
<p>Dzisiejsza data: <strong style='color:blue'>%s</strong></p>
<p style='color: green'>%s</p>
</body></html>"""
,
dt
.
toLocalTime
(),
dt
.
toLocalDate
(),
dt
.
format
(
FORMAT_DATY
));
}
@GetMapping
(
"/time5"
)
public
String
time5
()
{
LocalDateTime
dt
=
LocalDateTime
.
now
();
System
.
out
.
println
(
"tutaj jestem"
);
return
"wyswietl_czas5.html"
;
}
}
PC32-Spring/src/main/resources/templates/wyswietl_czas5.html
0 → 100644
View file @
a6cd2156
<!DOCTYPE html>
<html
lang=
"pl"
>
<head>
<meta
charset=
"UTF-8"
>
<title>
Informacja o czasie
</title>
</head>
<body>
<h1>
Wyświetl czas 5
</h1>
<p>
Zaraz napiszę, która godzina.
</p>
</body>
</html>
\ No newline at end of file
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