Commit 10a32d5e by Patryk Czarnik

Przykład blog

parent 1cf2e1b0
package alx.aplikacja;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/blog")
public class BlogController {
@Autowired
private BlogService blogService;
@GetMapping
public String rozmowaGet() {
return "blog.html";
}
@PostMapping
public String rozmowaPost(String tekst, Model model) {
if(tekst != null && !tekst.isBlank()) {
blogService.addTekst(tekst);
}
model.addAttribute("teksty", blogService.getTeksty());
return "blog.html";
}
}
package alx.aplikacja;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/blog.json")
public class BlogRestController {
@Autowired
private BlogService blogService;
@GetMapping
public List<String> getTeksty() {
return blogService.getTeksty();
}
@GetMapping(path="/{numer}", produces="text/plain")
public String getTekst(@PathVariable("numer") int i) {
return blogService.getTekst(i);
}
@PutMapping(path="/{numer}", consumes="text/plain")
public void setTekst(
@PathVariable("numer") int i,
@RequestBody String tekst) {
blogService.setTekst(i, tekst);
}
@PostMapping(consumes="text/plain")
public void addTekst(@RequestBody String tekst) {
blogService.addTekst(tekst);
}
}
package alx.aplikacja;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.springframework.stereotype.Component;
@Component
public class BlogService {
private List<String> teksty = Collections.synchronizedList(new ArrayList<>());
public List<String> getTeksty() {
return Collections.unmodifiableList(teksty);
}
public String getTekst(int i) {
return teksty.get(i);
}
public void setTekst(int i, String tekst) {
teksty.set(i, tekst);
}
public void addTekst(String tekst) {
teksty.add(tekst);
}
}
<!DOCTYPE html>
<html lang="pl" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Blog</title>
<link rel="stylesheet" type="text/css" th:href="@{/styl.css}" href="../static/styl.css">
</head>
<body>
<h1>Blog - wersja webowa</h1>
<form method="post">
<label for="tekst">Napisz coś:</label><input id="tekst" name="tekst" type="text"><br>
<button>OK</button>
</form>
<section>
<h2>Zapamiętane teksty:</h2>
<ul>
<li th:each="tekst : ${teksty}" th:text="${tekst}">przykładowy tekst</li>
</ul>
</section>
</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