https://intheham.tistory.com/67
include 지시자 및 액션 태그를 활용한 index.jsp
복습 ※ 웹뷰(JSP)가 컨트롤러(Servlet)에 요청 보내는 법 * 동기 요청 (페이지 이동) -
intheham.tistory.com
작동 방식이나 jsp 표현 방식은 이전과 같다.
서버 전송 방식이 Servlet 에서 JPA Controller 로 변했을 뿐!
index.jsp
<table>
<tr>
<td colspan="2" style="width:100%; height:30%">
<%@ include file="header.jsp" %>
</td>
</tr>
<tr>
<td style="width:50px; height:60%">
<c:if test="${sessionScope.type == 1}"> <!-- 구매자 -->
<%@ include file="cmenu.jsp" %>
</c:if>
<c:if test="${sessionScope.type == 2}"> <!-- 판매자 -->
<%@ include file="smenu.jsp" %>
</c:if>
</td>
<td>
<c:if test="${not empty bodyview }">
<jsp:include page="${bodyview }" />
</c:if>
</td>
</tr>
</table>
header.jsp
<style>
.t1{
width:200px;
height:50px;
}
</style>
<h1>함함 샵</h1>
<c:if test="${not empty sessionScope.loginId}">
<h3>~ hello ${sessionScope.loginId} ~</h3>
</c:if>
<table>
<tr>
<c:if test="${empty sessionScope.loginId}">
<td class="t1"><a href="/shopmember/join">회원가입</a></td>
<td class="t1"><a href="/shopmember/login">로그인</a></td>
</c:if>
<c:if test="${not empty sessionScope.loginId}">
<td class="t1"><a href="/shopmember/edit">내정보보기</a></td>
<td class="t1"><a href="/shopmember/logout">로그아웃</a></td>
<td class="t1"><a href="/shopmember/out?id=${sessionScope.loginId }">회원탈퇴</a></td>
</c:if>
</tr>
</table>
cmenu.jsp
<h3>구매자 메뉴</h3>
<a href="">구매내역</a><br/>
<a href="">상품목록</a>
smenu.jsp
<h3>판매자 메뉴</h3>
<a href="">상품등록</a><br/>
<a href="">상품목록</a>
ShopmemberController
(1) 모든 메서드의 return 경로를 "index"나 "redirect:/" 로 바꿔준다.
(* @ResponseBody 로 가는 AJAX 를 제외 )
- return "index"; >> 내부에서 요청하는 뷰 경로
- return "redirect:/" >> 외부에서 요청하는 뷰 경로 (HomeController 에서 매칭함)
@Controller
public class HomeController {
@RequestMapping("/") // http://localhost:8081/ (외부 요청)
public String home() {
return "index"; // /WEB-INF/views/index.jsp (내부 요청)
}
}
(2) include로 띄울 jsp 경로를 map에 담아 반환한다.
@GetMapping("/join")
public String joinForm(ModelMap map) {
map.addAttribute("bodyview", "/WEB-INF/views/shopmember/join.jsp");
return "index";
}
총코드
더보기
package com.example.demo.shopmember;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
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.ResponseBody;
import jakarta.servlet.http.HttpSession;
@Controller
@RequestMapping("/shopmember")
public class ShopmemberController {
@Autowired
private ShopmemberService service;
// 회원가입 폼
@GetMapping("/join")
public String joinForm(ModelMap map) {
map.addAttribute("bodyview", "/WEB-INF/views/shopmember/join.jsp");
return "index";
}
// 아이디 중복 체크
@ResponseBody
@PostMapping("/idcheck")
public Map idcheck(String id) {
ShopmemberDto dto = service.getMember(id);
boolean flag = (dto == null);
Map map = new HashMap(); // Map 하나 당 {}
map.put("flag", flag); // {"flag", true(or false)}
return map;
}
// 회원가입 완료
@PostMapping("/join")
public String join(ShopmemberDto dto) {
service.saveMember(dto);
return "index";
}
// 로그인 폼
@GetMapping("/login")
public String loginForm(ModelMap map) {
map.addAttribute("bodyview", "/WEB-INF/views/shopmember/login.jsp");
return "index";
}
// 로그인 완료
@PostMapping("/login")
public String login(ShopmemberDto dto, HttpSession session) {
ShopmemberDto dto2 = service.getMember(dto.getId());
if (dto2 != null && dto2.getPwd().equals(dto.getPwd())) {
dto2 = service.getMember(dto.getId());
session.setAttribute("loginId", dto2.getId());
session.setAttribute("type", dto2.getType());
}
return "index";
}
// 내정보 폼
@GetMapping("/edit")
public String myinfo(HttpSession session, ModelMap map) {
String id = (String) session.getAttribute("loginId");
map.addAttribute("vo", service.getMember(id));
map.addAttribute("bodyview", "/WEB-INF/views/shopmember/edit.jsp");
return "index";
}
// 내정보 수정 완료
@PostMapping("/edit")
public String edit(ShopmemberDto dto) {
service.saveMember(dto);
return "redirect:/";
}
// 로그아웃
@GetMapping("/logout")
public String logout(HttpSession session) {
session.invalidate();
return "index";
}
// 회원탈퇴
@GetMapping("/out")
public String out(String id) {
service.delMember(id);
return "redirect:/";
}
}