最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2018年01月22日 11:02:53   作者:quanke  
本篇文章主要介紹了Spring Boot 與 Kotlin 處理Web表單提交的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

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

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

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的默認(rèn)無參構(gòu)造函數(shù),默認(rèn)把所有的類設(shè)置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"
}

創(chuàng)建實(shí)體類Hello

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

創(chuàng)建Controller

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模板文件的名稱,對應(yīng)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.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/

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Spring?Boot實(shí)現(xiàn)MyBatis動態(tài)創(chuàng)建表的操作語句

    Spring?Boot實(shí)現(xiàn)MyBatis動態(tài)創(chuàng)建表的操作語句

    這篇文章主要介紹了Spring?Boot實(shí)現(xiàn)MyBatis動態(tài)創(chuàng)建表,MyBatis提供了動態(tài)SQL,我們可以通過動態(tài)SQL,傳入表名等信息然組裝成建表和操作語句,本文通過案例講解展示我們的設(shè)計思路,需要的朋友可以參考下
    2024-01-01
  • 解決分頁插件pagehelper在SpringBoot不起作用的問題

    解決分頁插件pagehelper在SpringBoot不起作用的問題

    這篇文章主要介紹了解決分頁插件pagehelper在SpringBoot不起作用的問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-05-05
  • SpringbBoot實(shí)現(xiàn)Tomcat集群的會話管理的詳細(xì)過程

    SpringbBoot實(shí)現(xiàn)Tomcat集群的會話管理的詳細(xì)過程

    文章介紹了如何使用Nginx作為負(fù)載均衡器和SpringSession配合Redis實(shí)現(xiàn)Tomcat集群的會話共享,確??绻?jié)點(diǎn)訪問時會話的一致性和持久性,通過具體的步驟和示例代碼,感興趣的朋友一起看看吧
    2024-12-12
  • Spring Cloud Feign實(shí)例講解學(xué)習(xí)

    Spring Cloud Feign實(shí)例講解學(xué)習(xí)

    這篇文章主要介紹了Spring Cloud Feign實(shí)例講解學(xué)習(xí),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-02-02
  • java讀取excel表格的方法

    java讀取excel表格的方法

    這篇文章主要為大家詳細(xì)介紹了java讀取excel表格的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-12-12
  • 代理模式之Java動態(tài)代理實(shí)現(xiàn)方法

    代理模式之Java動態(tài)代理實(shí)現(xiàn)方法

    今天一個偶然的機(jī)會我突然想看看JDK的動態(tài)代理,因?yàn)橐郧耙仓酪稽c(diǎn),而且只是簡單的想測試一下使用,使用很快里就寫好了這么幾個接口和類,需要的朋友可以參考下
    2012-11-11
  • spring?data?jpa查詢一個實(shí)體類的部分屬性方式

    spring?data?jpa查詢一個實(shí)體類的部分屬性方式

    這篇文章主要介紹了spring?data?jpa查詢一個實(shí)體類的部分屬性方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-02-02
  • 關(guān)于log4j日志擴(kuò)展---自定義PatternLayout

    關(guān)于log4j日志擴(kuò)展---自定義PatternLayout

    這篇文章主要介紹了關(guān)于log4j日志擴(kuò)展---自定義PatternLayout,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • 如何解決java.util.concurrent.CancellationException問題

    如何解決java.util.concurrent.CancellationException問題

    這篇文章主要介紹了如何解決java.util.concurrent.CancellationException問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-05-05
  • maven package 打包報錯 Failed to execute goal的解決

    maven package 打包報錯 Failed to execute goal的解決

    這篇文章主要介紹了maven package 打包報錯 Failed to execute goal的解決,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-11-11

最新評論

南召县| 高州市| 赫章县| 闸北区| 额济纳旗| 古田县| 京山县| 栖霞市| 乐亭县| 兰州市| 乌苏市| 京山县| 肥乡县| 三门县| 都昌县| 应城市| 宽城| 扎鲁特旗| 翁牛特旗| 隆回县| 曲水县| 鹤峰县| 麦盖提县| 霸州市| 新闻| 山丹县| 桓仁| 绵竹市| 蒙阴县| 古田县| 灵川县| 黄山市| 桃园市| 宁都县| 渭南市| 朝阳市| 平阴县| 英山县| 托克逊县| 阜南县| 韶山市|