Когда мы делаем веб -разработку, мы определенно не сможем избежать подчинения формы. Эта статья создает и представляет форму через Spring Boot с использованием языка Kotlin.
Ниже мы добавим представление формы обработки на основе предыдущего проекта «Spring Boot Boot и Kotlin Rendering Views с использованием шаблона Freemarker».
Файл build.gradle не изменился. Вот полная сборка.gradle
Group 'name.quanke.kotlin'Version' 1.0-SnapShot'BuildScript {ext.kotlin_version = '1.2.10' Ext.spring_boot_version = '1.5.4.Release' Repositories {mavencentral ()} зависимости {classpath "org.jetbrains.kotlin: kotlinle-plugrindenties {classpath" org.jetbrains.kotlin: kotlinle-pligrindenties {classpath "ourg.jetbrains.kotlinlristr_cers-gradlindly {wrg.jetbrains.kotlinlrilr ClassPath ("org.springframework.boot: Spring-Boot-Gradle-Plugin: $ spring_boot_version") // Котлин интегрирует конструктор без параметра-по умолчанию Springboot и устанавливает все классы для открытия плагина класса ClassPath (" classpath ("org.jetbrains.kotlin: kotlin-allopen: $ kotlin_version")}} Применить плагин: 'kotlin'apply plugin: "kotlin-spring" // См. 'org.springframework.boot'jar {basename =' gupt11-5-4-service 'version =' 0.1.0 '} Репозитории {mavencentral ()} зависимости {compile "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-thymeleaf: $ spring_boot_version" // compile "com.fasterxml.jackson.module: jackson-module-kotlin "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"}Создать класс Entity Hello
/*** Создано http://quanke.name 2018/1/12. */класс данных Hello (var id: long? = 0, var content: string? = "")
Создать контроллер
импорт name.quanke.kotlin.chaper11_5_4.entity.helloimport org.springframework.stereotype.controllerimport org.springframework.ui.modelmapimport org.springframework.web.bindation.modelattributemport org.springframework.web.bind.annotation.postmappingImport org.springframework.web.bind.annotation.requestmapping/*** Создано http://quanke.name 2018/1/10. */ @Controlerclass hellocontroller {@requestmapping ("/") Fun Index (map: modelmap): string {///add attribute в read map.addattribute ("host", "http://quanke.name") map.addattribut src/main/resources/templates/index.html return "index"} @postmapping ("/hello") Fun hellopostsubmit (@modelattribute hello: hello): string {return "result"}}Отображение страницы
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" = "html; charset = utf-8 "/> </head> <body> <h1 th: text =" $ {host} "> hello world </h1> <h1> form </h1> <form action ="#"th: action ="@{/hello} "th: object =" $ {hello} "method ="> <p> id: <input = "th: th: field {hello}" "/"> <p> id: <input = "th: field {hello}"} "> <p> {/in hello} <p> Сообщение: <input type = "text" th: field = "*{content}"/> </p> <p> <input type = "propect" value = "opper"/> <input type = "reset" value = "reset"/> </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> result </h1> <p th: text = "'id:' + $ {hello.id}"/> <p th: text = "'' Content: ' + $ {hello.content}"/> <a href = "/" rel = "nofollow"> Отправить другое сообщение </a> </body> </href = "/" rel = "nofollow"> Отправить другое сообщение </a> </> </href = "/" rel = "nofollow">.Spring Boot Start
Импорт org.springframework.boot.springapplicationImport org.springframework.boot.autoconfigure.springbootapplication/*** Создан http://quanke.name на 2018/1/1/9. */@SpringBootApplicationClass ApplicationFun Main (args: array <string>) {springApplication.run (application :: class.java, *args)}}Начните проект и посетите ttp: // localhost: 8080/:
Ссылка: https://spring.io/guides/gs/handling-form-submission/
Выше всего содержание этой статьи. Я надеюсь, что это будет полезно для каждого обучения, и я надеюсь, что все будут поддерживать Wulin.com больше.