Spring Boot与Kotlin处理Web表单提交的方法

 更新时间:2018年01月22日 11:02:53   作者:quanke  
本篇文章主要介绍了Spring Boot 与 Kotlin 处理Web表单提交的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

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

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

相关文章

  • Spring Boot实现MyBatis动态创建表的操作语句

    Spring Boot实现MyBatis动态创建表的操作语句

    这篇文章主要介绍了Spring Boot实现MyBatis动态创建表,MyBatis提供了动态SQL,我们可以通过动态SQL,传入表名等信息然组装成建表和操作语句,本文通过案例讲解展示我们的设计思路,需要的朋友可以参考下
    2024-01-01
  • 解决分页插件pagehelper在SpringBoot不起作用的问题

    解决分页插件pagehelper在SpringBoot不起作用的问题

    这篇文章主要介绍了解决分页插件pagehelper在SpringBoot不起作用的问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-05-05
  • SpringbBoot实现Tomcat集群的会话管理的详细过程

    SpringbBoot实现Tomcat集群的会话管理的详细过程

    文章介绍了如何使用Nginx作为负载均衡器和SpringSession配合Redis实现Tomcat集群的会话共享,确保跨节点访问时会话的一致性和持久性,通过具体的步骤和示例代码,感兴趣的朋友一起看看吧
    2024-12-12
  • Spring Cloud Feign实例讲解学习

    Spring Cloud Feign实例讲解学习

    这篇文章主要介绍了Spring Cloud Feign实例讲解学习,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-02-02
  • java读取excel表格的方法

    java读取excel表格的方法

    这篇文章主要为大家详细介绍了java读取excel表格的方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-12-12
  • 代理模式之Java动态代理实现方法

    代理模式之Java动态代理实现方法

    今天一个偶然的机会我突然想看看JDK的动态代理,因为以前也知道一点,而且只是简单的想测试一下使用,使用很快里就写好了这么几个接口和类,需要的朋友可以参考下
    2012-11-11
  • spring data jpa查询一个实体类的部分属性方式

    spring data jpa查询一个实体类的部分属性方式

    这篇文章主要介绍了spring data jpa查询一个实体类的部分属性方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-02-02
  • 关于log4j日志扩展---自定义PatternLayout

    关于log4j日志扩展---自定义PatternLayout

    这篇文章主要介绍了关于log4j日志扩展---自定义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的解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-11-11

最新评论