Spring/Spring MVC

Spring/Spring MVC

[์Šคํ”„๋ง MVC] - 10. DispatcherServlet / ํ•ธ๋“ค๋Ÿฌ ๋งคํ•‘๊ณผ ํ•ธ๋“ค๋Ÿฌ ์–ด๋Œ‘ํ„ฐ

SpringMVC ๊ตฌ์กฐ DispatcherServlet ๊ตฌ์กฐ ์‚ดํŽด๋ณด๊ธฐ ์Šคํ”„๋ง MVC์˜ ํ”„๋ก ํŠธ ์ปจํŠธ๋กค๋Ÿฌ๊ฐ€ ๋ฐ”๋กœ ๋””์ŠคํŒจ์ฒ˜ ์„œ๋ธ”๋ฆฟ(DispatcherServlet)์ด๋‹ค. ์Šคํ”„๋ง ๋ถ€ํŠธ๋Š” DispacherServlet ์„ ์„œ๋ธ”๋ฆฟ์œผ๋กœ ์ž๋™์œผ๋กœ ๋“ฑ๋กํ•˜๋ฉด์„œ ๋ชจ๋“  ๊ฒฝ๋กœ( urlPatterns="/" )์— ๋Œ€ํ•ด์„œ ๋งคํ•‘ํ•œ๋‹ค. ์„œ๋ธ”๋ฆฟ์ด ํ˜ธ์ถœ๋˜๋ฉด FrameworkServlet.service() ๋ฅผ ์‹œ์ž‘์œผ๋กœ ์—ฌ๋Ÿฌ ๋ฉ”์„œ๋“œ๊ฐ€ ํ˜ธ์ถœ๋˜๋ฉด์„œ DispacherServlet.doDispatch() ๊ฐ€ ํ˜ธ์ถœ๋œ๋‹ค. 1. ํ•ธ๋“ค๋Ÿฌ ์กฐํšŒ // Determine handler for the current request. mappedHandler = getHandler(processedRequest); if (mappedHandler == null) {..

Spring/Spring MVC

[์Šคํ”„๋ง MVC] - 9. ํ”„๋ก ํŠธ ์ปจํŠธ๋กค๋Ÿฌ - ์–ด๋Œ‘ํ„ฐ

๋งŒ์•ฝ ์–ด๋–ค ๊ฐœ๋ฐœ์ž๋Š” ControllerV3 ๋ฐฉ์‹์œผ๋กœ ๊ฐœ๋ฐœํ•˜๊ณ  ์‹ถ๊ณ , ์–ด๋–ค ๊ฐœ๋ฐœ์ž๋Š” ControllerV4 ๋ฐฉ์‹์œผ๋กœ ๊ฐœ๋ฐœํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด ์–ด๋–ป๊ฒŒ ํ•ด์•ผํ• ๊นŒ? ์ง€๊ธˆ๊นŒ์ง€ ์šฐ๋ฆฌ๊ฐ€ ๊ฐœ๋ฐœํ•œ ํ”„๋ก ํŠธ ์ปจํŠธ๋กค๋Ÿฌ๋Š” ํ•œ๊ฐ€์ง€ ๋ฐฉ์‹์˜ ์ปจํŠธ๋กค๋Ÿฌ ์ธํ„ฐํŽ˜์ด์Šค๋งŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ControllerV3 , ControllerV4 ๋Š” ์™„์ „ํžˆ ๋‹ค๋ฅธ ์ธํ„ฐํŽ˜์ด์Šค์ด๋‹ค. ๋”ฐ๋ผ์„œ ํ˜ธํ™˜์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค. ๋งˆ์น˜ v3๋Š” 110v์ด๊ณ , v4๋Š” 220v ์ „๊ธฐ ์ฝ˜์„ผํŠธ ๊ฐ™์€ ๊ฒƒ์ด๋‹ค. ์ด๋Ÿด ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ๋ฐ”๋กœ ์–ด๋Œ‘ํ„ฐ์ด๋‹ค. ์–ด๋Œ‘ํ„ฐ ํŒจํ„ด์„ ์‚ฌ์šฉํ•ด์„œ ํ”„๋ก ํŠธ ์ปจํŠธ๋กค๋Ÿฌ๊ฐ€ ๋‹ค์–‘ํ•œ ๋ฐฉ์‹์˜ ์ปจํŠธ๋กค๋Ÿฌ๋ฅผ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋„๋ก ๋ณ€๊ฒฝํ•ด๋ณด์ž. V5 ๊ตฌ์กฐ - ์–ด๋Œ‘ํ„ฐ ํŒจํ„ด ํ•ธ๋“ค๋Ÿฌ ์–ด๋Œ‘ํ„ฐ: ์ค‘๊ฐ„์— ์–ด๋Œ‘ํ„ฐ ์—ญํ•  , ๋‹ค์–‘ํ•œ ์ข…๋ฅ˜์˜ ์ปจํŠธ๋กค๋Ÿฌ๋ฅผ ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ๋‹ค. ํ•ธ๋“ค๋Ÿฌ: ์ปจํŠธ๋กค๋Ÿฌ์˜ ์ด๋ฆ„์„ ๋” ๋„“์€ ๋ฒ”์œ„์ธ ..

Spring/Spring MVC

[์Šคํ”„๋ง MVC] - 8. ํ”„๋ก ํŠธ ์ปจํŠธ๋กค๋Ÿฌ - ModelView

ModelView - V3 ModelView ์„œ๋ธ”๋ฆฟ์˜ ์ข…์†์„ฑ์„ ์ œ๊ฑฐํ•˜๊ธฐ ์œ„ํ•ด Model์„ ์ง์ ‘ ๋งŒ๋“ค๊ณ , ์ถ”๊ฐ€๋กœ View ์ด๋ฆ„๊นŒ์ง€ ์ „๋‹ฌํ•˜๋Š” ๊ฐ์ฒด 0. HTTP ์š”์ฒญ @WebServlet(name = "frontControllerServletV3",urlPatterns = "/front-controller/v3/*") public class FrontControllerServletV3 extends HttpServlet { /front-controller/v3 ํ•˜์œ„ ๋ชจ๋“  ์š”์ฒญ์€ ์ด ์„œ๋ธ”๋ฆฟ์—์„œ ๋ฐ›์•„๋“ค์ธ๋‹ค. 1. ์ปจํŠธ๋กค๋Ÿฌ ์กฐํšŒ private Map controllerMap = new HashMap(); public FrontControllerServletV3() { controllerMap.put("/front..

Spring/Spring MVC

[์Šคํ”„๋ง MVC] - 7. ํ”„๋ก ํŠธ ์ปจํŠธ๋กค๋Ÿฌ

ํ”„๋ก ํŠธ ์ปจํŠธ๋กค๋Ÿฌ ํŒจํ„ด ํ”„๋ก ํŠธ ์ปจํŠธ๋กค๋Ÿฌ ๋„์ž… ์ „ ํ”„๋ก ํŠธ ์ปจํŠธ๋กค๋Ÿฌ ๋„์ž… ํ›„ FrontController ํŒจํ„ด ํŠน์ง• ํ”„๋ก ํŠธ ์ปจํŠธ๋กค๋Ÿฌ ์„œ๋ธ”๋ฆฟ ํ•˜๋‚˜๋กœ ํด๋ผ์ด์–ธํŠธ์˜ ์š”์ฒญ์„ ๋ฐ›์Œ ํ”„๋ก ํŠธ ์ปจํŠธ๋กค๋Ÿฌ๊ฐ€ ์š”์ฒญ์— ๋งž๋Š” ์ปจํŠธ๋กค๋Ÿฌ๋ฅผ ์ฐพ์•„์„œ ํ˜ธ์ถœ ์ž…๊ตฌ๋ฅผ ํ•˜๋‚˜๋กœ! / ๊ณตํ†ต ์ฒ˜๋ฆฌ ๊ฐ€๋Šฅ ํ”„๋ก ํŠธ ์ปจํŠธ๋กค๋Ÿฌ๋ฅผ ์ œ์™ธํ•œ ๋‚˜๋จธ์ง€ ์ปจํŠธ๋กค๋Ÿฌ๋Š” ์„œ๋ธ”๋ฆฟ์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š์•„๋„ ๋จ V1 ๊ตฌ์กฐ urlPatterns = "/front-controller/v1/*" /front-controller/v1 ๋ฅผ ํฌํ•จํ•œ ํ•˜์œ„ ๋ชจ๋“  ์š”์ฒญ์€ ์ด ์„œ๋ธ”๋ฆฟ์—์„œ ๋ฐ›์•„๋“ค์ธ๋‹ค. private Map controllerMap = new HashMap(); public FrontControllerServletV1() { controllerMap.put("/front-controller/v1..

Spring/Spring MVC

[์Šคํ”„๋ง MVC] - 6. MVC ํŒจํ„ด - ์ ์šฉ , ํ•œ๊ณ„

MVC ํŒจํ„ด - ์ ์šฉ ์„œ๋ธ”๋ฆฟ์„ ์ปจํŠธ๋กค๋Ÿฌ๋กœ ์‚ฌ์šฉํ•˜๊ณ , JSP๋ฅผ ๋ทฐ๋กœ ์‚ฌ์šฉํ•ด์„œ MVC ํŒจํ„ด์„ ์ ์šฉํ•ด๋ณด์ž. Model์€ HttpServletRequest ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. request๋Š” ๋‚ด๋ถ€์— ๋ฐ์ดํ„ฐ ์ €์žฅ์†Œ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š”๋ฐ, request.setAttribute() , request.getAttribute() ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ฐ์ดํ„ฐ๋ฅผ ๋ณด๊ด€ํ•˜๊ณ , ์กฐํšŒํ•  ์ˆ˜ ์žˆ๋‹ค. ํšŒ์› ๋“ฑ๋ก ํผ - ์ปจํŠธ๋กค๋Ÿฌ @WebServlet(name = "mvcMemberFormServlet", urlPatterns = "/servlet-mvc/members/new-form") public class MvcMemberFormServlet extends HttpServlet { @Override protected void service(Http..

Spring/Spring MVC

[์Šคํ”„๋ง MVC] - 5. ์„œ๋ธ”๋ฆฟ๊ณผ JSP์˜ ํ•œ๊ณ„ / MVC ํŒจํ„ด์˜ ๋“ฑ์žฅ

์„œ๋ธ”๋ฆฟ์œผ๋กœ ํšŒ์› ๊ด€๋ฆฌ ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋งŒ๋“ค๊ธฐ @WebServlet(name = "memberSaveServlet",urlPatterns = "/servlet/members/save") public class MemberSaveServlet extends HttpServlet { private MemberRepository memberRepository = MemberRepository.getInstance(); @Override protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("MemberSaveServle..

Spring/Spring MVC

[์Šคํ”„๋ง MVC] - 4. HTTP ์‘๋‹ต ๋ฐ์ดํ„ฐ ( ํ…์ŠคํŠธ / HTML / API )

HttpServletResponse HTTP ์‘๋‹ต ๋ฉ”์‹œ์ง€๋ฅผ ์ƒ์„ฑํ•œ๋‹ค . //[status-line] response.setStatus(HttpServletResponse.SC_OK); setStatus() : ์ƒํƒœ ์ฝ”๋“œ ์ง€์ • ํ•˜๊ธฐ //response.setHeader("Content-Type", "text/plain;charset=utf-8"); response.setContentType("text/plain"); response.setCharacterEncoding("utf-8"); [์ฟ ํ‚ค ํŽธ์˜ ๋ฉ”์„œ๋“œ] private void cookie(HttpServletResponse response) { // Set-Cookie: myCookie=good; Max-Age=600; // response.setHe..

Spring/Spring MVC

[์Šคํ”„๋ง MVC] - 3. HTTP ์š”์ฒญ ๋ฐ์ดํ„ฐ ( GET / POST / API )

HTTP ์š”์ฒญ ๋ฐ์ดํ„ฐ HTTP ์š”์ฒญ ๋ฉ”์‹œ์ง€๋ฅผ ํ†ตํ•ด ํด๋ผ์ด์–ธํŠธ์—์„œ ์„œ๋ฒ„๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋‹ฌํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณด์ž. ์ฃผ๋กœ ๋‹ค์Œ 3๊ฐ€์ง€ ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•œ๋‹ค. GET - ์ฟผ๋ฆฌ ํŒŒ๋ผ๋ฏธํ„ฐ /url?username=hello&age=20 ๋ฉ”์‹œ์ง€ ๋ฐ”๋”” ์—†์ด, URL์˜ ์ฟผ๋ฆฌ ํŒŒ๋ผ๋ฏธํ„ฐ์— ๋ฐ์ดํ„ฐ๋ฅผ ํฌํ•จํ•ด์„œ ์ „๋‹ฌ ์˜ˆ) ๊ฒ€์ƒ‰, ํ•„ํ„ฐ, ํŽ˜์ด์ง•๋“ฑ์—์„œ ๋งŽ์ด ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ์‹ POST - HTML Form content-type: application/x-www-form-urlencoded ๋ฉ”์‹œ์ง€ ๋ฐ”๋””์— ์ฟผ๋ฆฌ ํŒŒ๋ฆฌ๋ฏธํ„ฐ ํ˜•์‹์œผ๋กœ ์ „๋‹ฌ username=hello&age=20 ์˜ˆ) ํšŒ์› ๊ฐ€์ž…, ์ƒํ’ˆ ์ฃผ๋ฌธ, HTML Form ์‚ฌ์šฉ HTTP message body์— ๋ฐ์ดํ„ฐ๋ฅผ ์ง์ ‘ ๋‹ด์•„์„œ ์š”์ฒญ HTTP API์—์„œ ์ฃผ๋กœ ์‚ฌ์šฉ, JSON, XML, TEXT ๋ฐ..

hello_u
'Spring/Spring MVC' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๊ธ€ ๋ชฉ๋ก (2 Page)