Spring Boot 与 Kotlin 处理Web表单提交


声明:本文转载自https://my.oschina.net/quanke/blog/1609961,转载目的在于传递更多信息,仅供学习交流之用。如有侵权行为,请联系我,我会及时删除。

我们在做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-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

/**  * 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.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
<!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="/">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/:

更多Spring Boot 和 kotlin相关内容,欢迎关注《Spring Boot 与 kotlin 实战》

参考

全科龙婷

本文发表于2018年01月20日 12:32
(c)注:本文转载自https://my.oschina.net/quanke/blog/1609961,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责。如有侵权行为,请联系我们,我们会及时删除.

阅读 1858 讨论 0 喜欢 0

抢先体验

扫码体验
趣味小程序
文字表情生成器

闪念胶囊

你要过得好哇,这样我才能恨你啊,你要是过得不好,我都不知道该恨你还是拥抱你啊。

直抵黄龙府,与诸君痛饮尔。

那时陪伴我的人啊,你们如今在何方。

不出意外的话,我们再也不会见了,祝你前程似锦。

这世界真好,吃野东西也要留出这条命来看看

快捷链接
网站地图
提交友链
Copyright © 2016 - 2021 Cion.
All Rights Reserved.
京ICP备2021004668号-1