英文:
How to handle form submission using Kotlin and Spring Boot
问题
我正在尝试使用Spring框架在Kotlin中创建一个简单的表单,但当我点击提交时,它只在h1标签中显示"Result",没有其他内容。
这是我的代码:
@Controller
class HtmlController {
@GetMapping("/")
fun home(model: Model): String {
model["title"] = "Homepage"
model["greeting"] = Greeting()
return "home"
}
@PostMapping("/")
fun greetingSubmit(@ModelAttribute greeting: Greeting?, model: Model): String? {
model["title"] = "greeting"
if (greeting != null) {
model["greeting"] = greeting
}
return "result"
}
}
// Greeting class
class Greeting {
var id: Long = 0
var content: String? = null
}
这是我的HTML模板:
<!-- result.mustache -->
{{> header}}
<h1>Result</h1>
<p th:text="'id: ' + ${greeting.id}" />
<p th:text="'content: ' + ${greeting.content}" />
<a href="/">Submit another message</a>
{{> footer}}
<!-- home.mustache -->
{{> header}}
<h1>Form</h1>
<form action="#" th:action="@{/greeting}" th:object="${greeting}" method="post">
<p>Id: <input type="text" th:field="*{id}"/></p>
<p>Message: <input type="text" th:field="*{content}"/></p>
<p><input type="submit" value="Submit"/> <input type="reset" value="Reset"/></p>
</form>
{{> footer}}
英文:
I am trying to use the Spring framework to make a simple form in Kotlin but when I click on submit, it only displays Result in h1 tags and nothing else
Here is my code:
@Controller
class HtmlController {
@GetMapping("/")
fun home(model: Model): String {
model["title"] = "Homepage"
model["greeting"] = Greeting()
return "home"
}
@PostMapping("/")
fun greetingSubmit(@ModelAttribute greeting: Greeting?, model: Model): String? {
model["title"] = "greeting"
if (greeting != null) {
model["greeting"] = greeting
}
return "result"
}
}
// Greeting class
class Greeting {
var id: Long = 0
var content: String? = null
}
Here are my HTML templates:
<!-- result.mustache -->
{{> header}}
<h1>Result</h1>
<p th:text="'id: ' + ${greeting.id}" />
<p th:text="'content: ' + ${greeting.content}" />
<a href="/">Submit another message</a>
{{> footer}}
<!-- home.mustache -->
{{> header}}
<h1>Form</h1>
<form action="#" th:action="@{/greeting}" th:object="${greeting}" method="post">
<p>Id: <input type="text" th:field="*{id}"/></p>
<p>Message: <input type="text" th:field="*{content}"/></p>
<p><input type="submit" value="Submit"/> <input type="reset" value="Reset"/></p>
</form>
{{> footer}}
答案1
得分: 1
我也尝试将指南转换为Kotlin...结果如下:
https://github.com/xerx593/handlingformsubmission-kotlin
关键要点:
- 控制器在语法上有误 (
model["xyz"]
), 我们可以使用model.addAttribute(...)
或model.asMap()["xyz"] = ...
- Get映射应该匹配请求 (+"submit another message") URL
- Post映射应该匹配
th:action
URL - 视图和文件名一致性 (+位置): 例如
"home"
->src/main/resources/templates/home.html
- ...
英文:
I, too, tried to convert the guide to kotlin ... with following result:
https://github.com/xerx593/handlingformsubmission-kotlin
Key points:
- Controller is syntactically wrong (
model["xyz"]
), we can usemodel.addAtribute(...)
ormodel.asMap()["xyz"] = ...
- Get mapping should match the request (+"submit another message") url
- Post mapping should match the
th:action
url - Consistence of view and file name (+location): e.g.
"home"
->src/main/resources/templates/home.html
- ...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论