Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
2
20230403
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
20230403
Commits
b31b0eb0
Commit
b31b0eb0
authored
Apr 24, 2023
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Data i czas - na razie bez szablonu
parent
7f0244b0
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
66 additions
and
0 deletions
+66
-0
TimeController.java
...Spring/src/main/java/com/example/demo/TimeController.java
+56
-0
index.html
PC26-Spring/src/main/resources/templates/index.html
+10
-0
No files found.
PC26-Spring/src/main/java/com/example/demo/TimeController.java
0 → 100644
View file @
b31b0eb0
package
com
.
example
.
demo
;
import
java.time.LocalDateTime
;
import
java.time.LocalTime
;
import
java.time.format.DateTimeFormatter
;
import
java.util.Locale
;
import
org.springframework.stereotype.Controller
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.ResponseBody
;
@Controller
public
class
TimeController
{
@RequestMapping
(
"/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
();
}
@RequestMapping
(
"/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"
,
new
Locale
(
"pl"
,
"PL"
));
// Aby powiedzieć wprost, jaki jest format (content-type) odpowiedzi zwracanej przez metdę, można uzyć parametru produces
@RequestMapping
(
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
@RequestMapping
(
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
));
}
}
PC26-Spring/src/main/resources/templates/index.html
View file @
b31b0eb0
...
@@ -11,7 +11,17 @@
...
@@ -11,7 +11,17 @@
<h2>
Przykłady pisane bezpośrednio w Controllerze
</h2>
<h2>
Przykłady pisane bezpośrednio w Controllerze
</h2>
<ul>
<ul>
<li><a
href=
"/hello"
>
Hello
</a></li>
<li><a
href=
"/hello"
>
Hello
</a></li>
<li><a
href=
"/time1"
>
Czas obiektowo
</a>
(json)
</li>
<li><a
href=
"/time2"
>
Czas tekstowo
</a></li>
<li><a
href=
"/time3"
>
Czas tekstowo
</a>
(text/plain)
</li>
<li><a
href=
"/time4"
>
Czas HTML w Javie
</a></li>
</ul>
</ul>
<h2>
Przykłady z szablonami
</h2>
<ul>
<li><a
href=
"/time5"
>
Czas szablon prosty
</a></li>
<li><a
href=
"/time6"
>
Czas szablon rozbudowany
</a></li>
</ul>
</body>
</body>
</html>
</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