欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

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

下面我們在之前《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 {
//    / 加入一個(gè)屬性,用來在模板中讀取
    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 與DBunit 配合使用方法

    Spring Boot 與DBunit 配合使用方法

    這篇文章主要介紹了Spring Boot 與DBunit 配合使用方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-09-09
  • Spring中@Value使用詳解及SPEL表達(dá)式

    Spring中@Value使用詳解及SPEL表達(dá)式

    這篇文章主要介紹了Spring中@Value使用詳解及SPEL表達(dá)式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-09-09
  • Spring事務(wù)管理詳細(xì)講解

    Spring事務(wù)管理詳細(xì)講解

    事務(wù)的作用就是為了保證用戶的每一個(gè)操作都是可靠的,事務(wù)中的每一步操作都必須成功執(zhí)行,只要有發(fā)生異常就?回退到事務(wù)開始未進(jìn)行操作的狀態(tài)。事務(wù)管理是Spring框架中最為常用的功能之一,我們在使用Spring?Boot開發(fā)應(yīng)用時(shí),大部分情況下也都需要使用事務(wù)
    2022-10-10
  • RequestContextHolder.getRequestAttributes()空指針問題及解決

    RequestContextHolder.getRequestAttributes()空指針問題及解決

    這篇文章主要介紹了RequestContextHolder.getRequestAttributes()空指針問題及解決,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-01-01
  • Java 泛型全解析

    Java 泛型全解析

    這篇文章主要介紹了Java 泛型的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)Java,感興趣的朋友可以了解下
    2020-08-08
  • Kryo序列化及反序列化用法示例

    Kryo序列化及反序列化用法示例

    這篇文章主要介紹了Kryo序列化及反序列化用法示例,小編覺得挺不錯(cuò)的,這里分享給大家,需要的朋友可以參考下。
    2017-10-10
  • 分享令人目瞪口呆的?Java?代碼技巧

    分享令人目瞪口呆的?Java?代碼技巧

    這篇文章主要介紹了令人目瞪口呆的?Java?代碼技巧,本文從寫?Java?程序的小方面一直寫到大方面,來闡述了如何才能寫好?Java?程序,并告訴讀者們?nèi)绾尾拍芴岣咦陨淼木幋a水平,需要的朋友可以參考下
    2022-05-05
  • spring cloud consul使用ip注冊服務(wù)的方法示例

    spring cloud consul使用ip注冊服務(wù)的方法示例

    這篇文章主要介紹了spring cloud consul使用ip注冊服務(wù)的方法示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • AsyncHttpClient ListenableFuture源碼流程解讀

    AsyncHttpClient ListenableFuture源碼流程解讀

    這篇文章主要為大家介紹了AsyncHttpClient ListenableFuture源碼流程解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-12-12
  • Spring ApplicationContext上下文核心容器深入探究

    Spring ApplicationContext上下文核心容器深入探究

    ApplicationContext是Spring應(yīng)用程序中的中央接口,由于繼承了多個(gè)組件,使得ApplicationContext擁有了許多Spring的核心功能,如獲取bean組件,注冊監(jiān)聽事件,加載資源文件等
    2023-01-01

最新評論