parent
6235e5ca60
commit
ae0a87b53d
@ -0,0 +1,43 @@
|
|||||||
|
package wanijo.wanijo2.http.controller
|
||||||
|
|
||||||
|
import jakarta.validation.Valid
|
||||||
|
import org.springframework.stereotype.Controller
|
||||||
|
import org.springframework.ui.Model
|
||||||
|
import org.springframework.ui.set
|
||||||
|
import org.springframework.validation.BindingResult
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping
|
||||||
|
import wanijo.wanijo2.domain.TagDao
|
||||||
|
import wanijo.wanijo2.http.form.NewForm
|
||||||
|
|
||||||
|
@Controller
|
||||||
|
@RequestMapping("/document")
|
||||||
|
class NewController(
|
||||||
|
private val tagDao: TagDao
|
||||||
|
) {
|
||||||
|
|
||||||
|
@GetMapping("/new")
|
||||||
|
fun newDocument(
|
||||||
|
model: Model
|
||||||
|
): String {
|
||||||
|
model["tags"] = tagDao.findAll()
|
||||||
|
model["form"] = NewForm()
|
||||||
|
return "new"
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping("/new")
|
||||||
|
fun saveNewDocument(
|
||||||
|
model: Model,
|
||||||
|
@Valid
|
||||||
|
newForm: NewForm,
|
||||||
|
bindingResult: BindingResult
|
||||||
|
): String {
|
||||||
|
if (bindingResult.hasErrors()) {
|
||||||
|
return newDocument(model)
|
||||||
|
}
|
||||||
|
|
||||||
|
return "redirect:/"
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,10 @@
|
|||||||
|
package wanijo.wanijo2.http.form
|
||||||
|
|
||||||
|
import jakarta.validation.constraints.NotEmpty
|
||||||
|
|
||||||
|
data class NewForm (
|
||||||
|
@NotEmpty
|
||||||
|
val name: String = "",
|
||||||
|
val description: String = "",
|
||||||
|
val tagIds: List<Long> = emptyList()
|
||||||
|
)
|
@ -0,0 +1,29 @@
|
|||||||
|
<!doctype html>
|
||||||
|
<html xmlns:th="http://www.thymeleaf.org"
|
||||||
|
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
|
||||||
|
layout:decorate="~{base}"
|
||||||
|
lang="">
|
||||||
|
<head>
|
||||||
|
<title>Home</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<main id="content" layout:fragment="content">
|
||||||
|
<form th:action="@{/document/new}" th:object="${form}" method="post">
|
||||||
|
<label for="name">name</label>
|
||||||
|
<input id="name" type="text" th:field="*{name}" required>
|
||||||
|
|
||||||
|
<label for="description">beschreibung</label>
|
||||||
|
<textarea id="description" th:field="*{description}"></textarea>
|
||||||
|
|
||||||
|
<label for="tags">tags</label>
|
||||||
|
<select multiple th:field="*{tagIds}" id="tags">
|
||||||
|
<option th:each="tag: ${tags}" th:value="${tag.id}" th:text="${tag.name}"></option>
|
||||||
|
</select>
|
||||||
|
|
||||||
|
<button type="submit">
|
||||||
|
anlegen
|
||||||
|
</button>
|
||||||
|
</form>
|
||||||
|
</main>
|
||||||
|
</body>
|
||||||
|
</html>
|
Loading…
Reference in new issue