본문 바로가기
Spring

230202 Spring response 실습

quiz01.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>    
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>

	<%--
	1. QuizController에는 /quiz 폴더 아래의 모든 요청을 처리할 수 있는 컨트롤러를 생성하세요
	2. quiz01  화면처리를 할 수 있는 메서드를 생성 (quiz01 맵핑)
	3. 다음 생년월일을 받아서 콘솔에 출력하는 메서드를 생성 (sendBirth 맵핑)
		조건) Quiz01VO 커맨드객체 사용, 콘솔에 전송된 값을 붙여서 출력합니다 ex)20180615
	4. 출력후엔 quiz01_ok 페이지에 "당신의 생일은 ~~~~년 ~~월 ~~일" 을 출력하세요
	--%>
	
	<h2>quiz화면(화면 URL요청: /quiz/quiz01)</h2>
	<form action="sendBirth" method="post">
		생년월일:<br>
		<input type="text" name="year" maxlength="4" size="4" placeholder="년(4자)">
		<select name="month">
			<c:forEach var="i" begin="1" end="12">
				<option>${i }</option>
			</c:forEach>
		</select>
		<input type="text" name="day" maxlength="2" size="4" placeholder="일">
		
		<input type="submit" value="전송">
	</form>
</body>
</html>

 

 

 

Quiz01VO.java

package com.simple.command;

public class Quiz01VO {

	private String year;
	private String month;
	private String day;



	//기본생성자
	public Quiz01VO() {}

	//생성자
	public Quiz01VO(String year, String month, String day) {
		super();
		this.year = year;
		this.month = month;
		this.day = day;
	}

	public String getYear() {
		return year;
	}

	public void setYear(String year) {
		this.year = year;
	}

	public String getMonth() {
		return month;
	}

	public void setMonth(String month) {
		this.month = month;
	}

	public String getDay() {
		return day;
	}

	public void setDay(String day) {
		this.day = day;
	}

	@Override
	public String toString() {
		return year+month+day;
	}





}

 

 

 

quiz01_ok.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<%-- 당신의 생일은 ${birth.year}년 ${birth.month}월 ${birth.day}일 입니다. --%>
	당신의 생일은${birth }입니다.
</body>
</html>

콘솔창엔 

20250306

출력


quiz02.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<%--
	1.QuizController에 화면처리 메서드 생성	
	2.폼 처리 메서드 생성
	3.입력한 정보를 quiz02_ok.jsp 화면에 출력
	4.방법 자유
	 --%>
	<h2>회원가입</h2>
	<form action="join" method="post">
		아이디:<input type="text" name="id"><br>
		비밀번호:<input type="password" name="pw"><br>
		이름:<input type="text" name="name"><br>
		이메일:<input type="email" name="email"><br>
		<input type="submit" value="회원가입">
	</form>
	
	
</body>
</html>

 

quiz02_ok.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	
	아이디 : ${id }
	비밀번호 : ${pw }
	이름 : ${name }
	이메일 : ${email }
</body>
</html>


quiz03.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<%--
	1. quiz03 파일의 화면처리를 할 수 있는 메서드를 생성하세요.
	2. 폼태그의 맵핑은 join2 으로 맵핑하세요.
	3. 아이디가 적지 않았다면 이라면 quiz03화면으로 이동해서 화면에 "아이디를 입력하세요" 출력
	4. 비밀번호와 비밀번호 체크가 다르다면 quiz03화면으로 이동해서 "비밀번호를 확인하세요" 출력
	5. 그렇지 않으면 quiz03_ok로 이동해서 "id님 회원가입을 축하합니다" 출력
	
	6. 힌트:문자열 비교는 equals()를 이용, RedirectAttribute 이용 
	--%>
 
	<form action="join2" method="post">
		ID: <input type="text" name="id" size="10"><br>
		비밀번호 : <input type="password" name="pw" size="10"><br>
		비밀번호 확인: <input type="password" name="pw_check" size="10"><br>
		<span>${msg }</span>
		<input type="submit" value="로그인">
	</form>

</body>
</html>

아이디를 입력하지 않거나, 비밀번호를 입력하지 않거나, 비밀번호-비밀번호 확인이 일치하지 않으면 해당 메시지 redirect로 출력

 

 

Quiz03VO.java

package com.simple.command;

public class Quiz03VO {

	private String id;
	private String pw;
	private String pw_check;
	
	//기본생성자
	public Quiz03VO() {}

	//생성자 
	public Quiz03VO(String id, String pw, String pw_check) {
		super();
		this.id = id;
		this.pw = pw;
		this.pw_check = pw_check;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getPw() {
		return pw;
	}

	public void setPw(String pw) {
		this.pw = pw;
	}

	public String getPw_check() {
		return pw_check;
	}

	public void setPw_check(String pw_check) {
		this.pw_check = pw_check;
	}

	@Override
	public String toString() {
		return "Quiz03VO [id=" + id + ", pw=" + pw + ", pw_check=" + pw_check + "]";
	}
	
}

 

quiz03_ok.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	${info.id }님 회원가입을 축하합니다. 
</body>
</html>


QuizController.java

package com.simple.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
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.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.simple.command.Quiz01VO;
import com.simple.command.Quiz03VO;

@Controller
@RequestMapping("/quiz")
public class QuizController {
	
	//quiz01
	@RequestMapping("/quiz01")
	public void quiz01() {}
	
	@RequestMapping("/quiz01_ok")
	public String quiz01_ok() {
		return "quiz/quiz01_ok";
	}
	
//	@RequestMapping("/sendBirth")
//	public String sendBirth(@ModelAttribute("birth")Quiz01VO vo) {
//		
//		System.out.println(vo.toString());
//		//출력문을 2023412가 아니라 20230412처럼 하려면 vo setter가 아니라 컨트롤러를 수정해야 한다.
//		return "quiz/quiz01_ok";
//	}
	
	@RequestMapping("/sendBirth")
	public String sendBirth(Quiz01VO vo, Model model) {
		String month="0";
		String day="0";
		if(vo.getMonth().length()==1) {
			month=month.concat(vo.getMonth());
		}else {
			month=vo.getMonth();
		}
		
		if(vo.getDay().length()==1) {
			day=day.concat(vo.getDay());
		}else {
			day=vo.getDay();
		}
		
		
		System.out.println(vo.getYear()+month+day);
		model.addAttribute("birth", vo.getYear()+"년 "+month+"월 "+day+"일");
		return "/quiz/quiz01_ok";
	}
	
	///////////////////////////////////////////////////////////////
	//quiz02
	@RequestMapping("/quiz02")
	public String quiz02() {
		return "quiz/quiz02";
	}
	
	@RequestMapping("/quiz02_ok")
	public String quiz02_ok() {
		return "/quiz/quiz02_ok";
	}
	
	@PostMapping("/join")
	public ModelAndView join(@RequestParam(value="id", required = false,defaultValue = "") String id,
			@RequestParam(value="pw", required = false,defaultValue = "")String pw,
			@RequestParam(value="name", required = false,defaultValue = "")String name,
			@RequestParam(value="email", required = false,defaultValue = "")String email) {
		
		ModelAndView mv = new ModelAndView();
		mv.addObject("id",id);
		mv.addObject("pw",pw);
		mv.addObject("name",name);
		mv.addObject("email",email);
		mv.setViewName("/quiz/quiz02_ok");
		
		return mv;
	}
	
	///////////////////////////////////////////////////////////////
	//quiz03
	@RequestMapping("/quiz03")
	public String quiz03() {
	return "quiz/quiz03";
	}
	
	@RequestMapping("/quiz03_ok")
	public String quiz03_ok() {
		return "/quiz/quiz03_ok";
	}
	
	@RequestMapping("join2")
	public String join2(@ModelAttribute("info")Quiz03VO vo,RedirectAttributes ra) {
		if(vo.getId().equals("")) {
			ra.addFlashAttribute("msg", "아이디를 입력하세요");
			return "redirect:/quiz/quiz03";
		}
		
		if(vo.getPw().equals("")||vo.getPw_check().equals("")) {
			ra.addFlashAttribute("msg", "비밀번호를 입력하세요");
			return "redirect:/quiz/quiz03";
		}else if(!vo.getPw().equals(vo.getPw_check())) {
			ra.addFlashAttribute("msg", "비밀번호를 확인하세요");
			return "redirect:/quiz/quiz03";
		}
		
		return "/quiz/quiz03_ok";
	}
	
	
}

 

 

전부 forward방식이다. 성공경로가 sendBrith, join, join2인건 이 때문.