เมื่อเราทำการพัฒนาเว็บเราจะไม่สามารถหลบหนีจากการส่งแบบฟอร์มได้อย่างแน่นอน บทความนี้สร้างและส่งแบบฟอร์มผ่านการบูตฤดูใบไม้ผลิโดยใช้ภาษา Kotlin
ด้านล่างนี้เราจะเพิ่มการส่งแบบฟอร์มการประมวลผลตามก่อนหน้านี้ "สปริงบูตและ KOTLIN Rendering Web Views โดยใช้ Freemarker Template Engine"
ไฟล์ build.gradle ไม่เปลี่ยนแปลง นี่คือ build.gradle ที่สมบูรณ์
group 'name.quanke.kotlin'version' 1.0-snapshot'buildscript {ext.kotlin_version = '1.2.10' ext.spring_boot_version = '1.5.4.release' repositories 'mavenCentral ()} "org.jetbrains.kotlin: kotlin-gradle-plugin: $ kotlin_version" classpath ("org.springframework.boot: Spring-boot-gradle-plugin: $ spring_boot_version") // Kotlin classpath ("org.jetbrains.kotlin: kotlin-noarg: $ kotlin_version") classpath ("org.jetbrains.kotlin: kotlin-allopen: $ kotlin_version")}} ใช้ปลั๊กอิน: 'kotlin'apply ปลั๊กอิน: "Kotlin-spring" // https://kotlinlang.org/docs/reference/compiler-plugins.html#kotlin-spring-compiler-pluginapply ปลั๊กอิน: 'org.springframework.boot'jar {basename =' บทที่ 11-5-4-service ' {Compile "org.jetbrains.kotlin: kotlin-stdlib-jre8: $ kotlin_version" Compile "org.springframework.boot: Spring-boot-starter-web: $ spring_boot_version "com.fasterxml.jackson.module: Jackson-Module-Kotlin: $ KOTLIN_VERSION" TestCompile "org.springframework.boot: สปริง--สตาร์เทสต์ทดสอบ: $ spring_boot_version" testCompile "org.jetbrains.kotlin kotlinoptions.jvmtarget = "1.8"} CompileTestKotlin {kotlinoptions.jvmtarget = "1.8"}สร้างคลาสเอนทิตีสวัสดี
/*** สร้างโดย http://quanke.name เมื่อ 2018/1/12 */คลาสข้อมูลสวัสดี (var id: ยาว? = 0, var เนื้อหา: string? = "")
สร้างคอนโทรลเลอร์
นำเข้า Name.quanke.kotlin.chaper11_5_4.entity.helloimport org.springframework.stereotype.controllerimport org.springframework.ui.modelmapimport org.springframework.web.bind. org.springframework.web.bind.annotation.postmappingimport org.springframework.web.bind.annotation.requestmapping/*** สร้างโดย http://quanke.name เมื่อปี 2018/1/10 */ @controlerclass hellocontroller {@requestmapping ("/") ดัชนีความสนุก (แผนที่: modelmap): สตริง {///เพิ่มแอตทริบิวต์เพื่ออ่านแผนที่ AdDattribute ("โฮสต์", "http://quanke.name" src/main/resources/templates/index.html return "index"} @postmapping ("/hello") fun hellopostsubmit (@modelattribute สวัสดี: สวัสดี): สตริง {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> charset = utf-8 "/> </head> <body> <h1 th: text =" $ {host} "> hello world </h1> <h1> form </h1> <form action ="#"th: action =" {/hello} "th: object =" hello} <p> ข้อความ: <อินพุต type = "text" th: field = "*{content}"/> </p> <p> <อินพุต type = "ส่ง" value = "ส่ง"/> <อินพุตประเภท = "รีเซ็ต" value = "รีเซ็ต"/> </p> src/main/resources/templates/result.html
<! doctype html> <html lang = "en" xmlns: th = "http://www.w3.org/1999/xhtml"> <head> <title> ชื่อเรื่อง </title> </head> <body> <h1> ผลลัพธ์ </h1> <p th: text = "'id:' + $ {hello.id}"/> <p th: text = "'เนื้อหา:' + $ {hello.content}"/> <a href = "/" rel = "external nofollow"สปริงบูตเริ่มต้น
นำเข้า org.springframework.boot.springapplicationImport org.springframework.boot.autoconfigure.springbootapplication/*** สร้างโดย http://quanke.name เมื่อปี 2018/1/9 */@springbootapplicationclass applicationfun main (args: array <string>) {springapplication.run (แอปพลิเคชัน :: class.java, *args)}เริ่มโครงการและเยี่ยมชม ttp: // localhost: 8080/:
การอ้างอิง: https://spring.io/guides/gs/handling-form-submission/
ข้างต้นเป็นเนื้อหาทั้งหมดของบทความนี้ ฉันหวังว่ามันจะเป็นประโยชน์ต่อการเรียนรู้ของทุกคนและฉันหวังว่าทุกคนจะสนับสนุน wulin.com มากขึ้น