免责声明作者不是程序员,不能编码
我既不是Kotlin,也不是Spring的专家,也不不是本文中使用的任何其他技术的专家。 我是一名普通的Java初中生,决定尝试kotlin。 借助Techtrain,一切都在Sapsan膝盖上完成
为谁
对于刚刚听说过kotlin但还没有动手的Java开发人员
为了什么
为了证明kotlin在spring boot上可以正常工作,并且在与html结合使用方面与DSL结合使用,它将比使用jsp的传统方法更加方便。
弹簧靴的配置
在这里,一切都与java没什么不同
- 创建项目(kotlin)
- 添加存储库jcenter,依赖于kotlin-stdlib和spring-boot-starter-web,kotlin-maven-plugin
- 确保创建一个可以工作的包,否则Spring将会掉落
- 创建一个开放的应用程序类
- 在Application中,将main方法添加到runApplication <Application>(* args)行中
- 添加一个HelloWorld控制器,该控制器将向世界返回问候
代号pom.xml<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>habr-sample</artifactId> <version>1.0-SNAPSHOT</version> <repositories> <repository> <id>jcenter</id> <name>jcenter</name> <url>https://jcenter.bintray.com</url> </repository> </repositories> <build> <plugins> <plugin> <groupId>org.jetbrains.kotlin</groupId> <artifactId>kotlin-maven-plugin</artifactId> <version>RELEASE</version> <executions> <execution> <id>compile</id> <phase>compile</phase> <goals> <goal>compile</goal> </goals> </execution> </executions> <configuration> <jvmTarget>1.8</jvmTarget> </configuration> </plugin> </plugins> </build> <dependencies> <dependency> <groupId>org.jetbrains.kotlin</groupId> <artifactId>kotlin-stdlib</artifactId> <version>RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>RELEASE</version> </dependency> </dependencies> </project>
班级 package example import org.springframework.boot.autoconfigure.SpringBootApplication import org.springframework.boot.runApplication @SpringBootApplication open class Application fun main(args: Array<String>) { runApplication<Application>(*args) }
package example import org.springframework.stereotype.Controller import org.springframework.web.bind.annotation.RequestMapping import org.springframework.web.bind.annotation.ResponseBody @Controller class HelloWorld{ @RequestMapping("/") @ResponseBody fun mainPage(): String { return " " } }
添加kotlinx.html
Kotlin具有DSL形式的扩展,可以方便地使用html。 它使您可以将html的声明性方法与通用语言的命令性方法混合使用。
table {// thead{ tr { td { +"" //+ } td { +"" } } } for (person in PersonGenerator().generate()) { // for kotlin tr { td { +person.name } td { text(person.age) // "+" String } } } }
此代码将形成一个html页面,其中将有一个标有人名和年龄的标牌
代号pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>habr-sample</artifactId> <version>1.0-SNAPSHOT</version> <repositories> <repository> <id>jcenter</id> <name>jcenter</name> <url>https://jcenter.bintray.com</url> </repository> </repositories> <build> <plugins> <plugin> <groupId>org.jetbrains.kotlin</groupId> <artifactId>kotlin-maven-plugin</artifactId> <version>RELEASE</version> <executions> <execution> <id>compile</id> <phase>compile</phase> <goals> <goal>compile</goal> </goals> </execution> </executions> <configuration> <jvmTarget>1.8</jvmTarget> </configuration> </plugin> </plugins> </build> <dependencies> <dependency> <groupId>org.jetbrains.kotlin</groupId> <artifactId>kotlin-stdlib-jdk8</artifactId> <version>RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>RELEASE</version> </dependency> <dependency> <groupId>org.jetbrains.kotlinx</groupId> <artifactId>kotlinx-html-jvm</artifactId> <version>RELEASE</version> </dependency> </dependencies> </project>
班级 package example data class Person(val name: String,var age: Int)
package example import java.util.* import kotlin.collections.ArrayList class PersonGenerator{ private val nameList = arrayOf("", "", "", "", "", ""); fun generate(): List<Person> { val random = Random() val personList = ArrayList<Person>() for (i in 1..15) { personList.add(Person(nameList.get(random.nextInt(nameList.size)), random.nextInt(30) + 18)) } return personList; } }
package example import kotlinx.html.* import kotlinx.html.stream.createHTML import org.springframework.stereotype.Controller import org.springframework.web.bind.annotation.RequestMapping import org.springframework.web.bind.annotation.ResponseBody @Controller class HelloWorld { @RequestMapping("/") @ResponseBody fun mainPage(): String { return createHTML() .html { body { table { thead() { tr { td { +"" } td { +"" } } } val personList= PersonGenerator().generate() for (person in personList ) { tr { td { +person.name } td { text(person.age) } } } } } } } }
添加CSS
Kotlinx.html实际上不知道如何使用CSS,您可以使用它添加指向已完成样式的链接或使用不安全的样式插入自己的样式。 如果添加Aza-Kotlin-CSS,一切都会变得更好-DSL与CSS一起工作
head { // bootstrap styleLink("https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css") styleLink("https://cdn.datatables.net/1.10.19/css/dataTables.bootstrap4.min.css") // style("text/css") { unsafe {// unsafe kotlinx.html raw( Stylesheet { // Aza-Kotlin-CSS table and td and th { color = 0xFC0Fc0 } }.render() ) } } meta { charset = "utf-8" } }
使用js也会遇到不安全的情况
总结
我认为,在DSL上编写视图比在JSP上编写更为方便。 我不需要先进入数据库,将计算结果放入一个中间对象,然后将其放入jsp中。 我可以立即绕过不需要的抽象层,从项目代码库中填充我的jsp。 不幸的是,我只在宠物项目中使用过jsp,根本不处理前端。 我想读一下专业人员对此问题的看法。
有用的链接
文章中出现的粉红色标签示例
Kotlin文档
将Spring Boot连接到Kotlin的文档
Kotlinx.html文档
Aza-Kotlin-CSS文档