MapleStory Finger Point

๐ŸŸค JAVA/๐ŸŸค Spring

[Spring] request, response

HYEJU01 2024. 8. 25. 20:02

 

//voidํ˜•์€ ๋“ค์–ด์˜จ ์š”์ฒญ ๊ฒฝ๋กœ๊ฐ€, ๋‚˜๊ฐ€๋Š” ๊ฒฝ๋กœ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค(ํ™”๋ฉด๊ฒฝ๋กœ)
	@RequestMapping("/req_ex01")
	public void ex01() { 
	}
	
	@RequestMapping("/req_ex01")
	public String ex01() {
		return "request/req_ex01"; //ํ™”๋ฉด๊ฒฝ๋กœ
	}

 

 

	//@RequestMapping(value = "/basic", method = RequestMethod.GET) //get๋งŒ ํ—ˆ์šฉํ•จ
	//@RequestMapping(value = "/basic", method = RequestMethod.POST) //post๋งŒ ํ—ˆ์šฉํ•จ
	//@RequestMapping("/basic") //get, post ๋‘˜๋‹ค ํ—ˆ์šฉํ•จ
    
    
	@RequestMapping(value = {"/basic", "/basic2"})
	public String basic() {
		
		System.out.println("basic์š”์ฒญ ์‹คํ–‰๋จ");
		return null; 
	}

 

	//@GetMapping("/basic3") //๋ฆฌํ€˜์ŠคํŠธ๋งตํ•‘ + get๋งŒํ—ˆ์šฉ
	@PostMapping("/basic3") //๋ฆฌ์‰์ŠคํŠธ๋งตํ•‘ + post๋งŒํ—ˆ์šฉ
	public String basic3() {
			
		System.out.println("basic3์š”์ฒญ ์‹คํ–‰๋จ");
		return null;
	}

 

 


 

 

	//๋ฆฌํ€˜์ŠคํŠธ ๊ฐ์ฒด๋กœ ๊ฐ’ ๋ฐ›๊ธฐ
	@RequestMapping(value = "/param", method = RequestMethod.POST)
	public String param(HttpServletRequest request) {
		
		System.out.println( request.getParameter("id") );
		System.out.println( request.getParameter("pw") );
		System.out.println( request.getParameterValues("inter") );
		//์ž˜~~~~ ์ฒ˜๋ฆฌํ•ด์„œ~~
		//๊ฒฐ๊ณผํ™”๋ฉด
		return "request/req_ex02_ok";
	}

 

 

	//@RequestParam๋ฐฉ์‹ - ๋ฐ˜๋“œ์‹œ ๊ฐ’์„ ํ™”๋ฉด์—์„œ ๋„˜๊ฒจ์•ผ ๋จ(ํ•„์ˆ˜)
	//required = false ๋Š” ๊ฐ’์ด ์—†๋”๋ผ๋„ ํ†ต๊ณผํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.
	//defaultValue๋Š” ๊ฐ’์ด ์—†์„๋•Œ ๊ธฐ๋ณธ๊ฐ’์„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
	@RequestMapping(value = "/param", method = RequestMethod.POST)
	public String param( @RequestParam("id") String x, //id๋ฅผ ๋ฐ›์•„์„œ x๋งตํ•‘
						 @RequestParam("pw") String y,
						 @RequestParam(value = "inter", required = false, defaultValue = "A") ArrayList<String> list ) { 
		
		
		System.out.println(x);
		System.out.println(y);
		System.out.println(list);
		//์ž˜~~~~ ์ฒ˜๋ฆฌํ•ด์„œ~~
		//๊ฒฐ๊ณผํ™”๋ฉด
		return "request/req_ex02_ok";
	}

 

 

	
	@RequestMapping(value = "/param", method = RequestMethod.POST)
	public String param( MemberVO vo  ) { 

		System.out.println(vo.toString());
				
		//์ž˜~~~~ ์ฒ˜๋ฆฌํ•ด์„œ~~
		//๊ฒฐ๊ณผํ™”๋ฉด
		return "request/req_ex02_ok";
	}

 

 


	//model์ „๋‹ฌ์ž 
	@RequestMapping("/res_ex02")
	public String ex02(ModelMap model) {
		
		model.addAttribute("data", "ํ™๊ธธ๋™"); //request.setAttribute(ํ‚ค, ๊ฐ’)
		model.addAttribute("now", new Date());
		
		return "response/res_ex02";
	}

 

 

	//modelAndView์ „๋‹ฌ์ž
	@RequestMapping("/res_ex02")
	public ModelAndView ex02() {
		
		ModelAndView mv = new ModelAndView();
		mv.addObject("data", "์ด์ˆœ์‹ "); //์—ฌ๊ธฐ์— ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ด๋Š”๋‹ค.
		mv.addObject("now", new Date());
		mv.setViewName("response/res_ex02"); //ํ™”๋ฉด์— ๋Œ€ํ•œ ๊ฒฝ๋กœ
		
		return mv; //
	}

 

	//@ModelAttribute = ๋ฆฌํ€˜์ŠคํŠธ + modelํ•ฉ์„ฑ์–ด
	@RequestMapping("/res_ex03")
	public String ex03( @ModelAttribute("info") String info ) {
		
		System.out.println(info); //info๋ผ๋Š” ์ด๋ฆ„์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๋„˜๊ฒจ์ค๋‹ˆ๋‹ค.
		
		return "response/res_ex03";
	}

 

 

	@RequestMapping("/res_ex04")
	public String ex04( @ModelAttribute("info") MemberVO vo) { //vo๋กœ ๋งตํ•‘ํ•˜๊ณ  info๋กœ ๋„˜๊ธด๋‹ค
		
		
		System.out.println(vo.toString());
		
		return "response/res_ex04";
	}

 

 


 

	//๋ฆฌ๋‹ค์ด๋ ‰ํŠธ ๋ผ๋Š”๊ฒƒ?
	//์Šคํ”„๋ง์€ ์ด๋™๋ฐฉ์‹ ๊ธฐ๋ณธ์ด forward์ž…๋‹ˆ๋‹ค
	//redirect๋Š” ๋‹ค์‹œ ๋‹ค์‹œ ์ปจํŠธ๋กค๋Ÿฌ๋ฅผ ํƒœ์šฐ๋Š” ์šฉ๋„๋กœ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
	
	
	//ํ™”๋ฉด์š”์ฒญ
	@RequestMapping("/login")
	public String loginView() {
		
		System.out.println("๋ฆฌ๋‹ค์ด๋ ‰ํŠธ ํƒ€๊ณ  ๋‚˜๊ฐ~");
		
		return "response/login";
	}

 

	@RequestMapping(value = "/loginForm", method = RequestMethod.POST )
	public String loginForm( MemberVO vo, RedirectAttributes ra ) {
		
		//id๋ž‘ pw๊ฐ€ ๋™์ผํ•˜๋ฉด ๋กœ๊ทธ์ธ ์„ฑ๊ณต
		String id = vo.getId();
		String pw = vo.getPw();
		
		if(id.equals(pw)) { //๋กœ๊ทธ์ธ ์„์„ธ์Šค
			return "response/login_ok"; //์ด๋™
		} else { //๋กœ๊ทธ์ธ ์‹คํŒจ
			
			//๋ฆฌ๋‹ค์ด๋ ‰ํŠธ๋ฅผ ๋ณด๋‚ผ๋•Œ, 1ํšŒ์„ฑ ํœ˜๋ฐœ๋ฐ์ดํ„ฐ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๋„˜๊ธฐ๋Š” ๋ฐฉ์‹์„ ์ œ๊ณตํ•ด์คŒ
			ra.addFlashAttribute("msg", "ํ‹€๋ ธ๋Š”๋ฐ์š”?ใ…‹");
			
			return "redirect:/response/login";
		}
		
		
	}