우리가 웹 개발을 할 때, 우리는 확실히 양식 제출을 피할 수 없을 것입니다. 이 기사는 Kotlin 언어를 사용하여 Spring Boot를 통해 양식을 작성하고 제출합니다.
아래는 프리 마커 템플릿 엔진을 사용한 이전 "스프링 부츠 및 Kotlin 렌더링 웹보기"프로젝트를 기반으로 처리 양식 제출을 추가합니다.
build.gradle 파일이 변경되지 않았습니다. 다음은 완전한 Build.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 : kotlin-kotlugin : kotlin-gupgin. classPath ( "org.springframework.boot : spring-boot-gradle-plugin : $ spring_boot_version") // Kotlin은 SpringBoot의 기본 매개 변수가없는 생성자를 통합하고 모든 클래스를 클래스 플러그인 클래스 경로 ( "org.jetbrains.kotlin : Kotlin-Noarg : $ kotlin_version")로 설정합니다. classpath ( "org.jetbrains.kotlin : kotlin-allopen : $ kotlin_version")}}}} 플러그인 적용 : 'kotlin'Apply 플러그인 : "Kotlin-Spring"// https://kotlinlang.org/Reference/Compiler-plugins.html#kotlin-compoller-compoler-compontler-compoller-compoller-compontler-plugin 'org.springframework.boot'jar {basename ='챕터 11-5-4-service '버전 ='0.1.0 '} 리포지토리 {mavencentral ()} 종속성 {compile "org.jetbrains.kotlin : kotlin-stdlib-jre8 : $ kotlin_verson"compile "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 : $ kotline" "org.springframework.boot : spring-boot-starter-test : $ spring_boot_version"testcompile "org.jetbrains.kotlin : Kotlin : $ kotlin_version"} compileKotlin {kotlinoptions.jvmtarget = "1.8"} compileTeTlin {kotlyptions. "1.8"}엔티티 클래스 제작 안녕하세요
/*** 2018/1/12에 http://quanke.name에 의해 만들어졌습니다. */data class hello (var id : long? = 0, var 컨텐츠 : String? = "")
컨트롤러를 만듭니다
가져 오기 이름 .quanke.kotlin.chaper11_5_4.entity.helloimport org.springframework.stereotyp.controllerimport org.springframework.ui.modelmapimport org.springframework.web.bind.annotation.modelattributeimport org.springframework.web.bind.annotation.postmappingimport org.springframework.web.bind.annotation.requestmapping/*** http://quanke.name 2018/1/10에 의해 생성되었습니다. */ @controllerclass hellocontroller {@requestmapping ( "/") fun index (map : modelmap) : String {///////////////////// addattribute ( "http://quanke.name"))) map.addattribute ( "hello ()) // 수익 파일의 이름, 해당 템플릿 파일, src/main/resources/templates/index.html return "index"} @postmapping ( "/hello") Fun HelloPostSubmit (@ModelAttribute hello : hello) : 문자열 {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"content = "text/html; charset = utf-8 "/> </head> <bod> <h1 th : text ="$ {host} "> hello world </h1> <h1> form </h1> <form action ="#"#"th : action = "@{/hello}"th : object = "$ {hello}"method = "post"> id> id "input type ="text " th : field = "*{id}"/> </p> <p> 메시지 : <input type = "text"th : field = "*{content}"/> </p> <p> <입력 유형 = "value ="제출 "/> <입력 유형 ="value = "reset"/> </p> </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> </head> <hod> <h1> 결과 </h1> <pth : text = " 'id :' + $ {hello.id}"/> <pth : ' + $ {hello.content} "/> <a href ="/"rel ="external nofollow "> </html> </html> </html>봄 부트 시작
import org.springframework.boot.springApplicationimport org.springframework.autoconfigure.springbootapplication/*** http://quanke.name 2018/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을 더 지원하기를 바랍니다.