国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > Java > 正文

Spring Boot與Kotlin處理Web表單提交的方法

2024-07-13 10:17:05
字體:
來源:轉載
供稿:網友

我們在做web開發的時候,肯定逃不過表單提交,這篇文章通過Spring Boot使用Kotlin 語言 創建和提交一個表單。

下面我們在之前《Spring Boot 與 Kotlin使用Freemarker模板引擎渲染web視圖》項目的基礎上,增加處理表單提交。

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()  }  dependencies {    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"    classpath("org.springframework.boot:spring-boot-gradle-plugin:$spring_boot_version")//    Kotlin整合SpringBoot的默認無參構造函數,默認把所有的類設置open類插件    classpath("org.jetbrains.kotlin:kotlin-noarg:$kotlin_version")    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 = 'chapter11-5-4-service'  version = '0.1.0'}repositories {  mavenCentral()}dependencies {  compile "org.jetbrains.kotlin:kotlin-stdlib-jre8:$kotlin_version"  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:$kotlin_version"  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"}compileTestKotlin {  kotlinOptions.jvmTarget = "1.8"}

創建實體類Hello

/** * Created by http://quanke.name on 2018/1/12. */data class Hello(var id: Long? = 0, var content: String? = "")

創建Controller

import name.quanke.kotlin.chaper11_5_4.entity.Helloimport org.springframework.stereotype.Controllerimport org.springframework.ui.ModelMapimport org.springframework.web.bind.annotation.ModelAttributeimport org.springframework.web.bind.annotation.PostMappingimport org.springframework.web.bind.annotation.RequestMapping/** * Created by http://quanke.name on 2018/1/10. */@Controllerclass HelloController {  @RequestMapping("/")  fun index(map: ModelMap): String {//    / 加入一個屬性,用來在模板中讀取    map.addAttribute("host", "http://quanke.name")    map.addAttribute("hello",Hello())    // return模板文件的名稱,對應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" content="text/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="post">  <p>Id: <input type="text" th:field="*{id}"/></p>  <p>Message: <input type="text" th:field="*{content}"/></p>  <p><input type="submit" value="Submit"/> <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="external nofollow" >Submit another message</a></body></html>

Spring Boot 啟動

import org.springframework.boot.SpringApplicationimport org.springframework.boot.autoconfigure.SpringBootApplication/** * Created by http://quanke.name on 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/

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VeVb武林網。


注:相關教程知識閱讀請移步到JAVA教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 平舆县| 岢岚县| 义乌市| 台湾省| 昌邑市| 且末县| 绥化市| 丰都县| 泗水县| 临清市| 砀山县| 永春县| 吴川市| 黄大仙区| 榆社县| 屏南县| 新和县| 万荣县| 舒城县| 遵化市| 大宁县| 麻栗坡县| 玛沁县| 织金县| 休宁县| 兰州市| 秭归县| 新巴尔虎右旗| 婺源县| 兴义市| 渭源县| 郓城县| 腾冲县| 汽车| 通州区| 龙里县| 鱼台县| 佛学| 荣昌县| 彩票| 怀集县|