package com.sp.app.test4;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

@Controller("test4.testController")
@RequestMapping("/test4/*")
public class TestController {

	@GetMapping("write")
	public String form() throws Exception {
		return "test4/write";
	}
	
	@PostMapping("write")
	public String submit(User dto,
			RedirectAttributes rAttr
			) throws Exception {
		String s = "회원가입을 축하합니다.";
		
		// DB 작업
		
		// 리다이렉트한 페이지에 값 넘기기(내부적으로 세션을 활용함)
		rAttr.addFlashAttribute("dto", dto);  // 한번만 dto가 나옴
		rAttr.addFlashAttribute("msg", s);
		
		return "redirect:/test4/complete";
	}
	
	@GetMapping("complete")
	public String complete(@ModelAttribute("dto") User dto) throws Exception {
		// @ModelAttribute("dto") 가 없으면 아래에서는 null이 된다.
		// F5를 눌러 새로 고침을 하면 초기화 되어 아래 이름은 null이 된다.
		// jsp에서 출력되는 내용은 위 메소드의 rAttr.addFlashAttribute("dto", dto); 내용이다. 
		System.out.println(dto.getName() + " : " + dto.getId());
		 
		return "test4/result";
	}
}
<%@ 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>

<form action="${pageContext.request.contextPath}/test4/write"
	method="post">
	<p>이름 : <input type="text" name="name"> </p>
	<p>아이디 : <input type="text" name="id"> </p>
	<p>비밀번호 : <input type="text" name="pwd"> </p>
	<p>생년월일 : <input type="text" name="birth"> </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>

<h3> 결 과 </h3>

<p> 이름 : ${dto.name} </p>
<p> 아이디 : ${dto.id} </p>
<p> 비밀번호 : ${dto.pwd} </p>
<p> 생일 : ${dto.birth} </p>

<p> ${msg} </p>

</body>
</html>

+ Recent posts