일요일.. 모레 있을 과제를 위해 밤을 불태운다.
여행 프로젝트 사이트에서 User 부분을 맡아서 만들었다.
Controller 와 Myinfo 부분에서 프로필 사진 아래에 grid 를 만들어서
내가 올린 정보들이 뜨도록 했다.
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import="Domain.Common.Dto.*"%>
<%@page import = "java.util.*" %>
<!DOCTYPE html>
<html>
<head>
<!-- link -->
<%@include file="/resources/static/jsp/link.jsp" %>
<script>
const path = "<%=request.getContextPath() %>";
console.log("Path : " + path);
</script>
<!-- myinfo.js -->
<script src="${pageContext.request.contextPath}/resources/static/js/user/myinfo.js" defer></script>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
UserDto userDto = (UserDto)request.getAttribute("userDto");
%>
<div class="wrapper">
<header>
<!-- topHeader -->
<%@include file="/resources/static/jsp/topHeader.jsp" %>
<!-- nav -->
<%@include file="/resources/static/jsp/nav.jsp" %>
</header>
<main class="layout">
<form style="width:400px;margin:30px auto;" method="POST">
<div class="m-3">
<label for="">USERID : </label>
<input type="text" class="form-control" name="userid" readonly value="${userDto.getUserid()}"/>
</div>
<div class="m-3">
<label for="">PASSWORD : </label>
<input type="password" class="form-control" name="password" value="${userDto.getPassword()}"/>
</div>
<div class="m-3">
<label for="">ROLE : </label>
<input type="text" class="form-control" name="role" readonly value="${userDto.getRole()}"/>
</div>
<div class="m-3">
<label for="">age : </label>
<input type="text" class="form-control" name="age" value="${userDto.getAge()}"/>
</div>
<div class="m-3">
<label for="">성별 : </label>
<select class="form-select" name="gender">
<option value="M" <%= userDto.getGender().equals("M") ? "selected" : "" %> >Male </option>
<option value="F" <%= userDto.getGender().equals("F") ? "selected" : "" %> >Female</option>
</select>
</div>
<div class="m-3">
<button formaction="${pageContext.request.contextPath}/user/myinfo?userid=<%=userDto.getUserid()%>" class="btn btn-success me-2">수정요청</button>
<button formaction="${pageContext.request.contextPath}/" class="btn btn-secondary">이전으로</button>
</div>
</form>
<div class="myPlan">
<table>
<thead>
<tr>
<th scope="col">정보1</th>
<th scope="col">정보2</th>
<th scope="col">정보3</th>
</tr>
</thead>
<tbody>
<%List<PlannerDto> plannerDto = (ArrayList<PlannerDto>)request.getAttribute("plannerDto"); %>
<%int totalCells = 9; // 3x3 테이블 고정
int plannerCount = plannerDto != null && !plannerDto.isEmpty() ? plannerDto.size() : 0; //Dto가 비어있다면 0
int index = 0; //각 칸에 들어갈 plannerID INDEX
for(int row=0;row<3;row++){
%>
<tr>
<%for(int col=0;col<3;col++){
%>
<td>
<%
if(index < plannerCount){
PlannerDto planner = plannerDto.get(index);
index++;
%>
<a href="${pageContext.request.contextPath }/user/detailPlan?plannerId=<%=planner.getPlannerid() %>">
Planner ID : <%=planner.getPlannerid() %><br>
</a>
</td>
<% }else{ //planner 개수가 부족할 경우 빈 셀
%>
<!-- 빈 셀 -->
<% } %>
<% } %>
</tr>
<% } %>
</tbody>
</table>
</div>
</main>
<!-- footer -->
<%@include file="/resources/static/jsp/footer.jsp" %>
</div>
</body>
</html>
package Controller.User;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import Controller.SubController;
import Domain.Common.Dto.PlannerDto;
import Domain.Common.Dto.UserDto;
import Domain.Common.Service.PlannerServiceImpl;
import Domain.Common.Service.UserServiceImpl;
public class UserMyInfoController implements SubController {
private UserServiceImpl userService;
private PlannerServiceImpl plannerService;
public UserMyInfoController() throws ServletException {
try {
this.userService = UserServiceImpl.getInstance();
this.plannerService = PlannerServiceImpl.getInstance();
} catch (Exception e) {
// TODO Auto-generated catch block
ExceptionHandler(e,null,null);
//예외 처리 함수로 던지기
}
}
public void ExceptionHandler(Exception e,HttpServletRequest req,HttpServletResponse resp) throws ServletException{
try {
req.setAttribute("exception", e);
req.getRequestDispatcher("/WEB-INF/view/user/error.jsp").forward(req, resp);
}catch(Exception ex) {
throw new ServletException(ex);
}
}
@Override
public void execute(HttpServletRequest req, HttpServletResponse resp) {
try {
//Method==GET -> 페이지 표시(Forwarding)
String method = req.getMethod();
if("GET".equals(method)) {
System.out.println("[BC] GET /myinfo..");
HttpSession session = req.getSession();
//userDto
UserDto userDto = (UserDto)session.getAttribute("userDto");
req.setAttribute("userDto", userDto);
//PlannerDto조회
Map<String,Object> plannerResult = plannerService.plannerList();
List<PlannerDto> allPlanners = (List<PlannerDto>) plannerResult.get("list");
List<PlannerDto> plannerDto = new ArrayList();
for(PlannerDto planner : allPlanners) { //로그인한 userid와 planner의 userid가 같을경우 특정 userid의 plannerDto 정보를 forward 처리
if(userDto.getUserid().equals(planner.getUserid())) {
plannerDto.add(planner);
}
}
req.setAttribute("plannerDto", plannerDto);
req.getRequestDispatcher("/WEB-INF/view/user/myinfo.jsp").forward(req, resp);
return ;
}
//Method==POST-> 도서 등록처리
//파라미터 받기
String userid = req.getParameter("userid");
String password = req.getParameter("password");
String role = req.getParameter("role");
Integer age = Integer.parseInt(req.getParameter("age"));
String gender = req.getParameter("gender");
// 유효성 확인
if(!isValid(null)) {
//
}
// 서비스 실행
UserDto userDto = new UserDto(userid,password,role,age,gender);
HttpSession session = req.getSession();
System.out.println("UserDto : " + userDto);
Map<String,Object> rvalue = userService.userUpdate(userDto);
String message = (String)rvalue.get("message");
Boolean isUpdated = (Boolean)rvalue.get("isUpdated");
if(isUpdated) {
session.setAttribute("userDto", userDto);
resp.sendRedirect(req.getContextPath() +"/" );
return ;
} else {
req.setAttribute("message", message);
req.getRequestDispatcher("/WEB-INF/view/index.jsp").forward(req, resp);
return ;
}
}catch(Exception e) {
try {
ExceptionHandler(e,req,resp);
} catch (ServletException e1) {
try{ throw new ServletException(e1); }catch(Exception e2) {e2.printStackTrace();}
}
System.out.println("[BC] Exception 발생.." + e);
}
}
private boolean isValid(Object dto) {
return true;
}
}
과제 이런식으로 일단 진행.
planner에 userid 와 userDto 에 userid가 같을 때 그에 맞는 userid 의 plannerDto 만 화면에 나오도록 했다.
강사님께 뭐 물어볼까