try adding thymeleaf

This commit is contained in:
luxic
2025-08-18 08:50:08 +07:00
parent a128eb6cca
commit 59da25207e
5 changed files with 88 additions and 1 deletions

View File

@@ -0,0 +1,32 @@
package id.my.luxic.pastebin.controller;
import id.my.luxic.pastebin.model.Paste;
import id.my.luxic.pastebin.service.PasteService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
@Controller
public class HomeController {
private final PasteService service;
public HomeController(PasteService service) {
this.service = service;
}
@GetMapping("/")
public String home(Model model) {
model.addAttribute("message", "Hello, Thymeleaf!");
return "index";
}
@GetMapping("/paste/{id}")
public String paste(Model model, @PathVariable String id) {
Paste paste = service.findById(id);
model.addAttribute("paste", paste);
return "paste";
}
}

View File

@@ -5,3 +5,5 @@ spring.datasource.username=postgres
spring.datasource.password=password
spring.jpa.hibernate.ddl-auto=update
spring.thymeleaf.cache=false
spring.devtools.livereload.enabled=true

View File

@@ -0,0 +1,30 @@
<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml">
<head>
...
</head>
<body>
<div class="navbar navbar-inverse navbar-fixed-top" th:fragment="header">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".nav-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">My project</a>
</div>
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li th:classappend="${module == 'index' ? 'active' : ''}">
<a href="#" th:href="@{/}">Index</a>
</li>
<li th:classappend="${module == 'paste' ? 'active' : ''}">
<a href="#" th:href="@{/paste/1wMoxOx8it}">Paste</a>
</li>
</ul>
</div>
</div>
</div>
</body>
</html>

View File

@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" lang="en">
<head>
<title>Thymeleaf Example</title>
</head>
<body>
<h1>Welcome to Thymeleaf!</h1>
<p th:text="${message}">This is a message</p>
</body>
</html>

View File

@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" lang="en">
<head>
<title>Thymeleaf Example</title>
</head>
<body>
<div th:replace="fragments/header :: header">
</div>
<h1>Welcome to Thymeleaf!</h1>
<p th:text="${paste.content}">This is a message</p>
</body>
</html>