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

Coding Planet

SPRING MVC - ๋ทฐ ๋ฆฌ์กธ๋ฒ„ (View Resolver) ๋ณธ๋ฌธ

๐ŸŒฑSPRING

SPRING MVC - ๋ทฐ ๋ฆฌ์กธ๋ฒ„ (View Resolver)

jhj.sharon 2023. 10. 24. 16:25
๋ฐ˜์‘ํ˜•

 

| View Resolver๋ž€

๋ทฐ ๋ฆฌ์กธ๋ฒ„๋Š” ์Šคํ”„๋ง MVC ์›น ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ ์ค‘์š”ํ•œ ์ปดํฌ๋„ŒํŠธ ์ค‘ ํ•˜๋‚˜์ด๋‹ค. MVC ํŒจํ„ด์—์„œ ์ปจํŠธ๋กค๋Ÿฌ๊ฐ€ ์ฒ˜๋ฆฌ๋ฅผ ๋งˆ์นœ ํ›„์— ์–ด๋–ค ๋ทฐ๋กœ ์‘๋‹ต์„ ์ƒ์„ฑํ• ์ง€ ๊ฒฐ์ •ํ•˜๋Š” ์—ญํ• ์„ ํ•œ๋‹ค.

 

| View Resolver์˜ ์—ญํ• 

  • ๋ทฐ ์ด๋ฆ„์˜ ๋งคํ•‘ : ์ปจํŠธ๋กค๋Ÿฌ๋Š” ํŠน์ • ๋ทฐ๋ฅผ ์‹๋ณ„ํ•˜๊ธฐ ์œ„ํ•ด ๋ฌธ์ž์—ด ํ˜•ํƒœ์˜ ๋ทฐ ์ด๋ฆ„์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ๋ทฐ ๋ฆฌ์กธ๋ฒ„๋Š” ์ด ์ด๋ฆ„์„ ์‹ค์ œ ๋ทฐ ํ…œํ”Œ๋ฆฟ ํŒŒ์ผ๋กœ ๋งคํ•‘ํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์ปจํŠธ๋กค๋Ÿฌ๊ฐ€ "home"์ด๋ผ๋Š” ๋ทฐ ์ด๋ฆ„์„ ๋ฐ˜ํ™˜ํ•˜๋ฉด, ๋ทฐ ๋ฆฌ์กธ๋ฒ„๋Š” ์ด๋ฅผ "home.jsp"๋‚˜ "home.html"๊ฐ™์€ ์‹ค์ œํŒŒ์ผ๋กœ ๋ณ€ํ™˜ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ๋‹ค์–‘ํ•œ ๋ทฐ ๊ธฐ์ˆ  ์ง€์› : ์Šคํ”„๋ง MVC๋Š” ๋‹ค์–‘ํ•œ ๋ทฐ ๊ธฐ์ˆ ์„ ์ง€์›ํ•˜๋ฉฐ ์ด๋ฅผ ์œ„ํ•ด ์—ฌ๋Ÿฌ ๋ทฐ ๋ฆฌ์กธ๋ฒ„๋ฅผ ์žฌ๊ณตํ•œ๋‹ค. JSP, Thymeleaf, FreeMarker ๋“ฑ ๋‹ค์–‘ํ•œ ํ…œํ”Œ๋ฆฟ ์—”์ง„์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

 

| ์ฃผ์š” View Resolver

  • InternalResourceViewResolver: JSP์™€ ๊ฐ™์€ ๋ฆฌ์†Œ์Šค๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ฐ€์žฅ ์ผ๋ฐ˜์ ์ธ ๋ทฐ ๋ฆฌ์กธ๋ฒ„. ์ ‘๋‘์‚ฌ์™€ ์ ‘๋ฏธ์‚ฌ๋ฅผ ์„ค์ •ํ•˜์—ฌ ๋ฐ˜ํ™˜๋œ ๋ทฐ ์ด๋ฆ„ ์•ž๋’ค์— ํŠน์ • ๋ฌธ์ž์—ด์„ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • UrlBasedViewResolver: URL ํŒจํ„ด์„ ๊ธฐ๋ฐ˜์œผ๋กœ ๋ทฐ๋ฅผ ๊ฒฐ์ •ํ•˜๋Š” ๋ฆฌ์กธ๋ฒ„
  • ResourceBundleViewResolver: ๋ทฐ ์„ค์ • ์ •๋ณด๋ฅผ ํ”„๋กœํผํ‹ฐ ํŒŒ์ผ์— ์ €์žฅํ•˜๊ณ  ์ด๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ๋ทฐ๋ฅผ ๊ฒฐ์ •ํ•˜๋Š” ๋ฆฌ์กธ๋ฒ„
  • XmlViewResolver: ๋ทฐ ์„ค์ • ์ •๋ณด๋ฅผ XML ํŒŒ์ผ์— ์ €์žฅํ•˜๊ณ  ์ด๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ๋ทฐ๋ฅผ ๊ฒฐ์ •ํ•˜๋Š” ๋ฆฌ์กธ๋ฒ„
  • BeanNameViewResolver: ๋ทฐ์˜ ๋นˆ ์ด๋ฆ„์„ ์ง์ ‘ ์‚ฌ์šฉํ•˜์—ฌ ๋ทฐ๋ฅผ ๊ฒฐ์ •ํ•˜๋Š” ๋ฆฌ์กธ๋ฒ„
  • ContentNegotiatingViewResolver: ํด๋ผ์ด์–ธํŠธ์˜ ์š”์ฒญ ๋‚ด์šฉ(์˜ˆ: Accept ํ—ค๋”)์„ ๊ธฐ๋ฐ˜์œผ๋กœ ์ ์ ˆํ•œ ๋ทฐ๋ฅผ ์„ ํƒํ•˜๋Š” ๋ฆฌ์กธ๋ฒ„

 

| ์˜ˆ์‹œ : UrlBasedViewResolver

  @Bean
    public UrlBasedViewResolver urlBasedViewResolver() {
        UrlBasedViewResolver urlBasedViewResolver = new UrlBasedViewResolver();
        urlBasedViewResolver.setOrder(1);
        urlBasedViewResolver.setViewClass(JstlView.class);
        urlBasedViewResolver.setPrefix("/WEB-INF/jsp/sharon/pj/");
        urlBasedViewResolver.setSuffix(".jsp");
        return urlBasedViewResolver;
    }
  1. setOrder(1):
    • ์—ฌ๋Ÿฌ ๋ทฐ ๋ฆฌ์กธ๋ฒ„๊ฐ€ ์„ค์ •๋œ ๊ฒฝ์šฐ ์ด ์ˆœ์„œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์–ด๋–ค ๋ฆฌ์กธ๋ฒ„๊ฐ€ ๋จผ์ € ์‹œ๋„๋˜๋Š”์ง€ ๊ฒฐ์ •ํ•œ๋‹ค. ๊ฐ’์ด ์ž‘์„์ˆ˜๋ก ๋จผ์ € ์‹œ๋„๋œ๋‹ค.
  2. setViewClass(JstlView.class):
    • ์‚ฌ์šฉํ•  ๋ทฐ ํด๋ž˜์Šค๋ฅผ ์„ค์ •ํ•œ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” JstlView ํด๋ž˜์Šค๊ฐ€ ์‚ฌ์šฉ๋œ๋‹ค. ์ด ํด๋ž˜์Šค๋Š” JSTL ํƒœ๊ทธ๋ฅผ ์ง€์›ํ•˜๋Š” JSP๋ฅผ ๋ Œ๋”๋งํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋œ๋‹ค.
  3. setPrefix("/WEB-INF/jsp/sharon/pj/"):
    • ์ด ์„ค์ •์€ ๋ฐ˜ํ™˜๋œ ๋ทฐ ์ด๋ฆ„ ์•ž์— ์ถ”๊ฐ€๋˜๋Š” ์ ‘๋‘์‚ฌ์ด๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ์ปจํŠธ๋กค๋Ÿฌ์—์„œ "home"์ด๋ผ๋Š” ๋ทฐ ์ด๋ฆ„์„ ๋ฐ˜ํ™˜ํ•˜๋ฉด ์‹ค์ œ JSP ํŒŒ์ผ์˜ ๊ฒฝ๋กœ๋Š” /WEB-INF/jsp/sharon/pj/home.jsp๊ฐ€ ehls๋‹ค.
  4. setSuffix(".jsp"):
    • ์ด ์„ค์ •์€ ๋ฐ˜ํ™˜๋œ ๋ทฐ ์ด๋ฆ„ ๋’ค์— ์ถ”๊ฐ€๋˜๋Š” ์ ‘๋ฏธ์‚ฌdl๋‹ค. ์ ‘๋‘์‚ฌ์™€ ํ•จ๊ป˜ ์‚ฌ์šฉ๋˜์–ด ์‹ค์ œ JSP ํŒŒ์ผ์˜ ๊ฒฝ๋กœ๋ฅผ ๊ฒฐ์ •.
๋ฐ˜์‘ํ˜•
Comments