Spring MVC 구조

2022. 7. 26. 16:32Spring

728x90

아래 그림은 Spring MVC구조의 흐름이다.

 

1 . 클라이언트의 모든 " *.do "요청을 DispatcherServlet이 받는다.

아래 코드는 WEB-INF/web.xml파일 안의 코드이다. => DispatcherServlet 등록

<servlet>
    <servlet-name>action</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>*.do</url-pattern>
</servlet-mapping>

위 코드는 *.do 라고 요청을 서버에 전달하면  컨테이너는 action 이라는 이름으로 등록된 DispatcherServlet 클라스 객체를 생성한다는 뜻이다.

 

 

2,3 DispatcherServlet은 HandlerMapping을 통해 요청을 처리할 Controller를 검색한 후 실행한다.

DispatcherServlet 객체가 생성되면 DispatcherServlet 객체 안의 init()메서드가 실행된다.

init() 메서드는 스프링 설정 파일(presentation-layer.xml) (원래는 action-servlet.xml)을 로딩하여 xmlWebApplicationContext(스프링 컨테이너)를 생성한다.

 

본래는 DispatcherServlet은 Spring 컨테이너를 구동할 때 [서블릿이름-servlet.xml]파일을 찾는다.

다른 파일을 등록하고 싶을 때는 web.xml에 아래코드처럼 코드를 작성하여야한다.

 

<servlet>
    <servlet-name>action</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/config/presentation-layer.xml</param-value>
    </init-param>
</servlet>

 

 

아래는 presentation-layer.xml 파일안 코드이다.

<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
		<property name = "mappings">
			<props>
				<prop key="/login.do">login</prop>
			</props>
		</property>
	</bean>
	
	<bean id="login" class="sonny.spring.web.User.LoginController"/>
</beans>

 

위 코드는 HandlerMapping, Controller 클래스를 <bean> 등록하여 스프링 컨테이너가 해당 객체들을 자동으로 생성해주는 코드이다.

 

=> Servlet 컨테이너 안에 있는 DispatcherServlet과 Spring 컨테이너 안에 있는 HandlerMapping, Controller, ViewResolver 객체들과 상호작용 한다.

 

 

 

4.  Controller는 비즈니스 로직 수행 결과로 얻어낸 Model 정보와 View 정보를 ModelAndView 객체에 저장하여 리턴한다.

 

아래는 LoginController 클래스이다.

 

public class LoginController implements Controller {

	@Override
	public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) {
		System.out.println("로그인처리");
		// 1. 사용자 입력 정보 추출
		String id = request.getParameter("id");
		String password = request.getParameter("password");
		
		// 2. 데이터베이스 연동처리
		UserVO vo = new UserVO();
		vo.setId(id);
		vo.setPassword(password);
		
		UserDAO userDAO = new UserDAO();
		UserVO user = userDAO.getUser(vo);
		
		// 3. 화면 네비게이션
		ModelAndView mav = new ModelAndView();
		if(user != null){
			mav.setViewName("getBoardList.do");
		}else{
			mav.setViewName("login.jsp");
		}
		return mav;
	}

 

5,6  DispatcherServlet은 ModelAndView로부터 View 정보를 추출하고 ViewResolver를 이용하여 응답으로 사용할 View를 얻고 이를 실행하여 응답을 전송한다.

 

 

 

 

참고) 인코딩 설정

 

 

web.xml파일

	<filter>
		<filter-name>encodingFilter</filter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>utf-8</param-value>
		</init-param>
	</filter>

	<filter-mapping>
		<filter-name>encodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

 

 

 

 

728x90

'Spring' 카테고리의 다른 글

Spring MVC 구조(2)  (0) 2022.07.29
Spring - BeanCreationException해결 / .metadata  (0) 2022.07.26
Spring - 트랜잭션 처리  (0) 2022.07.21
Spring - AOP  (0) 2022.07.19
의존성 관리 -2  (0) 2022.07.15