실습(1)
- [Web_Spring] 06 이어서
- 문제
/*
* 아이디와 비밀번호를 입력받은 후 아이디가 admin일 경우 admin.html로 이동
* 아이디가 user일 경우 user.html로 이동
*
* - goLogin.html : 아이디와 비밀번호 입력 페이지 출력
* - admin.html : 관리자 페이지 출력
* - user.html : 일반 회원 페이지 출력
*/
1. src/main/resource/templates/ex/goLogin.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/goLogin" 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/ex/admin.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>admin</title>
</head>
<body>
<h1>관리자 페이지</h1>
</body>
</html>
3. src/main/resource/templates/ex/user.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>user</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("goLogin")
public void goLogin(){}
@PostMapping("goLogin")
public String goLogin(UserVO userVO){
if(userVO.getId().equals("admin")){
return "/ex/admin";
}
return "/ex/user";
}
실습(2)
- [Web_Spring] 06 이어서
- 문제
/*
* 이름을 입력하고 출근 또는 퇴근 버튼을 클릭한다.
* 출근 시간은 09:00이며, 퇴근 시간은 17:00이다.
* 출근 버튼 클릭 시 9시가 넘으면 지각으로 처리하고,
* 퇴근 버튼 클릭 시 17시 전이라면 퇴근이 아닌 업무시간으로 처리한다.
*
* - getToWork.html
* - leaveWork.html
* - late.html
* - work.html
*
* */
1. src/main/resource/templates/work/checkIn.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>check in</title>
</head>
<body>
<form action="" name="checkIn">
<div>
이름 <input type="text" name="name">
</div>
<div>
<input type="button" value="출근하기" id="getToWork">
<input type="button" value="퇴근하기" id="leaveWork">
</div>
</form>
</body>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
let form = document.checkIn;
$("#getToWork").on("click", function(){
$(form).attr("action", "/ex/getToWork");
form.submit();
});
$("#leaveWork").on("click", function(){
$(form).attr("action", "/ex/leaveWork");
form.submit();
});
</script>
</html>
2. src/main/resource/templates/work/getToWork.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>get to work</title>
</head>
<body>
<h1 th:text="|${name}님 어서오세요!|"></h1>
</body>
</html>
3. src/main/resource/templates/work/leaveWork.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>leave work</title>
</head>
<body>
<h1 th:text="|${name}님 고생하셨습니다.|"></h1>
</body>
</html>
4. src/main/resource/templates/work/work.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>work</title>
</head>
<body>
<h1 th:text="|${name}님 아직 근무시간입니다.|"></h1>
</body>
</html>
5. src/main/resource/templates/work/late.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>late</title>
</head>
<body>
<h1 th:text="|${name}님 지각입니다.|"></h1>
</body>
</html>
6. src/main/java/com.example.ex02/controller/ExampleController.java
@GetMapping("checkIn")
public String checkIn(){
return "/work/checkIn";
}
@GetMapping("/getToWork")
public String getToWork(@ModelAttribute("name") String name){
Calendar now = Calendar.getInstance();
int hour = now.get(Calendar.HOUR_OF_DAY);
int minute = now.get(Calendar.MINUTE);
boolean lateCondition = hour >= 9 && minute > 0;
return lateCondition ? "/work/late" : "/work/getToWork";
}
@GetMapping("/leaveWork")
public String leaveWork(@ModelAttribute("name") String name){
Calendar now = Calendar.getInstance();
int hour = now.get(Calendar.HOUR_OF_DAY);
int minute = now.get(Calendar.MINUTE);
boolean leaveWorkCondition = hour >= 17 && minute >= 0;
return leaveWorkCondition ? "/work/leaveWork" : "/work/work";
}
@GetMapping("test")
public void test(){
}
@PostMapping("test")
public String test(String name, Model model){
model.addAttribute("name", name);
return "result";
}
'웹 개발 > Spring' 카테고리의 다른 글
[Web_Spring] 09 (0) | 2022.06.21 |
---|---|
[Web_Spring] 08 (0) | 2022.06.20 |
[Web_Spring] 06 (0) | 2022.06.18 |
[Web_Spring] 05 (0) | 2022.06.17 |
[Web_Spring] 03 (0) | 2022.06.15 |