1. home.html
<!DOCTYPE html>
<html lang="ko" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>홈화면</title>
</head>
<body>
관리자 홈화면
<a th:href="@{/admin/home.do(menu=1)}"><button>회원관리</button></a>
<a th:href="@{/admin/home.do(menu=2)}"><button>게시물관리</button></a>
<hr />
<div th:if="${menu == 1}">
회원관리
<form th:action="@{/admin/스크립트에서변경됨.do}" method="post" id="form">
<table border="1">
<tr th:each="obj, idx : ${list}">
<td><input type="checkbox" name="chk" th:value="${obj.userid}" /></td>
<td th:text="${obj.userid}"></td>
<td th:text="${obj.age}"></td>
<td th:text="${obj.phone}"></td>
<td th:text="${obj.gender}"></td>
<td th:text="${obj.regdate}"></td>
<td th:text="${obj.block}"></td>
</tr>
</table>
<input type="button" value="회원차단"
th:onclick="|javascript:handleBlock(1)|"/>
<input type="button" value="회원해제"
th:onclick="|javascript:handleBlock(2)|"/>
</form>
</div>
<div th:if="${menu == 2}">
게시물관리
</div>
<script>
const handleBlock = (type) => {
const form = document.getElementById('form');
if(type == 1){
form.action="[[@{/admin/memberblock.do(type=1)}]]";
}
else if(type==2){
form.action="[[@{/admin/memberblock.do(type=2)}]]";
}
form.submit();
}
</script>
</body>
</html>
2. AdminController.java
package com.example.controller;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
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 com.example.dto.MemberDTO;
import com.example.mapper.MemberMapper;
@Controller
@RequestMapping(value="/admin")
public class AdminController {
@Autowired MemberMapper memberMapper;
@PostMapping(value="/memberblock.do")
public String memberBlockPOST(
@RequestParam(name="type") int type,
@RequestParam(name="chk") List<String> list){
System.out.println( type );
System.out.println(list.toString());
Map<String, Object> map = new HashMap<>();
if(type == 1) {
map.put("chk", 0); // BLOCK값을 0으로 변경
}
else if(type == 2) {
map.put("chk", 1); // BLOCK값을 1로 변경
}
map.put("list", list);
memberMapper.updateMemberBlock(map);
return "redirect:/admin/home.do?menu=1";
}
@GetMapping(value="/home.do")
public String homeGET(Model model,
@RequestParam(name="menu", defaultValue = "0") int menu){
if(menu == 0) { //menu정보가 없다면 menu=1로 변경함.
return "redirect:/admin/home.do?menu=1";
}
if(menu == 1) {
List<MemberDTO> list = memberMapper.selectMemberList();
model.addAttribute("list", list);
}
else if(menu == 2) {
}
model.addAttribute("menu", menu);
return "admin/home";
}
}
3. MemberMapper.java
public int updateMemberBlock(Map <String ,Object> map);
4. memberMapper.xml
<!--foreach 는 반복문-->
<update id="updateMemberBlock" parameterType="map">
UPDATE MEMBERTBL SET
BLOCK = (CASE
<foreach collection="list" item="tmp" separator=" ">
WHEN USERID=#{tmp} THEN #{chk}
</foreach>
END)
WHERE USERID IN(
<foreach collection="list" item="tmp" separator=", ">
#{tmp}
</foreach>
);
</update>