Wenn wir Webentwicklung durchführen, werden wir definitiv nicht in der Lage sein, der Einreichung der Form zu entkommen. In diesem Artikel wird ein Formular über Spring Boot mit der Kotlin -Sprache erstellt und unterbreitet.
Im Folgenden werden wir das Projekt "Spring Boot und Kotlin Rendering -Webansichten mit Freemarker Template Engine" basierend auf dem vorherigen Projekt "Spring Boot und Kotlin Rendering" hinzufügen.
Die build.gradle -Datei hat sich nicht geändert. Hier ist ein kompletter Build.gradle
Gruppe 'name.quanke.kotlin'version' 1.0-snapshot'buildscript {ext.kotlin_version = '1.2.10' ext.spring_boot_version = '1.5.4.Release' Repositories {mavencental ()} Abhängigkeiten {Klassenpath "org. classPath ("org.springframework.boot: Spring-Boot-Gradle-Plugin: $ spring_boot_version") // Kotlin integriert Springboot-Standard-parameterlosen Konstruktor und setzt alle Klassen für das Open-Class-Plugin-Klassenpath ("org.jetbrains.kotlin: kotlin-noarg: $ kotlin_version")) classPath ("org.jetbrains.kotlin: kotlin-allopen: $ kotlin_version")}} Plugin anwenden: 'kotlin'apply plugin: "kotlin-sping" // siehe https://kotlinlang.org/docs/reference/compiler-Plugin.html#kotlin-ccompiler-Plugin.html#kotlin-ccompiler-plugin.htmtml#kotlin- 'org.springFramework.boot'jar {baseName =' Kapitel11-5-4-Service 'Version =' 0.1.0 '} Repositories {mavencentral ()} Abhängigkeiten {Compile "org.jetbrains.kotlin: Kotlin-Stdlib-jre8: $ kotlin_version" compile "compile" compile "compile" compile "compile" compile "compiles "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-Moodule: $ kotination-test-wsion. "org.springframework.boot: Spring-Boot-Starter-Test: $ Spring_Boot_Version" testCompile "org.jetbrains.kotlin: kotlin-test-junit: $ kotlin_version"} compilekotlin {kotlinoptions.jvmtarget = "1.8"} compiletestkotlin "1,8"}Erstellen Sie die Entitätsklasse Hallo
/*** Erstellt von http://quanke.name am 2018/1/12. */Datenklasse Hallo (var id: lang? = 0, var Inhalt: String? = "")
Controller erstellen
name importieren.quanke.kotlin org.springframework.web.bind.annotation.postmappingimport org.springframework.web.bind.annotation.requestmapping/*** Erstellt von http://quanke.name am 2018/1/10. */ @ControllerClass HelloconTroller {@RequestMapPing ("/") Fun Index (map: modelMap): String {///fügen Sie ein Attribut hinzu map.addAttribute ("Host", "http://quanke.name") MAP.AddatTribute ("HALLEINGENDE (" HALLE (HALLE) () //). src/main/ressourcen/templates/index.html return "index"} @postmapping ("/hello") Fun HellopostSubmit (@ModelAttribute Hallo: Hallo): String {return "Ergebnis"}}Seitenschicht Anzeigeschicht
src/main/ressourcen/templates/index.html
<! DocType html> <html xmlns: th = "http://www.w3.org/1999/xhtml"> <head Lang = "en"> <title> quanke.name </title> <meta http-äquiv = content-type "content =" content = "content =" content = "content =" content = "content =" content = "content" content = "/" text/html; charset = utf-8 "/> </head> <body> <h1 th: text =" $ {host} "> Hallo Welt </h1> <H1> Form </h1> <Form Action ="#"th: action ="@{/Hallo} "th: Object =" $ {hello} methode = "post"> </} id: <p Input ". <p> Nachricht: <Eingabe type = "text" th: field = "*{content}"/> </p> <p> <Eingabe type = "subGing" value = "subieren"/> <Eingabe type = "reset" value = "reset"/> </p> </Form> </body> </html> src/main/ressourcen/templates/result.html
<! DocType html> <html lang = "en" xmlns: th = "http://www.w3.org/1999/xhtml"> <kopf> <meta http-equiv = "content-type" content = "text/html; charset = utf-8"/> <title> Titel </title> </head> <body> <h1> Ergebnis </h1> <p th: text = "'id:' + $ {hello.id}"/> <p th: text = "'Inhalt:' + $ {Hallo.Spring Boot Start
import org.springframework.boot.springapplicationImport org.springframework.boot.autoconfigure.springbootApplication/*** Erstellt von http://quanke.name am 2018/1/9. */@SpringbootApplicationClass applicationFun main (Args: Array <string>) {SpringApplication.run (application :: class.java, *args)}Starten Sie das Projekt und besuchen Sie ttp: // localhost: 8080/:
Referenz: https://spring.io/guides/gs/handling-form-submission/
Das obige ist der gesamte Inhalt dieses Artikels. Ich hoffe, es wird für das Lernen aller hilfreich sein und ich hoffe, jeder wird Wulin.com mehr unterstützen.