Cuando estamos haciendo desarrollo web, definitivamente no podremos escapar de la presentación del formulario. Este artículo crea y envía un formulario a través del arranque de primavera usando el idioma Kotlin.
A continuación, agregaremos el envío del formulario de procesamiento basado en el proyecto anterior "Boot Spring y Kotlin Renderizando el proyecto utilizando el motor de plantilla Freemarker".
El archivo build.gradle no ha cambiado. Aquí hay una construcción completa.
Grupo 'name.quanke.kotlin'version' 1.0-snapshot'buildscript {ext.kotlin_version = '1.2.10' ext.spring_boot_version = '1.5.4.Release' Repositorios {mavencentral ()} dependencias {classpath "org.jetbrains.kotlin: kotlin-gradle--opllin: $ thierntlin: $" classpath ("org.springframework.boot: spring-boot-gradle-plugin: $ spring_boot_version") // Kotlin integra el constructor de parámetros predeterminado de SpringBoot, y establece todas las clases para abrir el complemento de clase classpath ("org.jetbrains.kotlin: kotlin-noarg: $ kotlin_version") classpath ("org.jetbrains.kotlin: kotlin-allopen: $ kotlin_version")}} Aplicar complemento: 'kotlin'apply plugin: "kotlin-spring" // consulte https://kotlinlang.org/docs/refers/compiler-plugins.html#kotlin Plugin: 'org.springframework.boot'jar {BaseName =' Capítulo11-5-4-Service 'Versión =' 0.1.0 '} Repositorios {mavencentral ()} dependencias {compilar "org.jetbrains.kotlin: kotlin-stdlib-jre8: $ kotlin_version" compilarse " "org.springframework.boot: spring-boot-starter-web: $ spring_boot_version" compile "org.springframework.boot: spring-boot-sharter-thymeleaf: $ spring_boot_version" // compilio "testivilmml.jackson.module: jackson-kotlin: $ koTlinsion" testcompile "testcompile" "org.springframework.boot: spring-boot-starter-test: $ spring_boot_version" testCompile "org.jetbrains.kotlin: kotlin-test-junit: $ kotlin_version"} compilekotlin {kotlinoptions.jvmtarget = "1.8"} compiletline {kotlintline.jotline "1.8"}Crear clase de entidad hola
/*** Creado por http://quanke.name el 2018/1/12. */Data Class Hello (Var ID: Long? = 0, VAR Content: String? = "")
Crear controlador
Nombre de importación org.springframework.web.bind.annotation.postmappingimport org.springframework.web.bind.annotation.requestmapping/*** creado por http://quanke.name el 2018/1/10. */ @ControlerClass hellocontroller {@RequestMapping ("/") Dun Index (map: modelmap): string {///Agregar un atributo a leer map.addattribute ("host", "http://quanke.name") map.addattribute ("Hola", hola ()) src/main/recursos/plantlates/index.html return "index"} @postmapping ("/hello") diversión hellopostSubmit (@modelattribute Hola: hola): cadena {return "resultado"}}Capa de visualización de la página
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 = "contenido" contenido "de contenido =" texto/html; Charset = utf-8 "/> </head> <body> <h1 th: text =" $ {host} "> hola world </h1> <h1> formal </h1> <form de acción ="#"th: action ="@{/hello} "th: object =" $ {hola} "método =" post "> <p> id: <input type =" text "th: campo ="*{{} "/> </p. <P> Mensaje: <input type = "text" th: field = "*{content}"/> </p> <p> <input type = "envíe" valor = "envíe"/> <input type = "reset" value = "reet"/> </p> </form> </body> </html> SRC/Main/Resources/Templates/Result.html
<! Doctype html> <html lang = "en" xmlns: th = "http://www.w3.org/1999/xhtml"> <head> <meta http-equiv = "content-type" content = "text/html; charset = utf-8"/> <title> title </title> </head> <body> <h1> resultado </h1> <p th: text = "'id:' + $ {hello.id}"/> <p th: text = "'content:' + $ {hello.content}"/> <a href = "/" rel = "nofollow externo"> enviar otro mensaje </a> </body> </html>Inicio de arranque de primavera
importar org.springframework.boot.springapplicationImport org.springframework.boot.autoconfigure.springbootapplication/*** creado por http://quanke.name el 2018/1/9. */@SpringBootApplicationClass ApplicationFun Main (args: Array <String>) {SpringApplication.run (Application :: Class.Java, *Args)}Comience el proyecto y visite ttp: // localhost: 8080/:
Referencia: https://spring.io/guides/gs/handling-form-submission/
Lo anterior es todo el contenido de este artículo. Espero que sea útil para el aprendizaje de todos y espero que todos apoyen más a Wulin.com.