查看: 3068|回复: 0

[Java语言] Spring Boot与Kotlin处理Web表单提交的方法

发表于 2018-2-24 08:00:02

我们在做web开发的时候,肯定逃不过表单提交,这篇文章通过Spring Boot使用Kotlin 语言 创建和提交一个表单。

下面我们在之前《Spring Boot 与 Kotlin使用Freemarker模板引擎渲染web视图》项目的基础上,增加处理表单提交。

build.gradle 文件没有变化,这里贴一下完整的build.gradle

  1. group 'name.quanke.kotlin'
  2. version '1.0-SNAPSHOT'
  3. buildscript {
  4. ext.kotlin_version = '1.2.10'
  5. ext.spring_boot_version = '1.5.4.RELEASE'
  6. repositories {
  7. mavenCentral()
  8. }
  9. dependencies {
  10. classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
  11. classpath("org.springframework.boot:spring-boot-gradle-plugin:$spring_boot_version")
  12. // Kotlin整合SpringBoot的默认无参构造函数,默认把所有的类设置open类插件
  13. classpath("org.jetbrains.kotlin:kotlin-noarg:$kotlin_version")
  14. classpath("org.jetbrains.kotlin:kotlin-allopen:$kotlin_version")
  15. }
  16. }
  17. apply plugin: 'kotlin'
  18. apply plugin: "kotlin-spring" // See https://kotlinlang.org/docs/reference/compiler-plugins.html#kotlin-spring-compiler-plugin
  19. apply plugin: 'org.springframework.boot'
  20. jar {
  21. baseName = 'chapter11-5-4-service'
  22. version = '0.1.0'
  23. }
  24. repositories {
  25. mavenCentral()
  26. }
  27. dependencies {
  28. compile "org.jetbrains.kotlin:kotlin-stdlib-jre8:$kotlin_version"
  29. compile "org.springframework.boot:spring-boot-starter-web:$spring_boot_version"
  30. compile "org.springframework.boot:spring-boot-starter-thymeleaf:$spring_boot_version"
  31. // compile "com.fasterxml.jackson.module:jackson-module-kotlin:$kotlin_version"
  32. testCompile "org.springframework.boot:spring-boot-starter-test:$spring_boot_version"
  33. testCompile "org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version"
  34. }
  35. compileKotlin {
  36. kotlinOptions.jvmTarget = "1.8"
  37. }
  38. compileTestKotlin {
  39. kotlinOptions.jvmTarget = "1.8"
  40. }
复制代码

创建实体类Hello

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

创建Controller

  1. import name.quanke.kotlin.chaper11_5_4.entity.Hello
  2. import org.springframework.stereotype.Controller
  3. import org.springframework.ui.ModelMap
  4. import org.springframework.web.bind.annotation.ModelAttribute
  5. import org.springframework.web.bind.annotation.PostMapping
  6. import org.springframework.web.bind.annotation.RequestMapping
  7. /**
  8. * Created by http://quanke.name on 2018/1/10.
  9. */
  10. @Controller
  11. class HelloController {
  12. @RequestMapping("/")
  13. fun index(map: ModelMap): String {
  14. // / 加入一个属性,用来在模板中读取
  15. map.addAttribute("host", "http://quanke.name")
  16. map.addAttribute("hello",Hello())
  17. // return模板文件的名称,对应src/main/resources/templates/index.html
  18. return "index"
  19. }
  20. @PostMapping("/hello")
  21. fun helloPostSubmit(@ModelAttribute hello: Hello): String {
  22. return "result"
  23. }
  24. }
复制代码

页面展示层

src/main/resources/templates/index.html

  1. <!DOCTYPE html>
  2. <html xmlns:th="http://www.w3.org/1999/xhtml">
  3. <head lang="en">
  4. <title>quanke.name</title>
  5. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
  6. </head>
  7. <body>
  8. <h1 th:text="${host}">Hello World</h1>
  9. <h1>Form</h1>
  10. <form action="#" th:action="@{/hello}" th:object="${hello}" method="post">
  11. <p>Id: <input type="text" th:field="*{id}"/></p>
  12. <p>Message: <input type="text" th:field="*{content}"/></p>
  13. <p><input type="submit" value="Submit"/> <input type="reset" value="Reset"/></p>
  14. </form>
  15. </body>
  16. </html>
复制代码

src/main/resources/templates/result.html

  1. <!DOCTYPE html>
  2. <html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
  5. <title>Title</title>
  6. </head>
  7. <body>
  8. <h1>Result</h1>
  9. <p th:text="'id: ' + ${hello.id}"/>
  10. <p th:text="'content: ' + ${hello.content}"/>
  11. <a href="/" rel="external nofollow" >Submit another message</a>
  12. </body>
  13. </html>
复制代码

Spring Boot 启动

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

启动工程,访问ttp://localhost:8080/:

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持程序员之家。



回复

使用道具 举报