Spring @Controller, @RequestMapping

2021. 11. 26. 09:52ใ†Spring

728x90

1. Controller ํด๋ž˜์Šค 

๊ธฐ๋ณธ์ ์œผ๋กœ Controller ํด๋ž˜์Šค๋ฅผ ์ž‘์„ฑํ•  ๋•Œ๋Š” 

1๏ธโƒฃ @Controller๋ฅผ ์ด์šฉํ•œ ํด๋ž˜์Šค ์ƒ์„ฑ 

2๏ธโƒฃ @RequestMapping์„ ์ด์šฉํ•œ ์š”์ฒญ ๊ฒฝ๋กœ ์ง€์ •

3๏ธโƒฃ ์š”์ฒญ ์ฒ˜๋ฆฌ ๋ฉ”์„œ๋“œ (handler) ๊ตฌํ˜„ 

4๏ธโƒฃ "๋ทฐ ์ด๋ฆ„" ํ˜น์€ ModelAndView ๋ฆฌํ„ด 

 

์ด๋•Œ url mapping๊ณผ ๋ฉ”์„œ๋“œ ์ด๋ฆ„ ๋ทฐ ์ด๋ฆ„์€ ๊ฐ™์„ ํ•„์š”๋Š” ์—†์ง€๋งŒ ํ—ท๊ฐˆ๋ฆด ์ˆ˜๋„ ์žˆ์œผ๋‹ˆ ํŒ๋‹จ์€ ์•Œ์•„์„œ ! 

 

2. ๋‹ค์–‘ํ•œ path,url ์•Œ์•„๋ณด๊ธฐ 

request path๋Š” /common ์ด๋ฉฐ context path๋Š” /sts10_request์ด๋‹ค. 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
	// forward๋ฐ›์€ jsp ์ธก์—์„œ ์›๋ž˜ request uri ๋‚ด์šฉ
	String servlet_request_uri = (String)request.getAttribute("javax.servlet.forward.request_uri");
	String conPath = request.getContextPath();
	String servlet_reqpath = servlet_request_uri.substring(conPath.length());
	
	String uri = request.getRequestURI();
	String reqPath = uri.substring(conPath.length());
	String url = request.getRequestURL().toString();
	
	// jsp ํŒŒ์ผ๋ช…
	String jspFile = uri.substring(uri.lastIndexOf("/") + 1, uri.length());
	String jspName = uri.substring(uri.lastIndexOf("/") + 1, uri.length() - 4);
	
	request.setAttribute("jspName", jspName);
%>
<!DOCTYPE html>
<html lang="ko">
<head>
	<meta charset="utf-8"/>
	<title><%= jspName %></title>
	<link rel = "stylesheet" type = "text/css" 
	href = "${pageContext.request.contextPath }/CSS/sts_common.css">
</head>
<body>
<hr>
<div class="sts">
<b>Servlet</b><br>
request URI : <span><%= servlet_request_uri %></span><br>
context path : <span><%= conPath %></span><br>
request path : <span><%=servlet_reqpath %></span><br>
VIEW file : <span><%= jspFile %></span><br>
<hr>
<b>VIEW(JSP)</b><br>
url : <span><%= url %></span><br>
VIEW Path : <span><%= reqPath %></span><br>
uri : <span><%= uri %></span><br>
</div>
</body>
</html>

์ด ์ฝ”๋“œ๋ฅผ ๊ธฐ๋ณธ์œผ๋กœ ๋‹ค์–‘ํ•œ @RequestMapping ์—ฐ์Šต์„ ํ•ด๋ณผ ์˜ˆ์ •์ด๋‹ค. 

 

 

3. View์— ๋ฐ์ดํ„ฐ ์ „๋‹ฌ 

1๏ธโƒฃ Model ๋งค๊ฐœ๋ณ€์ˆ˜ 

controller

request path๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ง€์ •ํ•œ๋‹ค. 

๊ทธ๋ฆฌ๊ณ  Model์„ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ memberAge์™€ memberName์— ๊ฐ๊ฐ ๊ฐ’์„ ๋‹ด์•„์„œ ํ•ด๋‹นํ•˜๋Š” ๋ทฐ๋ฅผ ๋ฆฌํ„ดํ•˜๋ฉด 

์Šคํ”„๋ง์ด ์•Œ์•„์„œ ๋„˜๊ฒจ์ค€๋‹ค. ๋”ฐ๋ผ์„œ model์— ๋‹ด๊ธฐ๋งŒ ํ•˜๋ฉด ์Šคํ”„๋ง์ด DispatcherServlet์— ๋„˜๊ฒจ์ค€๋‹ค. 

 

member ํด๋” ์•ˆ info.jsp
๊ฒฐ๊ณผ 

 

2๏ธโƒฃ ModelAndView ๋ฆฌํ„ด 

Controller

ModelAndView์—๋Š” mbName, mbDate์— ๋ฐ์ดํ„ฐ๋„ ๋‹ด๊ณ , ๋ทฐ ๋„ ์ง€์ •ํ•œ๋‹ค์Œ์— ๋ฆฌํ„ดํ•˜๋ฉด ๋œ๋‹ค. 

member/find.jsp
๊ฒฐ๊ณผ

 

3๏ธโƒฃ @RequestMapping์— ํ™•์žฅ์ž ํŒจํ„ด๋„ ๊ฐ€๋Šฅ 

controller

@RequestMapping("/member/*.do)๋ผ๊ณ  ์ง€์ •ํ•˜๊ณ , 

member/doMember.jsp
๊ฒฐ๊ณผ
๊ฒฐ๊ณผ2

 

 

๋งˆ์ง€๋ง‰์œผ๋กœ Handler๋ฉ”์„œ๋“œ ๋™์ž‘์„ ์ •๋ฆฌํ•˜๋ฉด 

1๏ธโƒฃ @RequestMapping์œผ๋กœ request๋ฅผ ๋ฐ›๋Š”๋‹ค. 

2๏ธโƒฃ ๊ทธ๋ฆฌ๊ณ  ํ•„์š”ํ•œ ๋™์ž‘์„ ์ˆ˜ํ–‰ํ•œ๋‹ค. (์ด๋•Œ DAO ํ˜น์€ Command ๊ฐ์ฒด ์‚ฌ์šฉ) 

3๏ธโƒฃ ํ•„์š”ํ•œ ๋™์ž‘ ์ˆ˜ํ–‰ ํ›„ ๊ฒฐ๊ณผ๋ฅผ Model์— ๋‹ด๊ณ  ๋ทฐ๋ฅผ ์ง€์ •ํ•œ๋‹ค. (ModelAndView ๋‹ด์•„ ๋ฆฌํ„ด)

์ด๋Ÿฐ์‹์œผ๋กœ ๋™์ž‘ํ•œ๋‹ค. 

 

โ— ๋งค๋ฒˆ @RequestMapping์— (value = "/member/info") ์ด๋Ÿฐ์‹์œผ๋กœ ํ•˜๋ฉด 

request url ๊ด€๋ฆฌ๊ฐ€ ๋’ค์„์ผ์ˆ˜๋„ ์žˆ๋‹ค. "/board/write"์ด๊ฒƒ๋„ ๊ฐ™์ด ์žˆ์œผ๋ฉด ํ—ท๊ฐˆ๋ฆผ 

์ฆ‰ ๋ชจ๋“  handler๊ฐ€ ํ•˜๋‚˜์˜ @Controller ํด๋ž˜์Šค์— ์žˆ์œผ๋ฉด ์ฒด๊ณ„์ ์œผ๋กœ ๊ด€๋ฆฌํ•˜๊ธฐ ์–ด๋ ต๊ธฐ ๋•Œ๋ฌธ์— ๋‚˜๋ˆ ์„œ ์ž‘์„ฑํ•ด์ค€๋‹ค. 

์ด๋Ÿฐ์‹์œผ๋กœ @Controller ์ง€์ •ํ›„์— @RequestMapping์„ ์ง€์ •ํ•ด์ฃผ๋ฉด /board๋กœ ์‹œ์ž‘ํ•˜๋Š” request ๋งŒ ์ฒ˜๋ฆฌํ•˜๋Š” ์ปจํŠธ๋กค๋Ÿฌ ํด๋ž˜์Šค๊ฐ€ ๋œ๋‹ค. 

๋”ฐ๋ผ์„œ ๋ฐ‘์˜ ํ•ธ๋“ค๋Ÿฌ ๋ฉ”์„œ๋“œ์˜ request๋Š” /board/list ์ด๋Ÿฐ์‹์ด ๋œ๋‹ค. 

 

 

728x90