๊ด€๋ฆฌ ๋ฉ”๋‰ด

bom's happy life

[Spring] JSP์—์„œ ๋ณด๋‚ธ ๋ฐ์ดํ„ฐ Java์—์„œ ๋ฐ›๋Š” ๋ฐฉ๋ฒ• ๋ณธ๋ฌธ

Deveolpment Study๐Ÿ—‚๏ธ/Spring Framework

[Spring] JSP์—์„œ ๋ณด๋‚ธ ๋ฐ์ดํ„ฐ Java์—์„œ ๋ฐ›๋Š” ๋ฐฉ๋ฒ•

bompeach 2023. 6. 5. 18:01

๋ฌธ์ œ : JSP ์—์„œ ํผ ํƒœ๊ทธ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•ด์„œ ์ปจํŠธ๋กค๋Ÿฌ์— ๋„˜๊ฒจ์ฃผ์—ˆ๋Š”๋ฐ, `rentTime` ๊ฐ’๋งŒ ๋„˜์–ด๊ฐ€๊ณ  ๋‚˜๋จธ์ง€ ๊ฐ’์ด ๋„˜์–ด๊ฐ€์ง€ ์•Š๋Š”๋‹ค.

 

.

.

 

๋ฌธ์ œ์˜ ์ฝ”๋“œ : 

function placeRentAdd(rentDate,rentTime) {
		alert(rentDate);
		alert(rentTime);
		var rentYN= confirm("์˜ˆ์•ฝํ•˜์‹œ๊ฒ ์Šต๋‹ˆ๊นŒ?");
		if(rentYN) {
			
                var form = document.createElement("form");
	    	form.setAttribute("charset", "utf-8");
	    	form.setAttribute("method", "POST");
	    	form.setAttribute("action", "/reservationForm.do");
	    	
	    	var rentInfo = document.createElement("input");
	    	rentInfo.setAttribute("name", "placeNm");
	    	rentInfo.setAttribute("value", "${placeNm}");
	    	rentInfo.setAttribute("name", "rentDate");
	    	rentInfo.setAttribute("value", rentDate);
	    	rentInfo.setAttribute("name", "rentTime");
	    	rentInfo.setAttribute("value", rentTime);
	        form.appendChild(rentInfo);
	        
	        form.style.display = "none";
	        
	        document.body.appendChild(form);
	        form.submit();
		} else {
			location.reload();
		}
	}

์ด๋ ‡๊ฒŒ ๋ณด๋‚ด์ฃผ๋‹ˆ๊นŒ `rentTime`๊ฐ’๋งŒ ๋„˜์–ด๊ฐ€๊ณ  `rentDate` ์™€ `placeNm` ๊ฐ’์ด ์ปจํŠธ๋กค๋Ÿฌ๋กœ ๋„˜์–ด๊ฐ€์ง€ ์•Š์•˜๋‹ค.

๊ทธ ์ด์œ ๋ฅผ ์‚ดํŽด๋ณด๋‹ˆ, `rentInfo` ์š”์†Œ์˜ `name` ๊ณผ `value` ์†์„ฑ์„ ์—ฌ๋Ÿฌ๋ฒˆ ์„ค์ •ํ•˜๊ณ  ์žˆ์–ด์„œ ์•ˆ ๋„˜์–ด๊ฐ”๋˜ ๊ฒƒ์ด์—ˆ๋‹ค. ๊ฐ ์†์„ฑ์„ ์„ค์ •ํ•  ๋•Œ๋งˆ๋‹ค ์ด์ „ ์„ค์ •์ด ๋ฎ์–ด์“ฐ์—ฌ์„œ ๋งˆ์ง€๋ง‰์œผ๋กœ ์„ค์ •ํ•œ ๊ฐ’๋งŒ ์ ์šฉ์ด ๋˜์—ˆ๋˜ ๊ฒƒ์ด๋‹ค. ๊ทธ๋ž˜์„œ `rentDate` ์™€ `placeNm` ์˜ ๊ฐ’์ด ์ œ๋Œ€๋กœ ์ „๋‹ฌ๋˜์ง€ ๋ชปํ•œ๊ฒƒ!

 

.

.

 

์ˆ˜์ •ํ•œ ์ฝ”๋“œ :

var form = document.createElement("form");
form.setAttribute("charset", "utf-8");
form.setAttribute("method", "POST");
form.setAttribute("action", "/reservationForm.do");

var rentInfo1 = document.createElement("input");
rentInfo1.setAttribute("type", "hidden");
rentInfo1.setAttribute("name", "placeNm");
rentInfo1.setAttribute("value", "${placeNm}");
form.appendChild(rentInfo1);

var rentInfo2 = document.createElement("input");
rentInfo2.setAttribute("type", "hidden");
rentInfo2.setAttribute("name", "rentDate");
rentInfo2.setAttribute("value", rentDate);
form.appendChild(rentInfo2);

var rentInfo3 = document.createElement("input");
rentInfo3.setAttribute("type", "hidden");
rentInfo3.setAttribute("name", "rentTime");
rentInfo3.setAttribute("value", rentTime);
form.appendChild(rentInfo3);

form.style.display = "none";

document.body.appendChild(form);
form.submit();

์œ„์˜ ์ฝ”๋“œ์—์„œ `rentInfo1`, `rentInfo2`, `rentInfo3` ๋กœ ๊ฐ๊ฐ `placeNm`, `rentDate`, `rentTime` ํ•„๋“œ๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์žˆ๋‹ค. ๊ฐ ํ•„๋“œ๋ฅผ ์ˆจ๊ฒจ์ง„(hidden) ํ•„๋“œ๋กœ ์ƒ์„ฑํ•˜๋ ค๋ฉด `type` ์†์„ฑ์„ `hidden` ์œผ๋กœ ์„ค์ •ํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  `setAttribute` ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ `name` ๊ณผ `value` ์†์„ฑ์„ ๊ฐ๊ฐ ์„ค์ •ํ•œ๋‹ค.

 

์ด๋ ‡๊ฒŒ ์ˆ˜์ •ํ•œ ํ›„์—๋Š” `rentDate`์™€ `placeNm` ๊ฐ’์ด ํผ ๋ฐ์ดํ„ฐ๋กœ ์ •ํ™•ํ•˜๊ฒŒ ์ „์†ก๋˜๊ณ , ์„œ๋ฒ„ ์ธก์˜ `reservationForm` ๋ฉ”์„œ๋“œ์—์„œ ํ•ด๋‹น ๊ฐ’์„ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๊ฒŒ ๋œ๋‹ค!

 

 

์ปจํŠธ๋กค๋Ÿฌ : 

@RequestMapping(value="/reservationForm.do", method = RequestMethod.POST)
    public String reservationForm(Model model, HttpServletRequest request) throws Exception {
		
		String placeNm = request.getParameter("placeNm");
		String rentDate = request.getParameter("rentDate");
		String rentTime = request.getParameter("rentTime");
		model.addAttribute("placeNm", placeNm);
		model.addAttribute("rentDate", rentDate);
		model.addAttribute("rentTime", rentTime);
		
		System.out.println("placeNm: " + placeNm);
		System.out.println("rentDate: " + rentDate);
		System.out.println("rentTime: " + rentTime);
		
		return "reservationForm";
	}

์ด๋ ‡๊ฒŒ ์ปจํŠธ๋กค๋Ÿฌ์—์„œ ๋ฐ›์•„์ฃผ๋‹ˆ ์ด์ œ์•ผ ๋ฐ์ดํ„ฐ ๊ฐ’์ด ์ž˜ ๋“ค์–ด์˜จ๋‹ค!