SPRING/스프링 MVC

스프링 MVC 연동

JUMP개발자 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";
    }

}

 

결과