AJAX ๋ก ์ฌ๋ฌ ๊ฐ ์ ๋ฌํ๊ณ ๋ฐ๊ธฐ
์ด์ ํฌ์คํธ์์ AJAX๋ก ์์ด๋ ์ค๋ณต์ฒดํฌ๋ฅผ ํด๋ณด์๋ค.
๊ทธ๋ ๋ค๋ฉด id ํ๋์ ๊ฐ์ด ์๋ ๋ค์ํ ๊ฐ์ ์ฃผ๊ณ ๋ฐ์ ๋์๋ ์ด๋ป๊ฒ ํด์ผํ๋์ง ์ดํด๋ณด์!
๋จผ์ ๋ก๊ทธ์ธ ํ์ด์ง์์ ์ดํด๋ณผ ์์ ์ธ๋ฐ, ์๋๋ ๋ก๊ทธ์ธ ํ์ด์ง๋ฅผ ajax๋ก ํ ๊ฒฝ์ฐ ๋ณด์์ ๋ฌธ์ ๊ฐ ์๊ธฐ ๋๋ฌธ์ ํ์ง ์์์ผ ํ๋ค! ๋ ์ ์ ๊ธฐ์ตํ๊ณ ์๋ ๋ด์ฉ์ ์ฐธ๊ณ ๋ง ํ๋๋ก ํ์ ๐
html
<body>
<table>
<tr>
<th>ID</th>
<td><input type = "text" name = "id"/></td>
</tr>
<tr>
<th>PW</th>
<td><input type = "password" name = "pw"/></td>
</tr>
<tr>
<th colspan = "2">
<button id="login">LOGIN</button>
<a href = "join.go">ํ์๊ฐ์
</a>
</th>
</tr>
</table>
</body>
java script
<script>
$('#login').on('click', function(){
var id = $('input[name="id"]').val();
var pw = $('input[name="pw"]').val();
$.ajax({
type: 'post',
url: 'login.ajax',
data: {'id':id, 'pw':pw},
dataType: 'JSON',
success: function(data){
if(data.login){
alert(id + '๋ ๋ฐ๊ฐ์ต๋๋ค.');
location.href='list.go';
}else{
alert('์์ด๋ ๋๋ ๋น๋ฐ๋ฒํธ๋ฅผ ํ์ธํด์ฃผ์ธ์.');
}
},error: function(e){
console.log(e);
}
});
});
</script>
function() ์ ์คํํ๋ค.input[name=id|pw] ์ธ ์์์ value๋ฅผ ๊ฐ์ ธ์ ๋ณ์์ ๋ด์์ค๋ค.login ๋ฉ์๋์ ๊ฐ์ด true ์ด๋ฉด list.go ๋ฉ์๋๋ก ์ฐ๊ฒฐํด์ฃผ๊ณ , false ์ผ ๊ฒฝ์ฐ ์๋ฆผ์ ๋ณด๋ธ๋ค.@PostMapping(value= "/login.ajax")
@ResponseBody
public Map<String, Object> login(String id, String pw, HttpSession session) {
Map<String, Object> result = new HashMap<String, Object>();
if(member_service.login(id, pw)) {
result.put("login", true);
session.setAttribute("loginId", id);
}else {
result.put("login", false);
}
return result;
}
@PostMapping ์ด๋
ธํ
์ด์
์ ์ธ (RequestMapping ๋ฅผ ์จ๋ ๋๋ค.)@ResponseBody ์ด๋
ธํ
์ด์
์ ์ธservice
public boolean login(String id, String pw) {
boolean success = false;
if(member_mapper.login(id,pw)>0){
success = true;
}
return success;
}
dto
int login(String id, String pw);
mapper
<select id="login" resultType="int">
SELECT COUNT(id) FROM member WHERE id = #{param1} AND pw = #{param2}
</select>
์ด๋ฒ์๋ ๋ ๋ง์ ๊ฐ์ ๋ฐ๋ ์ฝ๋๋ฅผ ์ดํด๋ณด์.
๋ก๊ทธ์ธ์ ์ฑ๊ณตํ ๊ฒฝ์ฐ ํ์์ ๋ณด๋ฅผ ํ์ธํ ์ ์๋ list.jsp ๋ก ์ด๋ํ๋ฉฐ, list.jsp ์๋ ํ์์ ์ด๋ฆ, ๋์ด, ์ฑ๋ณ, ์ด๋ฉ์ผ์ด ๋
ธ์ถ๋๋ค.
html
<button onclick="location.href='insert.go'">์
์ฌ์ ๋ฑ๋ก</button>
<table>
<thead>
<tr>
<th>ID</th> <th>์ด๋ฆ</th> <th>๋์ด</th> <th>์ด๋ฉ์ผ</th>
</tr>
</thead>
<tbody id="list"></tbody>
</table>
java script
<script>
$.ajax({
type: 'GET',
url: 'list.ajax',
data: {}, // ๋ณด๋ด๋ ํ๋ผ๋ฏธํฐ๊ฐ ์์ ๊ฒฝ์ฐ ๋น ์ํ๋ก ๋ณด๋ธ๋ค.
dataTyle: 'JSON',
success: function(data){
console.log(data);
if(data.login){
drawList(data.list);
}else{
alert('๋ก๊ทธ์ธ์ด ํ์ํ ์๋น์ค์
๋๋ค.');
location.href='./';
}
}, error: function(e){
console.log(e);
}
});
function drawList(list){
var content = '';
list.forEach(function(item,idx){
content += '<tr>';
content += '<td>'+item.id+'</td>';
content += '<td>'+item.name+'</td>';
content += '<td>'+item.age+'</td>';
content += '<td>'+item.email+'</td>';
content += '</tr>';
console.log(item.id, item.age, item.name, item.email);
});
$('#list').html(content);
}
</script>
success ์์ login ์ฌ๋ถ์ ๋ฐ๋ผ list๋ฅผ ๋ณด์ฌ์ฃผ๊ฑฐ๋ or ๋ก๊ทธ์ธ ํ์ด์ง๋ก ๋๋ ค๋ณด๋ด์ค๋ค.forEach ๋ฅผ ์ฌ์ฉํด ๋ฐ์ดํฐ๊ฐ ์์ ๋๊น์ง ๊ณ์ํด์ ์ถ๊ฐํด์ค๋ค.@GetMapping(value="/list.go")
public String list() {
return "list";
}
@GetMapping(value="/list.ajax")
@ResponseBody
public Map<String, Object> list(HttpSession session){
Map<String, Object> map = new HashMap<String, Object>();
if(session.getAttribute("loginId") != null) {
map.put("login", true);
List<HashMap<String, Object>> list = member_service.list();
map.put("list", list);
}else {
map.put("login", false);
}
return map;
}
@GetMapping ์ด๋
ธํ
์ด์
์ ์ธ@ResponseBody ์ด๋
ธํ
์ด์
์ ์ธservice
public List<HashMap<String, Object>> list() {
return member_mapper.list();
}
dto
List<HashMap<String, Object>> list();
mapper
<select id="list" resultType="map">
SELECT id, name, age, email FROM member
</select>
* ๋ก ๋ฐ์ดํฐ๋ฅผ ๋๊ธธ ๊ฒฝ์ฐ ๋ชจ๋ ๋ฐ์ดํฐ๋ฅผ ๋ค ๋ณด์ฌ์ฃผ๊ฒ ๋๊ธฐ ๋๋ฌธ์ ๋ณด์ฌ์ค ์ปฌ๋ผ๋ง ์ง์ ํด์ค์ผ ํ๋ค.