๐ŸŒธ [SPRING] | dispatcherServlet.xml

1


๐Ÿ”น dispatcherServlet.xml

โœ… <beans></beans> ์•ˆ์—์„œ ์ž‘์„ฑ๋ผ์š”.

โœ… <mvc:annotation-driven />

  • MVC ํŒจํ„ด ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ์œผ๋ฉด ๊ผญ ์ ์–ด์ค˜์•ผ ํ•ด์š”.
  • ์ฆ‰, @Controller ์• ๋…ธํ…Œ์ด์…˜์„ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ์œผ๋ฉด ์ ์–ด์ค˜์•ผ ํ•ด์š”.

โœ… <context:annotation-config/>

  • ์• ๋…ธํ…Œ์ด์…˜์„ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ์œผ๋ฉด ๊ผญ ์ ์–ด์ค˜์•ผ ํ•ด์š”.
  • ๋ฉค๋ฒ„ํ•„๋“œ์™€ ๋ฉ”์†Œ๋“œ์˜ ์• ๋…ธํ…Œ์ด์…˜์„ ํ™œ์„ฑํ™”ํ•ด์š”.

โœ… <context:component-scan/>

<context:component-scan base-package="com.koreait.spring" />

  • ์• ๋…ธํ…Œ์ด์…˜์œผ๋กœ ๋นˆ ๋“ฑ๋กํ•  ํด๋ž˜์Šค๋“ค์„ ์Šค์บ”ํ•ด์ฃผ๋Š” ์‹œ์ž‘ ํŒจํ‚ค์ง€๋ฅผ ์„ค์ •ํ•ด์š”.
  • ํด๋ž˜์Šค ์–ด๋…ธํ…Œ์ด์…˜์„ ํ—ˆ์šฉํ•ด์š”.
  • ์‹œ์ž‘ํŒจํ‚ค์ง€๋ฅผ ์„ค์ •ํ•ด์š”.

โœ… <mvc:resources/>

<mvc:resources mapping="/res/**" location="/res/" />

  • web.xml์˜ <servlet>์„ค์ •์˜ ์˜ˆ์™ธ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์ฃผ๋Š” ๊ฑฐ์˜ˆ์š”.
  • web.xml ์˜ <servlet-mapping>์ด /๋กœ ์‹œ์ž‘ํ•˜๋Š” ๋ชจ๋“  ์š”์ฒญ์„ ๋ฐ›์•„์ฃผ๊ณ , <servlet>์ด ๊ทธ ์š”์ฒญ์„ ๋ฐ›์•„์ฃผ๋Š” controller์™€ ๋งคํ•‘์‹œ์ผœ์š”.
  • ๊ทธ๋Ÿฐ๋ฐ css๋‚˜ js๋‚˜ img๋Š” controllerํ•„์š”์—†์–ด์š”.
  • ๊ทธ๋ž˜์„œ ์˜ˆ์™ธ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์ฃผ๋Š” ๊ฑฐ์˜ˆ์š”.
  • web.xml์˜ <servlet>์ด /๋กœ ์‹œ์ž‘ํ•˜๋Š” ๋ชจ๋“  ์š”์ฒญ์„ ๋ฐ›์•„์ฃผ๋Š” ๋ฐ, /res/๋กœ ์‹œ์ž‘ํ•˜๋Š” ์š”์ฒญ์ด ๋“ค์–ด์˜ค๋ฉด web.xml์˜ <servlet>์ด ๊ด€์—ฌํ•˜์ง€ ์•Š๊ณ  ์—ฌ๊ธฐ์„œ ๋‹ด๋‹นํ•ด์„œ "webapp"์˜ /res/ํด๋” ๋กœ ์—ฐ๊ฒฐ์‹œํ‚ค๊ฒ ๋‹ค๋Š” ๋œป์ด์˜ˆ์š”.
  • **๋Š” ๋ช‡ ์ฐจ ์ฃผ์†Œ๊ฐ€ ์žˆ๋˜ ์ƒ๊ด€์—†๋‹ค๋Š” ๋œป์ด์˜ˆ์š”.

โœ… <bean InternalResourceViewResolver/>

<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>

  • ์‚ฌ์šฉํ•  ํŒŒ์ผ๋ช…์„ ์™„์„ฑํ•ด์ฃผ๋Š” ๊ฐ์ฒด์˜ˆ์š”.
  • @RequestMapping์˜ return๊ฐ’์˜ ์•ž๋ถ€๋ถ„์—๋Š” /WEB-INF/views/๋ฅผ ๋ถ™์ด๊ณ  ๋’ท๋ถ€๋ถ„์—๋Š” .jsp๋ฅผ ๋ถ™์—ฌ์ค˜์š”.

โœ… <bean MappingJackson2HttpMessageConverter/>

<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>

  • Jackson ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ pom.xml์—์„œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์„ค์น˜ํ•˜๊ณ  ์—ฌ๊ธฐ์„œ bean๋“ฑ๋ก์„ ํ•ด์•ผ์ง€ ์“ธ ์ˆ˜ ์žˆ๋‹ค

๐Ÿ”น ์ „์ฒด์ฝ”๋“œ

๐Ÿ‘† ๋งํฌ๋ฅผ ๋ˆ„๋ฅด๋ฉด ์†Œ์Šค๋ฅผ ๋ณผ ์ˆ˜ ์žˆ์–ด์š”.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/mvc
       https://www.springframework.org/schema/mvc/spring-mvc.xsd
       http://www.springframework.org/schema/context
       https://www.springframework.org/schema/context/spring-context.xsd">
    <mvc:annotation-driven/> <!--mvc ํŒจํ„ด์„ ์“ฐ๊ณ  ์‹ถ์œผ๋ฉด ๊ผญ ์ค˜์•ผ ํ•œ๋‹ค.-->
    <context:annotation-config/> <!--์–ด๋…ธํ…Œ์ด์…˜ ํ™œ์„ฑํ™” ๋ฉค๋ฒ„ํ•„๋“œ, ๋ฉ”์†Œ๋“œ-->

    <!-- ํด๋ž˜์Šค ์–ด๋…ธํ…Œ์ด์…˜ ํ—ˆ์šฉ, ์‹œ์ž‘ํŒจํ‚ค์ง€ ์„ค์ •-->
    <context:component-scan base-package="com.koreait.first" />

    <!--
    **๋Š” ๋ช‡ ์ฐจ ์ฃผ์†Œ๊ฐ€ ์žˆ๋˜ ์ƒ๊ด€์—†๋‹ค๋Š” ๋œป
    web.xml <servlet-mapping>์ด /๋กœ ์‹œ์ž‘ํ•˜๋Š” ๋ชจ๋“  ์š”์ฒญ์„ <servlet>์˜ dispatcher๊ฐ€ ๋‹ค ๋ฐ›์•„์„œ handlermapping์ด ๊ทธ ์š”์ฒญ์„ ๋ฐ›์•„์ฃผ๋Š” controller๊ฐ€ ์žˆ๋Š”์ง€ ๋ฌผ์–ด๋ณด๊ณ  ๋งคํ•‘์‹œํ‚จ๋‹ค.
    ๊ทธ๋Ÿฐ๋ฐ css๋‚˜ js๋‚˜ img๋Š” controllerํ•„์š”์—†๋‹ค
    /res/๋กœ ์‹œ์ž‘ํ•˜๋Š” ์š”์ฒญ์ด ๋“ค์–ด์˜ค๋ฉด dispatcher๊ฐ€ ๊ด€์—ฌํ•˜์ง€ ์•Š๊ณ  ์–˜๊ฐ€ ๋‹ด๋‹นํ•ด์„œ "webapp"์— /res/ํด๋” ๋กœ ์—ฐ๊ฒฐ์‹œํ‚ค๊ฒ ๋‹ค
    -->
    <mvc:resources mapping="/res/**" location="/res/" />

    <!-- ์‚ฌ์šฉํ•  ํŒŒ์ผ๋ช…์„ ์™„์„ฑํ•ด์ฃผ๋Š” ๊ฐ์ฒด -->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/" />
        <property name="suffix" value=".jsp" />
    </bean>

    <!--pom.xml์—์„œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์„ค์น˜ํ•˜๊ณ  ์—ฌ๊ธฐ์„œ bean๋“ฑ๋ก์„ ํ•ด์•ผ์ง€ ์“ธ ์ˆ˜ ์žˆ๋‹ค-->
    <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>



</beans>
profile
๋ช‡ ๋ฒˆ์„ ๋„˜์–ด์ ธ๋„ ์•ž์œผ๋กœ ๊ณ„์† ๋‚˜์•„๊ฐ€์ž

0๊ฐœ์˜ ๋Œ“๊ธ€