一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - Spring Boot與Kotlin處理Web表單提交的方法

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

2021-03-25 10:48quanke Java教程

本篇文章主要介紹了Spring Boot 與 Kotlin 處理Web表單提交的方法,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

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

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

build.gradle 文件沒有變化,這里貼一下完整的build.gradle

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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-plugin
apply 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

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

創建Controller

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import name.quanke.kotlin.chaper11_5_4.entity.Hello
import org.springframework.stereotype.Controller
import org.springframework.ui.ModelMap
import org.springframework.web.bind.annotation.ModelAttribute
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestMapping
/**
 * Created by http://quanke.name on 2018/1/10.
 */
@Controller
class 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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<!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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
<!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 啟動

?
1
2
3
4
5
6
7
8
9
10
11
12
import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.SpringBootApplication
/**
 * Created by http://quanke.name on 2018/1/9.
 */
 
@SpringBootApplication
class Application
 
fun main(args: Array<String>) {
  SpringApplication.run(Application::class.java, *args)
}

啟動工程,訪問ttp://localhost:8080/:

參考:https://spring.io/guides/gs/handling-form-submission/

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:https://www.jianshu.com/p/a790c185948e

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 性满足久久久久久久久 | 色婷丁香 | 四虎影视4hu最新地址在线884 | 四虎最新永久在线精品免费 | 亚洲国产美女精品久久 | 久久日韩精品无码一区 | 男人女人日皮视频 | 久久精品18 | 1024免费观看完整版在线播放 | 国产在线观看人成激情视频 | www免费看| 日韩欧美中文在线 | 男女男精品网站免费观看 | 国产亚洲精品美女 | 大学生宿舍飞机 free | chinesegay黑袜玩奴 | 1769最新资源站| 波多野结衣家庭教师 | a毛片久久免费观看 | 婷婷在线综合 | 精品在线网站 | heyzo1754北岛玲在线视频 | 91在线 一区 二区三区 | 纲手被强喷水羞羞漫画 | 成人久久久 | 国产精品久久久久久岛国 | 色综合天天综合网看在线影院 | 精品日韩欧美一区二区三区 | 国产原创精品 | 97综合 | 日本精品一区二区在线播放 | 精品91自产拍在线观看99re | 国产伦精品一区二区三区免费观看 | 日本视频在线播放 | 91成| 亚洲日本aⅴ片在线观看香蕉 | 肉文高h调教| 99精品视频免费 | 激情乱文 | 视频一区二区三区在线观看 | 九九99九九精彩 |