Web開発を行っているときは、フォームの提出から逃れることはできません。この記事では、Kotlin言語を使用してSpring Bootを介してフォームを作成および提出します。
以下に、以前の「Spring Boot and KotlinレンダリングWebビューをFreeMarker Template Engine」プロジェクトに基づいて、処理フォームの送信を追加します。
build.gradleファイルは変更されていません。これは完全なbuild.gradleです
グループ 'name.quanke.kotlin'version' 1.0-snapshot'buildscript {ext.kotlin_version = '1.2.10' ext.spring_boot_version = '1.5.4.Release' repositories {mavencentral()}依存関係{classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$ kotlin_version" classpath( "org.springframework.boot:spring-boot-gradle-plugin:$ spring_boot_version") classpath( "org.jetbrains.kotlin:kotlin-noarg:$ kotlin_version")classpath( "org.jetbrains.kotlin:kotlin-allopen:$ kotlin_version")}} fipplageプラグイン: 'kotlin'applyプラグイン: "kotlin-sspring" // see https://kotlinlang.org/docs/reference/compiler-plugins.html#kotlin-spring-compiler-pluginapplyプラグイン: 'org.springframework.boot'jar {basename =' baster11-5-4-service 'バージョン=' 0.1.0 'deposirisis() {compile "org.jetbrains.kotlin:kotlin-stdlib-jre8:$ kotlin_version" compile "org.springframework.boot:spring-boot-starter-web:$ spring_boot_version" compile "org.springframework.boot:spring-startarter--startertertartertartertertertartertertertertertertemelef:$ 「com.fasterxml.jackson.module:jackson-module-kotlin:$ kotlin_version "testcompile" org.springframework.boot:spring-boot-starter-test:$ spring_boot_version "testcompile" org.jetbrains.kotlin:kotlin-test-junit "kotlinion kotlinoptions.jvmtarget = "1.8"} cypiletestkotlin {kotlinoptions.jvmtarget = "1.8"}エンティティクラスの作成こんにちは
/*** 2018/1/12にhttp://quanke.nameによって作成されました。 */data class hello(var id:long?= 0、var content:string?= "")
コントローラーを作成します
name.quanke.kotlin.chaper11_5_4.entity.helloimport org.springframework.stereotype.controllerimport org.springframework.ui.modelmapimport org.springframework.web.bind.annotation.modelattributimport org.springframework.web.bind.annotation.postmappingimport org.springframework.web.bind.annotation.RequestMapping/***は、http://quanke.name on 2018/1/10によって作成されました。 */ @ControlerClass Hellocontroller {@RequestMapping( "/")Fun Index(Map:ModelMap):String {///bap.addattribute( "host"、 "http://quanke.name")map.addattribute( "hello())// src/main/resources/templates/index.html return "index"} @postmapping( "/hello")fun hellopostsubmit(@modelattribute hello:hello):string {return "result"}}}ページ表示レイヤー
src/main/resources/templates/index.html
<!doctype html> <html xmlns:th = "http://www.w3.org/1999/xhtml"> <head lang = "en"> <title> quanke.name </title> <meta http-equiv = "content-type" text = "/html; charset = utf-8 "/> </head> <body> <h1 th:text =" $ {host} "> hello world </h1> <h1> form </h1> <form action ="# "th:"@{/hello} "th:object =" $ {hello} "method =" post "> <putpint ="> <post = "> <p>メッセージ:<入力タイプ= "text" th:field = "*{content}"/> </p> <p> <punt type = "submit" value = "submit"/> <入力タイプ= "reset" value = "reset"/> </p> </form> </body> </html> src/main/resources/templates/result.html
<!doctype html> <html lang = "en" xmlns:th = "http://www.w3.org/199999999999999999999999999999999999999999999999999999999999999999999 <title> title> title </title> </head> <body> <h1> result </h1> <p th:text = "'id:' + $ {hello.id}"/> <p th:text = "'content:' + $ {hello.content}"/> <a href = "/" rel = "external nofollow">別のメッセージ</aスプリングブートスタート
org.springframework.boot.springApplicationimport org.springframework.boot.autoconfigure.springbootapplication/***は、http://quanke.name on 2018/1/9によって作成されました。 */@springbootapplication applicationfun main(args:array <string>){springApplication.run(application :: class.java、 *args)}プロジェクトを開始して、ttp:// localhost:8080/にアクセスしてください。
参照:https://spring.io/guides/gs/handling-form-submission/
上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。