ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 스프링 MVC 연동
    SPRING/스프링 MVC 2021. 6. 6. 01:45

    DispatcherServlet

    • 스프링 MVC의 핵심.
    • Front Controller 역할을 한다.

    DispatcherServlet web.xml 등록

    - /app/ ~으로 url을 입력하면 DispatcheServlet으로 매핑이 된다.

    - Controller로 매핑할 수 있도록 한다.

    <web-app>
      <display-name>Archetype Created Web Application</display-name>
    
      <servlet>
        <servlet-name>app</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
          <param-name>contextClass</param-name>
          <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
        </init-param>
        <init-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>com.springmvc.WebConfig</param-value>
        </init-param>
      </servlet>
    
      <servlet-mapping>
        <servlet-name>app</servlet-name>
        <url-pattern>/app/*</url-pattern>
      </servlet-mapping>
      
    </web-app>

     

    @Configuration
    @ComponentScan(excludeFilters = @ComponentScan.Filter(Controller.class))
    // AppConfig에서는 Controller를 scan X
    public class AppConfig {
    }
    
    @Configuration
    @ComponentScan
    // WebConfig에서 Controller Scan
    public class WebConfig {
    }
    
    @RestController
    public class HelloController {
    
        @Autowired
        HelloService helloService;
    
        @GetMapping("/hello")
        public String hello() {
            return "Hello, " + helloService.getName();
        }
    
    }
    
    @Service
    public class HelloService {
    
        public String getName() {
            return "jumpDeveloper";
        }
    
    }
    

     

    결과

     

     

    'SPRING > 스프링 MVC' 카테고리의 다른 글

    스프링 MVC -WEB.XML 제거  (0) 2021.06.10
    DispatchServlet 분석  (0) 2021.06.06
    스프링 IOC 컨테이너 연동  (0) 2021.06.05
    서블릿 리스너와 필터  (0) 2021.06.05
    Spring MVC란?  (0) 2021.06.02
Designed by Tistory.