Now, in this post, i will demo integrated spring boot with spring mvc.
Ok, let's get started.
Step1: create spring boot project with STS
In this post, i am going to demo spring boot using tomcat (embed) and thymeleaf template engine.<?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.shine2share</groupId>
<artifactId>springboot_demo1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springboot_demo1</name>
<description>Building project by spring boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
package com.shine2share;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringbootDemo1Application {
public static void main(String[] args) {
SpringApplication.run(SpringbootDemo1Application.class, args);
}
}
package com.shine2share.controller;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapping;@Controller public class HomeController { @RequestMapping("/")model.addAttribute("name", "Spring Developer.");public String sayHello(Model model) { return "index"; }}

package com.shine2share fun printName(name: String): String { return name }
package com.share2study import com.shine2share.printName fun myFun(): String { return printName("Dycky") }
// lambda expression with no param val xinChao = {println("Xin chao! Toi la Kotlin Dev.")} fun main(args: Array<String>) { xinChao() }
// lambda expression with 1 param val xinChao = {name: String -> println("Xin chao! $name la Kotlin Dev.")} fun main(args: Array<String>) { xinChao("Ducky") }
val calSum = {num1: Int, num2: Int -> println("Sum of num1 and num2 is: " + (num1+num2))} fun main(args: Array<String>) { calSum(1,2) }
fun main(args: Array<String>) { println(stringList.last()) // "Hola" println(stringList.last({ s: String -> s.length == 8})) // "Xin chao"} val stringList: List<String> = listOf("Hello", "Xin chao", "Hola")
println(stringList.last{ s: String -> s.length == 8}) // "Xin chao"
fun main(args: Array<String>) { println(stringList.last{ it.length == 8}) // "Xin chao"} val stringList: List<String> = listOf("Hello", "Xin chao", "Hola")
fun main(args: Array<String>) { printName() } fun printName() { val intList = listOf(1, 2, 3, 4, 5) intList.forEach { if (it % 2 == 0) { return } } println("Ket thuc ham printName()") }
println("Ket thuc ham printName()")sẽ ko in ra theo mong muốn.
intList.forEach { if (it % 2 == 0) { return@forEach } }
fun printName() { val intList = listOf(1, 2, 3, 4, 5) intList.forEach myLabel@{ if (it % 2 == 0) { return@myLabel } } println("Ket thuc ham printName()") }
class Circle { fun calArea(radius: Double): Double { return Math.PI * Math.pow(radius, 2.0) } }
fun main(args: Array<String>) { val circle = Circle() println("Dien tich hinh tron la: " + circle.calArea(3.0)) }
fun sayHello(name: String) : String { return "Hello " + name }
fun sayHello(name: String) : String { return "Hello $name"}
fun main(args: Array<String>) { val result: String = sayHello("Viet nam") println(result) }
fun print(name: String): Unit { println(name) }
fun getArea(width: Long, height: Long): Long { return width*height }
fun getArea(width: Long, height: Long) = width*height
fun getArea(width: Long, height: Long): Long = width*height
getArea(height = 14, width = 12)
fun getArea(width: Long, height: Long = 10): Long = width*height
getArea(12)
@JvmOverloadsfun getArea(width: Long, height: Long = 10): Long = width*height
long getArea(long width);
fun sayHello(vararg names: String) { for(name in names) { println("Hello $name") } }
sayHello("xin chao", "hello", "hola")
fun returnTwoValue(id: Int): Pair<Int?, Int?> { val values1: Map<Int, Int> = mapOf(1 to 100, 2 to 200, 3 to 300) val values2: Map<Int, Int> = mapOf(1 to 1000, 2 to 2000, 3 to 3000) val value1 = values1[id] val value2 = values2[id] return Pair(value1, value2) }
fun returnTwoValue(id: Int): Pair<Int?, String?> { val values1: Map<Int, Int> = mapOf(1 to 1, 2 to 2) val values2: Map<Int, String> = mapOf(1 to "Hanoi", 2 to "Saigon") val value1 = values1[id] val value2 = values2[id] return Pair(value1, value2) }
println(returnTwoValue(1).first) println(returnTwoValue(1).second)
fun getNameAddressAndAge(id: Int): Triple<String?, String?, Int?> { val Names: Map<Int, String> = mapOf(1 to "My", 2 to "Nam") val Addresses: Map<Int, String> = mapOf(1 to "Hanoi", 2 to "Saigon") val Ages: Map<Int, Int> = mapOf(1 to 12, 2 to 22) return Triple(Names[id], Addresses[id], Ages[id]) }
println(getNameAddressAndAge(1).first) println(getNameAddressAndAge(1).second) println(getNameAddressAndAge(1).third)
Sử dụng dấu .. giữa 2 số này.for(a in five2One) print(a)
if( 1 in five2One) { print("1 in five2One") }
val name: String = null // will not compile
Nếu bạn cố tính muốn gán giá trị null thì thêm dấu ? sau kiểu data, như sau:val name: String? = null // will compilename = "Nguyen The Lam" // new value is "Nguyen The Lam"val name: String? = nullprint(name?.length) // will compile and print "null"val name: String? = nullval len: Int? = name?.lengthprint(len) // will compile and print "null"val name: String? = nullval len: Int? = name!!.lengthprint(len) // NullPointerException errorval fullName = nullval name: String = fullName?: "Ducky"print(name) // will compile and print "Ducky"for (value in range) { // Execute code}for (number in 1..9) { print("$number ") // will print 1 2 3 4 5 6 7 8 9}val numbers = intArrayOf(1,2,3,4,5)for ((index, value) in numbers.withIndex()) { print("$index index value is $value\n")}0 index value is 11 index value is 22 index value is 33 index value is 44 index value is 5val numbers = intArrayOf(1,2,3,4,5)for (index in numbers.indices) { print("$index index value is ${numbers[index]}\n")}0 index value is 11 index value is 22 index value is 33 index value is 44 index value is 5val check= 10if (check % 2 == 0) { print("$check chia hết cho 2") // 10 chia hết cho 2}if (number is Int) { print("$number is an integer")}val number = 3if (number % 2 == 0) { print("$number chia hết cho 2")} else { print("$number ko chia hết cho 2") // 3 ko chia hết cho 2}val number = 3val result = if (number % 2 == 0) { "$number chia hết cho 2"} else { "$number ko chia hết cho 2"}print(result) // 3 ko chia hết cho 2val result = if (number % 2 == 0) { "Divisible by 2" "Number is $number" // Nếu thực thi thì chỉ string này được trả về} else { "Not divisible by 2" "Number is $number"}fun guessTheNumber(number: Int) { when (number) { 1 -> println("number is 1") 2 -> println("number is 2") 3 -> println("number is 3") else -> println("number is neither 1, 2 or 3") }}fun guessTheNumber(number: Int) { when { number == 1 -> println("number is 1") number == 2 -> println("number is 2") number == 3 -> println("number is 3") else -> println("number is neither 1, 2 or 3") }}val number = 2when (number) { 1 -> println("number is 1") 2 -> { println("number is 2") println("it is an even number") } 3 -> println("number is 3")}val number = 2when (number) { 1, 2 -> println("number is either 1 or 2") // lệnh này sẽ đc thực thi. (hoặc là 1 hoặc là 2) 3 -> println("number is 3")}