Commit 89061dae by Patryk Czarnik

Spis treści i styl

parent 7eaaf33a
package alx; package alx;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.ResponseBody;
import java.util.Map;
@Controller @Controller
public class RootController { public class RootController {
@GetMapping("/") @GetMapping("/")
@ResponseBody
public String root() { public String root() {
return "<h1>Aplikacja Springowa</h1>"; return "index.html";
} }
@GetMapping("/hello") @GetMapping("/hello")
...@@ -17,4 +21,29 @@ public class RootController { ...@@ -17,4 +21,29 @@ public class RootController {
public String hello() { public String hello() {
return "HEllo Spring"; return "HEllo Spring";
} }
@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";
}
} }
body {
background-color: #FFFFCC;
font-family: 'Arial', sans-serif;
}
h1 {
color: green;
text-align: center;
}
form {
margin: 30px auto;
padding: 20px;
width: 800px;
border: 4px solid blue;
background-color: #AAEEFF;
}
.wynik {
background-color: #FFFFFF;
border: 3px solid green;
margin: 20px auto;
width: 800px;
padding: 10px;
color: green;
}
.error {
background-color: #FFFFFF;
border: 6px double red;
margin: 20px auto;
padding: 10px;
width: 800px;
color: red;
font-weight: bold;
}
<!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="@{/moj_kalkulator}">Prosty Kalkulator</a> - wersja z zajęć</li>
<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>
<li><a th:href="@{/historia.txt}">Historia tekstowo</a></li>
<li><a th:href="@{liczenie(liczba1=211,liczba2=303,operacja='*')}">Liczenie bezpośrednie</a> w parametrami URL</li>
</ul>
</body>
</html>
<!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>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment