Kotlin으로 Spring Boot 개발하기 - sign, login 화면

Yuri Lee·2020년 9월 21일
0

IntelliJ 코드 변경 후 자동 빌드 기능 활성화

  • Setting -> Build, Execution, ... -> Compiler -> Builder project automatically
  • ctrl+shift+a -> registry 입력
  • compiler.automake.allow.when.app.running 체크

고급 url 매핑
url: /post/({1,2,3,...}

url에서 클라이언트가 요청한다. url에서 어노테이션을 변수를 받아오는 방법이 있다.

  • @PathVariable
  • @GetMapping

http 통신은 get과 post 방식이 있다. 클라이언트가 어떤 것을 선택했을 때 정보가 나오는 것을 get 이라고 한다. 하지만 비밀번호와 같은 경우는 get 방식을 사용하면 주소창에 나오게 되므로 보안상 매우 치명적이게 된다. 이럴때는 post 방식을 활용한다.

@Controller
class HtmlController {

    @RequestMapping("/")
    fun index(model:Model) : String {
        return "index"
    }
}

@RequestMapping를 @GetMapping으로 바꾸면 get 요청으로 들어온 url만 처리할 수 있게 된다.

대부분 웹 페이지를 가져올 때는 html을 가져올 때는 get 방식을 사용하고 form 에서 data를 전송할 때는 post 방식을 사용한다. 같은 url에도 get는 html을 가져오고 post는 데이터를 전송하는 식으로 구분을 한다.

Sign, Login 페이지

  • sign 화면 개발과 url 매핑
  • mustache 문법을 이용한 html 중첩 제거
  • login 화면 개발과 url 매핑
  • @PathVariable을 이용한 url 매핑 중복 제거

화면개못생김, 나중에 고쳐야지ㅠ

header, footer가 중복되므로 추가해주었다.

package com.example.demo

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

@Controller
class HtmlController {

    @RequestMapping("/")
    fun index(model:Model) : String {
        model.addAttribute("title","Home")
        return "index"
    }

    @GetMapping("/{formType}")
    fun htmlForm(model: Model, @PathVariable formType:String) : String {

        var response : String=""

        if (formType.equals("sign")) {
            response="sign"
        }
        else if (formType.equals("login")) {
            response="login"
        }

        model.addAttribute("title", response)

        return response
    }
}

[Reference]

이 글은 유투버 kwangsung choi의 코틀린 스프링부트 강좌를 바탕으로 정리한 내용입니다.

profile
Step by step goes a long way ✨

0개의 댓글