Lorsque nous faisons du développement Web, nous ne pourrons certainement pas échapper à la soumission des formulaires. Cet article crée et soumet un formulaire via Spring Boot à l'aide de la langue Kotlin.
Ci-dessous, nous ajouterons une soumission de formulaire de traitement basé sur le projet de vue Web "Spring Boot et Kotlin Rending Web à l'aide du projet Freemarker Modèle".
Le fichier build.gradle n'a pas changé. Voici une construction complète. Gradle
Groupe 'name.quanke.kotlin'version' 1.0-snapshot'buildscript {ext.kotlin_version = '1.2.10' ext.spring_boot_version = '1.5.4.4 ClassPath ("org.springframework.boot: Spring-Boot-Gradle-Plugin: $ printemps_boot_version") // Kotlin intègre le constructeur paramètre sans paramètre de Springboot, et définit toutes les classes pour ouvrir le plugin de classe ClassPath ("org.jetbrains.kotlin: Kotlin-noarg: $ kotlin_version") CLASSPATH ("org.Jetbrains.kotlin: Kotlin-Allopen: $ Kotlin_version")}} Appliquer Plugin: 'Kotlin'Apply Plugin: "Kotlin-spring" // voir https://kotlinlang.org/docs/reference/combinplugins.htmlin Plugin: 'org.springframework.boot'jar {basename =' Chapter11-5-4-Service 'version =' 0.1.0 '} Repositories {mavencentral ()} dépendances {compile "org.jetbrains.kotlin: kotlin-stdlib-jre8: $ kotlin_version" Compile "org.springframework.boot: printemps-boot-starter-web: $ printemps_boot_version" compile "org.springframework.boot: printemps-boot-starter-thymeleaf: $ printemps_boot_version" // compile "com.fasterxml.jackson.module: jackson-module-kotlin: $ kotlin_version" "org.springframework.boot: printemps-boot-starter-test: $ printemps_boot_version" testcompile "org.jetbrains.kotlin: kotlin-test-junit: $ kotlin_version"} compilekotlin {kotlinoptions.jvmtarget = "1.8} CompileTestkotlin {kotlin "1.8"}Créer une classe d'entité bonjour
/ ** * Créé par http://quanke.name le 2018/1/12. * / classe de données bonjour (var id: long? = 0, var contenu: string? = "")
Créer un contrôleur
Importer name.quanke.kotlin.chaper11_5_4.entity.heloimport org.springframework.sterreotype.Controllerimport org.springframework.ui.modelmapimport org.springframework.web.bind.annotation.modelattributardImportImport org.springframework.web.bind.annotation.postmappingImport org.springframework.web.bind.annotation.requestmapping / ** * créé par http://quanke.name le 2018/1/10. * / @ CONTRIRCLACLASS HELLOCONTROLLER {@RequestMapping ("/") Fun Index (Map: ModelMap): String {// / Ajouter un attribut pour lire map.addattribute ("host", "http://quanke.name") map.addattribute ("Http Hello ()) // Le nom du fichier de modèle de retour, correspondance, correspondant à la correspondance pour être correspondant à la correspondance, correspondant à la correspondance, correspondant à la correspondance, correspondant à la correspondance, correspondant à la correspondance, correspondant à la correspondance, correspondant à la correspondance, correspondant à la correspondance, correspondant à la correspondance, correspondant à la correspondance, correspondant à la correspondance, correspondant à la correspondance, correspondant à la correspondance, correspondant à la correspondance, correspondant à la correspondance, à la correspondance, src / main / ressources / modèles / index.html return "index"} @postmapping ("/ hello") fun hellOsTSubmit (@ModeLattrait Hello: Hello): String {return "result"}}Page Afficher la couche
src / main / ressources / modèles / index.html
<! Doctype html> <html xmlns: th = "http://www.w3.org/1999/xhtml"> <head lang = "en"> <itle> quanke.name </ title> <meta http-equiv = "contenu-type" contenu = "text / html; charset = utf-8 "/> </ head> <body> <h1 th: text =" $ {host} "> bonjour monde <p> Message: <input type = "text" th: field = "* {content}" /> </p> <p> <input type = "soume" value = "soumed" /> <input type = "reset" value = "reset" /> </p> </ form> </ body> </ html> src / main / ressources / modèles / résultat.html
<! Doctype html> <html lang = "en" xmlns: th = "http://www.w3.org/1999/xhtml"> <adread> <meta http-equiv = "contenu-type" contenu = "text / html; charset = utf-8" /> <Title> Title </ title> </ head> <body> <h1> Résultat </h1> <p th: text = "'id:' + $ {hello.id}" /> <p th: text = "'Content:' + $ {hello.content}" /> <a href = "/" rel = "external nofollow"> soumettre un autre message </a>Démarrage du démarrage de printemps
import org.springframework.boot.springApplicationImport org.springframework.boot.autoconfigure.springbootapplication / ** * créé par http://quanke.name le 2018/1/9. * / @ SpringbootApplicationClass ApplicationFun Main (Args: Array <string>) {SpringApplication.run (application :: class.java, * args)}Démarrez le projet et visitez TTP: // LocalHost: 8080 /:
Référence: https://spring.io/guides/gs/handling-forme-submission/
Ce qui précède est tout le contenu de cet article. J'espère que cela sera utile à l'apprentissage de tous et j'espère que tout le monde soutiendra davantage Wulin.com.