Ketika kami melakukan pengembangan web, kami pasti tidak akan dapat menghindari pengiriman formulir. Artikel ini membuat dan mengirimkan formulir melalui boot musim semi menggunakan bahasa Kotlin.
Di bawah ini, kami akan menambahkan pengiriman formulir pemrosesan berdasarkan pada tampilan web "Spring Boot dan Kotlin Rendering Web sebelumnya menggunakan Freemarker Template Engine".
File build.gradle belum berubah. Berikut ini adalah build.gradle lengkap
grup 'name.quanke.kotlin'version' 1.0-snapshot'buildscript {ext.kotlin_version = '1.2.10' ext.spring_boot_version = '1.5.4.release' Repositori {Mavencentral ()} Ketergantungan {classpath "org.jetbrains (mavencentral ()} Dependensi {classpath" org.jetbrains. classpath ("org.springframework.boot: spring-boot-gradle-plugin: $ spring_boot_version") // Kotlin mengintegrasikan konstruktor tanpa parameter default springboot, dan mengatur semua kelas untuk membuka plugin kelas classpath ("org.jetbrains.kotlin: Kotlin-noargin: $") classpath ("org.jetbrains.kotlin: Kotlin-Allopen: $ Kotlin_version")}} Terapkan plugin: 'Kotlin'Apply Plugin: "Kotlin-Spring" // Lihat https://kotlinlang.org/docs/reference/compiler-pluler-lugin-luginer Plugin: 'org.springframework.boot'jar {baseName =' bab11-5-4-service 'versi =' 0.1.0 '} repositori {mavencentral ()} dependensi {compile "org.jetbrains.kotlin: Kotlin-stdlib-jre8: $ Kotlin" "org.springframework.boot:spring-boot-starter-web:$spring_boot_version" compile "org.springframework.boot:spring-boot-starter-thymeleaf:$spring_boot_version"// compile "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: $ Kotlin_version"} Compilekotlin {Kotlinoptions.jvmtar = "1.8" Kotlinoptions.jvmTarget = "1.8"}Buat kelas entitas halo
/*** dibuat oleh http://quanke.name pada 2018/1/12. */kelas data halo (var id: panjang? = 0, var konten: string? = "")
Buat pengontrol
Impor name.Quanke.kotlin.chaper11_5_4.entity.helloimport org.springframework.stereotype.controllerimport org.springframework.ui.modelmapimport org.springframework.web.bind.annotation.modelattributed org.springframework.web.bind.annotation.postmappingImport org.springframework.web.bind.annotation.requestmapping/*** Dibuat oleh http://quanke.name pada 2018/1/10. */ @ControllerClass HelloController {@RequestMapping ("/") Fun Index (peta: ModelMap): String {///Tambahkan atribut untuk membaca peta.addattribute ("host", "http://quanke.name") peta.adDattribute ("hello", hello () ///quanke.name ") peta.adDattribute (" hello ", hello ()/the the name/the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the The SRC/Main/Resources/Templates/Index.html Return "Index"} @PostMapping ("/Hello") Fun HelloPostSubmit (@ModelAttribute Hello: Hello): String {return "hasil"}}Lapisan Tampilan Halaman
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" content="text/html; charset = utf-8 "/> </head> <hody> <h1 th: text =" $ {host} "> halo world </h1> <h1> form </h1> <form Action ="#"th: action ="@{/hello} "tH: objek =" $ {hello} "Metode =" post "> <p> Id: <input =" $ " Th: field = "*{id}"/> </p> <p> Pesan: <input type = "text" th: field = "*{content}"/> </p> <p> <input type = "kirim" value = "kirim"/> <input type = "reset" value = "reset"//</p> </form src/main/sumber daya/templat/hasil.html
<! Doctype html> <html lang = "en" xmlns: th = "http://www.w3.org/1999/xhtml"> <head> <meta http-equiv = "content" content = "text/html; charset = UTF-8"/ <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" >Submit another message</a></body></html>Mulai boot musim semi
impor org.springframework.boot.springapplicationImport org.springframework.boot.autoconfigure.springbootApplication/*** Dibuat oleh http://quanke.name pada 2018/1/9. */@SpringbootApplicationClass applicationFun main (args: array <string>) {springapplication.run (application :: class.java, *args)}Mulai proyek dan kunjungi ttp: // localhost: 8080/:
Referensi: https://spring.io/guides/gs/handling-form-submission/
Di atas adalah semua konten artikel ini. Saya berharap ini akan membantu untuk pembelajaran semua orang dan saya harap semua orang akan lebih mendukung wulin.com.