package com.sp.app.join;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;

/*
 @SessionAttributes
 - 모델의 객체를 세션에 저장하여 뷰(jsp)에서 공유
 - Controller에서 사용
 - 용도
 	스프링 form태그 라이브러리를 이용할 경우
 	여러 단계에 걸쳐 입력된 값을 처리할 경우(지속적으로 값을 유지)
 		double submit 방지 - 브라우저의 뒤로가기 안됨
 
 */

@SessionAttributes("user") // 클래스명과같아야함
@Controller("join.joinController")
@RequestMapping("/join/*")
public class JoinController {
	
	@ModelAttribute("user") // user 이름으로 모든 페이지가 공유할 수 있는 객체를 만듦
	public User command() {
		return new User(); // 세션에 저장할 객체 메모리 할당
	}
	
	@RequestMapping(value = "main", method = RequestMethod.GET)
	public String joinForm(@ModelAttribute("user") User user) throws Exception {
		// 회원가입 처음화면(step1.jsp) 출력
		return "join/step1";
	}
	
	// @ModelAttribute("user")는 @SessionAttributes("user")에서 설정한
	// 설정이름이 동일하므로 세션에 저장된 user을 사용함
	// User 클래스명 첫글자가 소문자인 이름과 동일한 경우 생략가능
	@RequestMapping(value = "step1", method = RequestMethod.POST)
	public String step1Submit(@ModelAttribute("user") User user) throws Exception {
		
		// 회원가입 두번째 화면 출력
		return "join/step2";
	}
	
	@RequestMapping(value = "step2", method = RequestMethod.POST)
	public String step2Submit(@ModelAttribute("user") User user,
			SessionStatus sessionStatus,
			Model model
			) throws Exception {
		// 회원가입 정보를 DB에 저장
		
		String s = "아이디:" +user.getId() + "<br>";
		s += "이름:"+user.getName() + "<br>";
		s += "이메일:"+user.getEmail() + "<br>";
		s += "패스워드:"+user.getPwd() + "<br>";
		s += "전화번호:"+user.getTel() + "<br>";
		
		// 세션에 저장된 내용 지우기
		sessionStatus.setComplete();
		model.addAttribute("msg", s);
		return "join/complete";
	}
	
}
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ page trimDirectiveWhitespaces="true" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link rel="icon" href="data:;base64,iVBORw0KGgo=">
</head>
<body>

<form method="post"
	action="${pageContext.request.contextPath}/join/step1">
	
	<p> 이름 : <input type="text" name="name" value="${user.name}"> </p>
	<p> 이메일 : <input type="text" name="email" value="${user.email}"> </p>
	<p>
		<button type="submit">다음단계</button>
	</p>

</form>

</body>
</html>
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ page trimDirectiveWhitespaces="true" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link rel="icon" href="data:;base64,iVBORw0KGgo=">
</head>
<body>

<form method="post" 
	action="${pageContext.request.contextPath}/join/step2">
	
	<p> 아이디 : <input type="text" name="id" value="${user.id}"> </p>
	<p> 패스워드 : <input type="password" name="pwd"> </p>
	<p> 전화번호: <input type="text" name="tel" value="${user.tel}"> </p>
	<p>
		<button type="button"
			onclick="location.href='${pageContext.request.contextPath}/join/main';" >이전단계</button>
		<button type="submit">회원가입</button>
	</p>

</form>

</body>
</html>
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ page trimDirectiveWhitespaces="true" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link rel="icon" href="data:;base64,iVBORw0KGgo=">
</head>
<body>

<h3> 환영합니다. </h3>
<p>
	${msg}
</p>

</body>
</html>

+ Recent posts