Spring Framework ์ ํ์ด์ฌ์ ์ฌ์ฉํ์ฌ ์๋์ ๊ธฐ๋ฅ์ ์์ฑํ๋ค
CrawlingTestController
๊ฐ๋จํ๊ฒ ๋ก๊ทธ์ธ ์ฒ๋ฆฌ๋ฅผ ์ํ login ๋ฉ์๋
1.์น๋ธ๋ผ์ฐ์ ์์ ๋ ์จ๋ณด๊ธฐ ๋ฒํผ์ ๋๋ฅด๋ฉด ajax์์ฒญ์ด /craw/weather์ผ๋ก ์ ๋ฌ๋๋ค
๋ก๊ทธ์ธ์ ๊ฑฐ์น ๊ฒฝ์ฐ์๋ง ๋ ์จ๋ณด๊ธฐ ๋ฒํผ์ด ์๋ํ๋ค
ajax ์์ฒญ์ ๋ํ ์๋ต
{title:๋ ์จ์ ๋ณด,date:2023.02.15,'weather:๊ตฌ๋ฆ์ด๋ง๊ณ , ์คํ์ ๋ง์์ง}
2. ํ๋ฐํธ์๋์์๋ ์๋ฐ์คํฌ๋ฆฝ์ ์ฌ์ฉํ์ฌ ์์ ์๋ต์ ์๋์ฒ๋ผ ํ๋ฉด์ ํ์ํ๋ค
<div>
<h3>๋ ์จ์ ๋ณด</h3>
<div class='date'>2023.02.15</div>
<div class='weather'>๊ตฌ๋ฆ์ด ๋ง๊ฒ ๊ณ , ์คํ์ ๋ง์์ง</div>
</div>
3. ์์ ๊ฐ์ ์น์ฌ์ดํธ์์ ๋ ์จ์ ๋ณด๋ฅผ ๊ฐ์ ธ์ฌ ์ ์๋ ํ์ด์ฌ ์์ง ํ๋ก๊ทธ๋จ ์์ฑ
ํ๋ฉด์ ์ ๋ชฉ, ๋ ์ง, ๋ ์จ ์ ๋ณด๋ฅผ ํ์ํ๋ค
์๋ฐ-์ดํด๋ฆฝ์ค ํํธ
CrawlingTestController.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
import java.util.HashMap;
import java.util.Map;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import jakarta.servlet.http.HttpSession;
@Controller
@RequestMapping("/crawl")
public class CrawlingTestController {
@GetMapping("/login")
public String loginForm()
{
return "thymeleaf/loginForm";
}
@PostMapping("/loginResult")
@ResponseBody
public Map<String,Boolean> loginResult(@RequestParam("id") String id, @RequestParam("pw") String pw, HttpSession session)
{
if(id.equals("smith")&&pw.equals("1234")) {
session.setAttribute("login", id);
Map<String,Boolean> map = new HashMap<>();
map.put("login", true);
return map;
}
return null;
}
@GetMapping("/weather")
public String weather(HttpSession session)
{
Object obj = session.getAttribute("login");
if(obj==null) {
return null;
}
return "thymeleaf/weather";
}
}
|
cs |
loginForm.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>๋ก๊ทธ์ธ ํผ</title>
<style type="text/css">
@import url(//fonts.googleapis.com/earlyaccess/jejumyeongjo.css);
.jm-font{
font-family: 'Jeju Myeongjo', cursive;/*์น ํฐํธ ์ง์ */
color: black;
font-size:3em;}
#id, #pw{
font-family: 'Jeju Myeongjo', serif;
color: black;
font-size:1.5em;}
.wrapper {
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;}
main {width:fit-content; margin:1em auto; }
main h3{ text-align: center;}
form {border:1px solid black; padding:1em;}
label {display:inline-block; width:3em; text-align: right; padding-right:1em; }
div.btn { width:fit-content; margin: 0.5em auto; }
</style>
<script src="https://code.jquery.com/jquery-3.6.1.min.js"
integrity="sha256-o88AwQnZB+VDvE9tvIXrMQaPlFFSUTR+nldQm1LuPXQ=" crossorigin="anonymous">
</script>
<script type="text/javascript">
function getLogin()
{
var obj = $('#loginForm').serialize();
$.ajax({
url : '/crawl/loginResult',
method:'post',
data : obj,
cache:false,
dataType:'json',
success:function(res){
alert(res.login ? '๋ก๊ทธ์ธ ์ฑ๊ณต':'๋ก๊ทธ์ธ ์คํจ');
if(res.login){
location.href="/crawl/weather"
}
},
error : function(xhr, status, err){
alert('์๋ฌ:' + err);
}
});
return false;
}
</script>
</head>
<body style="background-color: rgb(227,209,254);">
<div class="wrapper" >
<main class="main-font">
<h3 class="jm-font">๐ง๐ป๋ก๊ทธ์ธ</h3>
<form id="loginForm" onsubmit="return getLogin();">
<div id ="id"><label for="userid">์์ด๋</label>
<input type="text" id="userid" name="id" style="height:25px">
</div>
<p>
<div id="pw"><label for="userpwd">์ํธ</label>
<input type="password" id="userpwd" name="pw" style="height:25px">
</div>
<div class="btn">
<button type="reset">์ทจ์</button>
<button type="submit">๋ก๊ทธ์ธ</button>
</div>
</form>
</main>
</div>
</body>
</html>
|
cs |
weather.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>์ค๋์ ๋ ์จ</title>
</head>
<style type="text/css">
#weatherInfo{
display:none;
}
</style>
<script type="text/javascript">
function showWeather(){
var weatherInfo = document.getElementById("weatherInfo");
weatherInfo.style.display="block";
}
</script>
<body>
<button type="button" onclick="javascript:showWeather();">๋ ์จ๋ณด๊ธฐ</button>
<div id="weatherInfo">
<h3>๋ ์จ์ ๋ณด</h3>
<div class="date">2023.02.15</div>
<div class="weather">๊ตฌ๋ฆ์ด ๋ง๊ฒ ๊ณ , ์คํ์ ๋ง์์ง</div>
</div>
</body>
</html>
|
cs |
์คํ๊ฒฐ๊ณผ :
ํ์ด์ฌ-์ฅฌํผํฐ ๋ ธํธ๋ถ ํํธ
๊ฒฐ๊ณผ :
json ๋ชจ๋
์คํ๊ฒฐ๊ณผ :
์คํ๊ฒฐ๊ณผ :
๋๊ธ