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
03bc8d35
Commit
03bc8d35
authored
Jun 26, 2024
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Przykłady nieco bardziej techniczne
parent
10a32d5e
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
106 additions
and
0 deletions
+106
-0
ParametryController.java
...ring/src/main/java/alx/aplikacja/ParametryController.java
+61
-0
RootController.java
PC32-Spring/src/main/java/alx/aplikacja/RootController.java
+29
-0
ping.html
PC32-Spring/src/main/resources/templates/ping.html
+16
-0
No files found.
PC32-Spring/src/main/java/alx/aplikacja/ParametryController.java
0 → 100644
View file @
03bc8d35
package
alx
.
aplikacja
;
import
org.springframework.stereotype.Controller
;
import
org.springframework.web.bind.annotation.GetMapping
;
import
org.springframework.web.bind.annotation.RequestHeader
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RequestParam
;
import
org.springframework.web.bind.annotation.ResponseBody
;
import
jakarta.servlet.http.HttpServletRequest
;
@Controller
@RequestMapping
(
"/parametry"
)
public
class
ParametryController
{
// Najprostsze podejście, aby odczytać wartość parametru zapytania:
// zadeklarować parametr o takiej samej nazwie w metodzie kontrolera.
// "Spring nam to przekaże".
// Podejście polegające na tym, że framework wywołuje "naszą metodę",
// a gdy my czegoś potrzebujemy, to dodajemy odp. parametr do tej metody,
// nazywa się "inversion of control (IoC)".
@GetMapping
(
"/witaj"
)
@ResponseBody
public
String
witaj
(
String
imie
)
{
return
"Witaj "
+
imie
;
}
// Dzięki adnotacji @RequestParam mamy większą kontrolę nad szczegółami parametru.
// Można m.in. podać inną nazwę niż nazw zmiennej w Javie, może podać wartość domyślną.
// http://localhost:8080/parametry/powtorz?tekst=Ala%20ma%20kota&n=10
@GetMapping
(
path
=
"/powtorz"
,
produces
=
"text/plain;charset=utf-8"
)
@ResponseBody
public
String
powtorz
(
@RequestParam
(
defaultValue
=
""
)
String
tekst
,
@RequestParam
(
name
=
"n"
,
defaultValue
=
"1"
)
int
ileRazy
)
{
return
(
tekst
+
"\n"
).
repeat
(
ileRazy
);
}
// Parametry bez adnotacji są opcjonalne - w razie braku Spring wywoła metodę i wstawi nulla.
// Parametry z adnotacją @RequestParam są domyślnie obowiązkowe. Aby był opcjonalny,
// trzeba wpisać required=false (wtedy brak parametru = null)
// lub podać defaultValue.
// Wpisując do własnych metod parametry specjalnych typów, albo używając specjalnych adnotacji,
// możemy uzyskać dostęp do różnych klas narzędziowych serwera lub informacji związanych z zapytaniem.
@GetMapping
(
path
=
"/techniczne"
,
produces
=
"text/plain;charset=UTF-8"
)
@ResponseBody
public
String
parametryTechniczne
(
HttpServletRequest
request
,
@RequestHeader
(
"User-Agent"
)
String
agent
)
{
return
"""
adres IP klienta: %s:%d
adres serwera: %s:%d
User-Agent z requestu: %s
User-Agent z parametru: %s
"""
.
formatted
(
request
.
getRemoteAddr
(),
request
.
getRemotePort
(),
request
.
getLocalAddr
(),
request
.
getLocalPort
(),
request
.
getHeader
(
"User-Agent"
),
agent
);
}
}
PC32-Spring/src/main/java/alx/aplikacja/RootController.java
View file @
03bc8d35
package
alx
.
aplikacja
;
import
jakarta.servlet.http.HttpServletRequest
;
import
org.springframework.stereotype.Controller
;
import
org.springframework.ui.Model
;
import
org.springframework.web.bind.annotation.GetMapping
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.ResponseBody
;
import
java.time.LocalDateTime
;
import
java.util.Map
;
/* Klasa typu Controller decyduje, w jaki sposób będą obsługiwane zapytania przychodzące na serwer.
* W projekcie może być wiele kontrolerów, a każdy kontroler może mieć wiele metod obsługujących zapytania.
...
...
@@ -31,4 +35,29 @@ public class RootController {
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"
;
}
}
PC32-Spring/src/main/resources/templates/ping.html
0 → 100644
View file @
03bc8d35
<!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