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
66c72a32
Commit
66c72a32
authored
Apr 24, 2023
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
przykład ping
parent
328bfa5a
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
44 additions
and
7 deletions
+44
-7
PierwszyController.java
...ng/src/main/java/com/example/demo/PierwszyController.java
+28
-7
ping.html
PC26-Spring/src/main/resources/templates/ping.html
+16
-0
No files found.
PC26-Spring/src/main/java/com/example/demo/PierwszyController.java
View file @
66c72a32
package
com
.
example
.
demo
;
import
java.
time.LocalDateTime
;
import
java.
util.Map
;
import
org.springframework.stereotype.Controller
;
import
org.springframework.ui.Model
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.ResponseBody
;
import
jakarta.servlet.http.HttpServletRequest
;
@Controller
public
class
PierwszyController
{
// Gdy metoda w kontrolerze zwraca napis, a nie osiada adnotacji @ResponseBody,
...
...
@@ -23,12 +26,30 @@ public class PierwszyController {
public
String
hello
()
{
return
"Hello world"
;
}
// Dodaj metodę wyświetlającą bieżący czas
@RequestMapping
(
"/czas"
)
@ResponseBody
public
LocalDateTime
czas
()
{
return
LocalDateTime
.
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 @
66c72a32
<!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