Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
J
java_dzienna_15_09
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_dzienna_15_09
Commits
7dbee03e
Commit
7dbee03e
authored
Sep 30, 2022
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
spis treści itp
parent
32bc9dae
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
96 additions
and
1 deletions
+96
-1
HelloController.java
...pring/src/main/java/com/example/demo/HelloController.java
+34
-1
index.html
PC29-Spring/src/main/resources/templates/index.html
+46
-0
ping.html
PC29-Spring/src/main/resources/templates/ping.html
+16
-0
No files found.
PC29-Spring/src/main/java/com/example/demo/HelloController.java
View file @
7dbee03e
package
com
.
example
.
demo
;
import
java.time.LocalTime
;
import
java.util.Map
;
import
javax.servlet.http.HttpServletRequest
;
import
org.springframework.stereotype.Controller
;
import
org.springframework.ui.Model
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.ResponseBody
;
/* Controller to jest klasa, która decyduje o tym, co ma się stać,
* gdy na serwer przyjdzie zapytanie kierowane pod określone adres.
* W projekcie może być wiele kontrolerów, a w klasie kontrolera może być wiele metod.
*/
*/
@Controller
public
class
HelloController
{
@RequestMapping
({
"/"
,
"index"
,
"index.html"
})
public
String
index
()
{
return
"index.html"
;
}
@RequestMapping
(
"/hello"
)
@ResponseBody
...
...
@@ -33,5 +41,30 @@ public class HelloController {
public
String
ktoraGodzina
()
{
return
"Jest godzina "
+
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"
;
}
}
PC29-Spring/src/main/resources/templates/index.html
0 → 100644
View file @
7dbee03e
<!DOCTYPE html>
<html
xmlns:th=
"http://www.thymeleaf.org"
>
<head>
<meta
charset=
"UTF-8"
>
<title>
Spis treści
</title>
<link
rel=
"stylesheet"
type=
"text/css"
th:href=
"@{/styl.css}"
>
</head>
<body>
<h1>
Spis treści
</h1>
<h2>
Przykłady pisane bezpośrednio w Controllerze
</h2>
<ul>
<li><a
th:href=
"@{/hello}"
>
Hello
</a></li>
<li><a
th:href=
"@{/ping}"
>
Ping
</a></li>
<li><a
th:href=
"@{/time1}"
>
Czas obiektowo
</a>
(json)
</li>
<li><a
th:href=
"@{/time2}"
>
Czas tekstowo
</a></li>
<li><a
th:href=
"@{/time3}"
>
Czas tekstowo
</a>
(text/plain)
</li>
<li><a
th:href=
"@{/time4}"
>
Czas HTML w Javie
</a></li>
</ul>
<h2>
Przykłady z szablonami
</h2>
<ul>
<li><a
th:href=
"@{/time5}"
>
Czas szablon prosty
</a></li>
<li><a
th:href=
"@{/time6}"
>
Czas szablon rozbudowany
</a></li>
</ul>
<h2>
Przykład Parametry
</h2>
<ul>
<li><a
th:href=
"@{/parametry/witaj}"
>
witaj
</a></li>
<li><a
th:href=
"@{/parametry/witaj(imie='Ala')}"
>
witaj?imie=Ala
</a></li>
<li><a
th:href=
"@{/parametry/powtorz}"
>
powtorz
</a></li>
<li><a
th:href=
"@{/parametry/powtorz(tekst='Ola ma psa')}"
>
powtorz
</a>
n = 1
</li>
<li><a
th:href=
"@{/parametry/powtorz(tekst='Ala ma kota',n=10)}"
>
powtorz
</a>
n = 10
</li>
</ul>
<h2>
Przykłady formularzy
</h2>
<ul>
<li><a
th:href=
"@{/kalkulator}"
>
Kalkulator
</a>
(formlularz)
</li>
<li><a
th:href=
"@{/kalkulator/historia}"
>
Historia Kalkulatora HTML
</a></li>
<li><a
th:href=
"@{/historia.json}"
>
Historia Kalkulatora JSON
</a></li>
</ul>
</body>
</html>
PC29-Spring/src/main/resources/templates/ping.html
0 → 100644
View file @
7dbee03e
<!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