Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
J
java_weekendowa_20221008
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_weekendowa_20221008
Commits
c854b308
Commit
c854b308
authored
Nov 13, 2022
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Uzupełnienia: ping
parent
50f96897
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
45 additions
and
2 deletions
+45
-2
PierwszyKontroler.java
...ing/src/main/java/com/example/demo/PierwszyKontroler.java
+29
-2
ping.html
PC26-Spring/src/main/resources/templates/ping.html
+16
-0
No files found.
PC26-Spring/src/main/java/com/example/demo/PierwszyKontroler.java
View file @
c854b308
package
com
.
example
.
demo
;
package
com
.
example
.
demo
;
import
java.time.LocalTime
;
import
java.time.LocalTime
;
import
java.util.Map
;
import
javax.servlet.http.HttpServletRequest
;
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
;
import
org.springframework.web.bind.annotation.ResponseBody
;
import
org.springframework.web.bind.annotation.ResponseBody
;
...
@@ -14,8 +18,6 @@ public class PierwszyKontroler {
...
@@ -14,8 +18,6 @@ public class PierwszyKontroler {
return
"Hello world - jestem Spring"
;
return
"Hello world - jestem Spring"
;
}
}
// TODO dopisz tu metodę, która wypisuje bieżący czas
// LocalTime.now()
@RequestMapping
(
"/godzina"
)
@RequestMapping
(
"/godzina"
)
@ResponseBody
@ResponseBody
public
String
ktoraGodzina
()
{
public
String
ktoraGodzina
()
{
...
@@ -23,4 +25,29 @@ public class PierwszyKontroler {
...
@@ -23,4 +25,29 @@ public class PierwszyKontroler {
// return LocalTime.now();
// return LocalTime.now();
}
}
@RequestMapping
(
"/ping"
)
public
String
ping
(
HttpServletRequest
request
,
Model
model
)
{
// Do metod w kontrolerze można dodawać parametry różnych typów,
// które "Spring zna" i gdy taki parametr się pojawia, to Spring
// wywołując metodę przekazuje nam w tym parametrze odpowiedni obiekt.
// To się nazywa wstrzykiwanie parametrów / parameter injection.
// A ogólnie to podejście, że nie my wywołuemy metodę, aby coś odczytać,
// tylko spring wywołuje NASZĄ metodę i Spring NAM przekazuje wymagane parametry,
// nazywa się "inversion of control" (IoC).
// Przykładowo tutaj dodaliśmy do metody parametr HttpServletRequest
// i Spring przekazuje nam tu techniczne informacje o zapytaniu (to samo, co
// było w serwletach).
// Do modelu można też dodać słownik / mapę.
// Odczyt wartości wygląda później np. tak ${clientInfo.userAgent}
String
ip
=
request
.
getRemoteAddr
();
String
host
=
request
.
getRemoteHost
();
System
.
out
.
println
(
"Zapytanie z adresu "
+
ip
);
model
.
addAttribute
(
"clientInfo"
,
Map
.
of
(
"userAgent"
,
request
.
getHeader
(
"User-Agent"
),
"ip"
,
ip
,
"host"
,
host
));
return
"ping.html"
;
}
}
}
PC26-Spring/src/main/resources/templates/ping.html
0 → 100644
View file @
c854b308
<!DOCTYPE html>
<html
lang=
"pl"
xmlns:th=
"http://www.thymeleaf.org"
>
<head>
<meta
charset=
"UTF-8"
>
<title>
Ping
</title>
<link
rel=
"stylesheet"
type=
"text/css"
th:href=
"@{/styl.css}"
href=
"../static/styl.css"
>
</head>
<body>
<h2>
Informacje o kliencie
</h2>
<ul>
<li>
Adres:
<strong
th:text=
"${clientInfo.ip}"
>
1.2.3.4
</strong>
(
<span
th:text=
"${clientInfo.host}"
>
abc.com
</span>
)
</li>
<li>
Przeglądarka:
<strong
th:text=
"${clientInfo.userAgent}"
>
IE
</strong></li>
</ul>
</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