Commit f68d8e1c by Patryk Czarnik

blog: dodawnaie tekstów do listy jako pole prywatne w kontrolerze

parent f770f388
package com.example.demo; package com.example.demo;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model; import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
...@@ -9,6 +12,7 @@ import org.springframework.web.bind.annotation.RequestMapping; ...@@ -9,6 +12,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
@Controller @Controller
@RequestMapping("/blog") @RequestMapping("/blog")
public class BlogController { public class BlogController {
private List<String> teksty = new ArrayList<>();
@GetMapping @GetMapping
public String rozmowaGet() { public String rozmowaGet() {
...@@ -18,8 +22,9 @@ public class BlogController { ...@@ -18,8 +22,9 @@ public class BlogController {
@PostMapping @PostMapping
public String rozmowaPost(String tekst, Model model) { public String rozmowaPost(String tekst, Model model) {
if(tekst != null && !tekst.isBlank()) { if(tekst != null && !tekst.isBlank()) {
teksty.add(tekst);
} }
model.addAttribute("teksty", teksty);
return "blog.html"; return "blog.html";
} }
......
...@@ -16,7 +16,7 @@ ...@@ -16,7 +16,7 @@
<section> <section>
<h2>Zapamiętane teksty:</h2> <h2>Zapamiętane teksty:</h2>
<ul> <ul>
<li>przykładowy tekst</li> <li th:each="tekst : ${teksty}" th:text="${tekst}">przykładowy tekst</li>
</ul> </ul>
</section> </section>
......
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