如何使用Kotlin和Spring Boot处理表单提交

huangapple go评论74阅读模式
英文:

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(&quot;/&quot;)
	fun home(model: Model): String {
		model[&quot;title&quot;] = &quot;Homepage&quot;
		model[&quot;greeting&quot;] =  Greeting()
		return &quot;home&quot;
	}

	@PostMapping(&quot;/&quot;)
	fun greetingSubmit(@ModelAttribute greeting: Greeting?, model: Model): String? {
		model[&quot;title&quot;] = &quot;greeting&quot;
		if (greeting != null) {
			model[&quot;greeting&quot;] = greeting
		}
		return &quot;result&quot;
	}

}


// Greeting class
class Greeting {
	var id: Long = 0
	var content: String? = null
}

Here are my HTML templates:

&lt;!-- result.mustache --&gt;
{{&gt; header}}

&lt;h1&gt;Result&lt;/h1&gt;
&lt;p th:text=&quot;&#39;id: &#39; + ${greeting.id}&quot; /&gt;
&lt;p th:text=&quot;&#39;content: &#39; + ${greeting.content}&quot; /&gt;
&lt;a href=&quot;/&quot;&gt;Submit another message&lt;/a&gt;

{{&gt; footer}}
&lt;!-- home.mustache --&gt;
{{&gt; header}}

&lt;h1&gt;Form&lt;/h1&gt;
&lt;form action=&quot;#&quot; th:action=&quot;@{/greeting}&quot; th:object=&quot;${greeting}&quot; method=&quot;post&quot;&gt;
    &lt;p&gt;Id: &lt;input type=&quot;text&quot; th:field=&quot;*{id}&quot;/&gt;&lt;/p&gt;
    &lt;p&gt;Message: &lt;input type=&quot;text&quot; th:field=&quot;*{content}&quot;/&gt;&lt;/p&gt;
    &lt;p&gt;&lt;input type=&quot;submit&quot; value=&quot;Submit&quot;/&gt; &lt;input type=&quot;reset&quot; value=&quot;Reset&quot;/&gt;&lt;/p&gt;
&lt;/form&gt;

{{&gt; footer}}

答案1

得分: 1

我也尝试将指南转换为Kotlin...结果如下:

https://github.com/xerx593/handlingformsubmission-kotlin

关键要点:

  • 控制器在语法上有误 (model[&quot;xyz&quot;]), 我们可以使用 model.addAttribute(...)model.asMap()[&quot;xyz&quot;] = ...
  • Get映射应该匹配请求 (+"submit another message") URL
  • Post映射应该匹配 th:action URL
  • 视图和文件名一致性 (+位置): 例如&quot;home&quot; -> 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[&quot;xyz&quot;]), we can use model.addAtribute(...) or model.asMap()[&quot;xyz&quot;] = ...
  • 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.&quot;home&quot; -> src/main/resources/templates/home.html
  • ...

huangapple
  • 本文由 发表于 2023年3月7日 02:01:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/75654282.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定