오늘은 몰랐으면 내일은 알면 된다
2022-12-14 (3) 스프링 MVC 본문
스프링 MVC는 스프링의 서브 프로젝트이다. 아래의 링크를 타고 들어가보면, 다양한 프로젝트들이 있는 것을 확인할 수 있다.
Spring | Projects
Spring Framework Provides core support for dependency injection, transaction management, web apps, data access, messaging, and more.
spring.io
스프링은 '코어' 프레임워크에 여러 서브 프로젝트를 결합해서, 다양한 상황에 대처할 수 있도록 개발되었다.
[스프링 MVC 프로젝트의 내부 구조]
스프링 MVC 프로젝트를 구성해서 사용한다는 의미는, 내부적으로는
1. POJO(Plain Old Java Object)영역인 ApplicationContext(xml 또는 .java)와
2. ServletContext(xml 또는 .java)로 설정하는 Web 관련 영역을
같이 연동해서 구동하게 된다.
public class WebInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[] {MyApplicationContext.class};
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[] {MyServletContext.class};
}
@Override
protected String[] getServletMappings() {
return new String[] {"/"};
}
}
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/myApplicationContext.xml</param-value>
</context-param>
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/myServletContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
즉 ServletContext에 대한 설정파일이 있어야만 Web Application Context로 구동이 가능하다. ApplicationContext만 있으면 그냥 스프링 컨테이너가 되어버린다.
스프링의 원래 목적 자체가 웹 애플리케이션을 목적으로 나온 프레임워크가 아니기 때문에, 달라지는 영역에 대해서는 완전히 분리하고 연동하는 방식으로 구현되어 있다.
[Java 설정을 이용하는 경우]
pom.xml 에 web.xml 이 없다는 설정을 추가한다.
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.3</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
web.xml 을 대신하려면 AbstractAnnotationConfigDispatcherServletInitializer를 상속받아야 한다.
public class WebInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
그리고 ApplicationContext 설정파일과 ServletContext 설정파일을 등록해준다.(위에있음)
ServletContext 설정파일에서 @EnableWebMvc는 mvc:annotation-driven과 같은 효과이다.
@Configuration
@ComponentScan(basePackages = {"control"})
@EnableWebMvc
public class MyServletContext implements WebMvcConfigurer {
<mvc:annotation-driven></mvc:annotation-driven>
[스프링 MVC의 기본 사상]
스프링 MVC는 Servlet/JSP API와 관련된 부분은 개발자들에게 보여주지 않고, 개발자들은 필요한 부분에 집중해서 개발할 수 있는 구조로 되어있다.
Servlet/JSP는 HttpServletRequest/HttpServletResponse라는 타입의 객체를 이용해 브라우저에서 전송한 정보를 처리한다. 스프링 MVC의 경우 이 위에 하나의 계층을 더한 형태가 된다.

따라서 개발자들이 Servlet/JSP API를 직접적으로 사용할 필요성이 줄어든다.
[스프링 MVC의 Controller]
Controller는 다음의 특징을 가진다.
1.HttpServletRequest/HttpServletResponse를 거의 사용할 필요 없이 필요 기능 구현
2.다양한 타입의 파라미터, 리턴 타입 사용 가능
3.GET 방식, POST 방식 등 전송 방식에 대한 처리를 어노테이션으로 처리 가능
4.상속/인터페이스 방식 대신에 어노테이션만으로 필요한 설정 가능
[@RequestMapping]
RequestMapping 어노테이션은 클래스 선언부에도 작성 가능하다.
@Controller
@RequestMapping("/sample/*")
public class fooController {
@RequestMapping("")
public void basic(){}

basic 메소드는 sample/ 뒤에 아무것도 적지 않을 때 요청된다.
[RedirectAttribute]
spring에서 다른 페이지로 redirect: 를 통해 페이지 이동을 할 수 있는데, 사용하지 않는다.
'Java > JAVA 개발자 양성과정' 카테고리의 다른 글
| 2022-12-15 (1) Controller Advice (0) | 2022.12.15 |
|---|---|
| 2022-12-14 (4) Controller의 Exception 처리 Advice (0) | 2022.12.14 |
| 2022-12-14 (2) myBatis xml Java 클래스 변환 (0) | 2022.12.14 |
| 2022-12-14 (1) OrderMapper.xml 만들기 (0) | 2022.12.14 |
| 2022-12-13 (2) MyBatis 연동 (0) | 2022.12.13 |