본문 바로가기

웹 개발/Spring

[Web_Spring] 08

실습(3)

- [Web_Spring] 06 이어서

 

 

- 문제

//    아이디 : apple
//    비밀번호 : banana
//    로그인 성공 시 apple님 환영합니다.
//    로그인 실패 시 로그인 실패

 

 

 

1. src/main/resource/templates/login/login.html

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>login</title>
</head>
<body>
<form action="/ex/login" method="post">
    <div>
        <input type="text" name="id" placeholder="아이디">
    </div>
    <div>
        <input type="password" name="pw" placeholder="비밀번호">
    </div>
    <div> 
        <input type="submit" value="로그인">
    </div>
</form>
</body>
</html>

 

 

 

2. src/main/resource/templates/login/success.html

<!doctype html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>success</title>
</head>
<body>
<h1 th:text="${id} + '님 환영합니다.'"></h1>
</body>
</html>

 

 

 

3. src/main/resource/templates/login/fail.html

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>fail</title>
</head>
<body>
<h1>로그인 실패</h1>
</body>
</html>

 

 

 

4. src/main/java/com.example.ex02/domain.vo/UserVO.java

package com.example.ex02.domain.vo;

import lombok.Data;
import org.springframework.stereotype.Component;

@Component
@Data
public class UserVO {
    private String id;
    private String pw;
}

 

 

 

5. src/main/java/com.example.ex02/controller/ExampleController.java

@GetMapping("login")
public String login(){
    return "/login/login";
}

@PostMapping("login")
public String login(UserVO userVO, Model model){
    if(userVO.getId().equals("apple")){
        if(userVO.getPw().equals("banana")){
            model.addAttribute("id", userVO.getId());
            return "/login/success";
        }
    }
    return "/login/fail";
}

 

 

 

 

실습(3)

- [Web_Spring] 06 이어서

 

 

- 문제

//    노래방 기계 제작
//    사용자의 점수에 따른 알맞는 메세지 출력

 

 

 

1. src/main/resource/templates/songbox/songbox.html

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>songbox</title>
</head>
<body>
<form action="/ex/songbox/" method="post">
    <select name="score">
        <option value="10">10점</option>
        <option value="20">20점</option>
        <option value="30">30점</option>
        <option value="40">40점</option>
        <option value="50">50점</option>
        <option value="60">60점</option>
        <option value="100">100점</option>
    </select>
    <input type="submit" value="등록">
</form>
</body> 
</html>

 

 

 

2. src/main/resource/templates/songbox/result.html

<!doctype html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>result</title>
</head>
<body>
<h1 th:text="${result}"></h1>
</body>
</html>

 

 

 

3. src/main/java/com.example.ex02/controller/ExampleController.java

@GetMapping("/songbox")
public String songbox(){
    return "/songbox/songbox";
}

@PostMapping("/songbox")
public String checkScore(Integer score, Model model){
    if(score < 50){
        model.addAttribute("result", "연습이 부족해요!");
        return "/songbox/result";
    }

    model.addAttribute("result", "훌륭해요!");
    return "/songbox/result";
}

 

 

 

 

'웹 개발 > Spring' 카테고리의 다른 글

[Web_Spring] 10  (0) 2022.06.22
[Web_Spring] 09  (0) 2022.06.21
[Web_Spring] 07  (0) 2022.06.19
[Web_Spring] 06  (0) 2022.06.18
[Web_Spring] 05  (0) 2022.06.17