Quando estamos fazendo desenvolvimento da Web, definitivamente não poderemos escapar do envio do formulário. Este artigo cria e envia um formulário através da Spring Boot usando o idioma Kotlin.
Abaixo, adicionaremos o envio do formulário de processamento com base no projeto anterior "Spring Boot e Kotlin Rendering Web, usando o projeto Freemarker Model Engine".
O arquivo Build.gradle não mudou. Aqui está uma compilação completa.gradle
Grupo 'Name.quanke.kotlin'version' 1.0-Snapshot'buildscript {ext.kotlin_version = '1.2.10' Ext.spring_boot_version = '1.5.4.release' REPROSTILIES.KOTLIN {MAVENCENTRAL ()} Dependências {ClassPath "ORG.JetBrains.Kotlin. ClassPath ("Org.springframework.boot: Spring-Boot-Gradle-Plugin: $ spring_boot_version") // Kotlin integra o construtor sem parâmetros sem parâmetros do Springboot e define todas as classes para abrir a classe plugin ClassPath ("org.jetbrains.kotlin: kotlin-nargin: classpath("org.jetbrains.kotlin:kotlin-allopen:$kotlin_version") }}apply plugin: 'kotlin'apply plugin: "kotlin-spring" // See https://kotlinlang.org/docs/reference/compiler-plugins.html#kotlin-spring-compiler-pluginapply plugin: 'org.springframework.boot'jar {Basename =' Capítulo11-5-4-service 'versão =' 0.1.0 '} repositórios {mavencentral ()} dependências {compilam "org.jetbrains.kotlin: kotlin-stdlib-jre8: $ kotlin_version" " "Org.springframework.boot: spring-boot-starter-web: $ spring_boot_version" compile "org.springframework.boot: spring-boot-starter-timeled: $ spring_boot_version" // compile "COM.Fasterxml.jackson.module: jacksonmsion" // compilando "complinlml.jackson.module: jacksonmsion" // Compile "combile.fasterxml.jackson.module: "Org.springframework.boot: Spring-Boot-Starter-Test: $ spring_boot_version" testcompile "org.jetbrains.kotlin: kotlin-test-junit: $ kotlin_version"} compilekotlin {KotLinOnPions.jvmtarget = "1.8"}}} "1.8"}Crie a classe de entidade olá
/*** Criado por http://quanke.name em 2018/1/12. */Data Class Hello (var ID: Long? = 0, var content: string? = "")
Criar controlador
Importar name.quanke.kotlin.chaper11_5_4.entity.helloimport org.springframework.tereotype.controllerimport org.springframework.ui.modelmapimport org.springframework.bind.anationAtation.mapimportImport.springframework.bind.anation.modelatttra org.springframework.web.bind.annotation.postMappingImport org.springframework.web.bind.annotation.requestmapping/*** Criado por http://quanke.name em 2018/1/10. */ @Controlerclass Hellocontroller {@RequestMapping ("/") Fun Index (map: ModelMap): String {///////Adicione um atributo para ler map.addattribute ("host", "http://quanke.name") map.addtribute ("hello", http://quanke.name ") map.addtribute (" hello, hello ()) // src/main/recursos/modaplates/index.html return "index"} @postMapping ("/hello) divertido hellopostSubMit (@modelattribute hello: hello): string {return" resultado "}}Camada de exibição da página
src/main/recursos/modelos/index.html
<! Doctype html> <html xmlns: th = "http://www.w3.org/1999/xhtml"> <head lang = "en"> <title> quanke.name </title> <meta http-equ-ouquo charset = utf-8 "/> </head> <body> <h1 th: text =" $ {host} "> hello world </h1> <h1> forma </h1> <formulário action ="#"th: action ="@{/hello} "th: object =" $ {hello} "" = "post"> <p> id: <b: th: field = "*{id}"/> </p> <p> mensagem: <input type = "text" th: field = "*{content}"/> </p> <p> <input type = "submit" value = "submit"/> <input Type = "reset =" resset "/> </p> src/main/recursos/modelos/resultado.html
<! Doctype html> <html lang = "pt" xmlns: th = "http://www.w3.org/1999/xhtml"> <head> <meta http-equiv = "content-type" = "text/html; charset = utf-88" <title> Title </title> </head> <body> <h1> resultado </h1> <p th: text = "'iD:' + $ {hello.id}"/> <p: text = "'Content:' + $ {hello.content}"/> <a href = "/" Rel = "externo> sollows"> signing outro} "Início da bota da primavera
importar org.springframework.boot.springapplicationimport org.springframework.boot.autoconfigure.springbootapplication/*** Criado por http://quanke.name em 2018/1/9. */@SpringBootApplicationClass ApplicationFun Main (Args: Array <string>) {SpringApplication.run (Application :: Class.java, *args)}Inicie o projeto e visite ttp: // localhost: 8080/:
Referência: https://spring.io/guides/gs/handling-form-submission/
O exposto acima é todo o conteúdo deste artigo. Espero que seja útil para o aprendizado de todos e espero que todos apoiem mais o wulin.com.