Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
A
alx_java2b_20250412
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
alx_java2b_20250412
Commits
4a18f33d
Commit
4a18f33d
authored
May 25, 2025
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
ParametryController
parent
2114d7ad
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
69 additions
and
2 deletions
+69
-2
ParametryController.java
...g/src/main/java/org/example/demo/ParametryController.java
+63
-0
RootController.java
...Spring/src/main/java/org/example/demo/RootController.java
+4
-1
szablon_czasu_6.html
...-Spring/src/main/resources/templates/szablon_czasu_6.html
+2
-1
No files found.
PC35-Spring/src/main/java/org/example/demo/ParametryController.java
0 → 100644
View file @
4a18f33d
package
org
.
example
.
demo
;
import
org.springframework.stereotype.Controller
;
import
org.springframework.web.bind.annotation.*
;
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.
@GetMapping
(
"/path/{a}/{b}"
)
@ResponseBody
public
String
parametrPath
(
@PathVariable
String
a
,
@PathVariable
(
"b"
)
String
drugi
)
{
return
"a = "
+
a
+
", b = "
+
drugi
;
}
// 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
);
}
}
PC35-Spring/src/main/java/org/example/demo/RootController.java
View file @
4a18f33d
...
@@ -52,7 +52,10 @@ public class RootController {
...
@@ -52,7 +52,10 @@ public class RootController {
String
host
=
request
.
getRemoteHost
();
String
host
=
request
.
getRemoteHost
();
System
.
out
.
println
(
"Zapytanie z adresu "
+
ip
);
System
.
out
.
println
(
"Zapytanie z adresu "
+
ip
);
model
.
addAttribute
(
"clientInfo"
,
Map
.
of
(
"userAgent"
,
request
.
getHeader
(
"User-Agent"
),
"ip"
,
ip
,
"host"
,
host
));
model
.
addAttribute
(
"clientInfo"
,
Map
.
of
(
"userAgent"
,
request
.
getHeader
(
"User-Agent"
),
"ip"
,
ip
,
"host"
,
host
));
return
"ping.html"
;
return
"ping.html"
;
}
}
...
...
PC35-Spring/src/main/resources/templates/szablon_czasu_6.html
View file @
4a18f33d
...
@@ -32,6 +32,7 @@
...
@@ -32,6 +32,7 @@
</ul>
</ul>
<h3>
Odwołania do elementów zagnieżdżonych
</h3>
<h3>
Odwołania do elementów zagnieżdżonych
</h3>
<ul>
<ul>
<!-- tak naprawdę wywołanie dt.getYear() -->
<!-- tak naprawdę wywołanie dt.getYear() -->
<li>
rok:
<strong
th:text=
"${dt.year}"
>
1997
</strong></li>
<li>
rok:
<strong
th:text=
"${dt.year}"
>
1997
</strong></li>
...
@@ -41,7 +42,7 @@
...
@@ -41,7 +42,7 @@
<!-- Można też wywołać metodę, która coś odczyta, nawet jeśli nie nazywa się getXXX() -->
<!-- Można też wywołać metodę, która coś odczyta, nawet jeśli nie nazywa się getXXX() -->
<li>
data:
<strong
th:text=
"${dt.toLocalDate()}"
>
2020-04-05
</strong></li>
<li>
data:
<strong
th:text=
"${dt.toLocalDate()}"
>
2020-04-05
</strong></li>
<li>
czas:
<strong
th:text=
"${dt.toLocalTime()}"
>
10:44:22
</strong></li>
<li>
czas:
<strong
th:text=
"${dt.toLocalTime()}"
th:class=
"${klasa}"
>
10:44:22
</strong></li>
<li>
miesiąc:
<em
th:text=
"${dt.month}"
>
APRIL
</em></li>
<li>
miesiąc:
<em
th:text=
"${dt.month}"
>
APRIL
</em></li>
</ul>
</ul>
...
...
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