controller 패키지 추가

SongController 추가

package kr.ac.kumoh.s20230000.w24w05thymeleaf.controller

import org.springframework.stereotype.Controller
import org.springframework.web.bind.annotation.GetMapping

@Controller
class SongController {
    @GetMapping("/song/random")
    fun getRandomSong(): String {
        return "random"
    }
}

random.html 추가

<!DOCTYPE html>
<html lang="ko">
<head>
    <meta charset="UTF-8">
    <title>Thymeleaf</title>
</head>
<body>
<h2>추천 노래</h2>
<p>테스형</p>
</body>
</html>

web에서 확인 localhost:8080/song/random

Model 사용하여 attribute 전달

package kr.ac.kumoh.s20230000.w24w05thymeleaf.controller

import org.springframework.stereotype.Controller
import org.springframework.ui.Model
import org.springframework.web.bind.annotation.GetMapping

@Controller
class SongController {
    @GetMapping("/song/random")
    fun getRandomSong(model: Model): String {
        model.addAttribute("songTitle", "소주 한 잔")
        return "random"
    }
}

random.html

<!DOCTYPE html>
<html lang="ko">
<head>
    <meta charset="UTF-8">
    <title>Thymeleaf</title>
</head>
<body>
<h2>추천 노래</h2>
<p>[[${songTitle}]]</p>
</body>
</html>

여기에 model repository service 추가

model

package kr.ac.kumoh.s20230000.w24w05thymeleaf.model

data class Song(
    var id: Int,
    var title: String,
    var singer: String,
)

Repository

package kr.ac.kumoh.s20230000.w24w05thymeleaf.repository

import kr.ac.kumoh.s20230000.w24w05thymeleaf.model.Song
import org.springframework.stereotype.Repository

@Repository
class SongRepository {
    protected val songs = listOf(
        Song(1, "사랑에 연습이 있었다면", "임재현"),
        Song(2, "테스형", "나훈아"),
        Song(3, "소주 한 잔", "임창정"),
    )

    val songsSize: Int
        get() = songs.size

    fun getSong(index: Int) = songs[index]
    
    fun fetchSong() = songs
}

Service