재미있게 코딩합시다

MVC 본문

IT /Spring(스프링)

MVC

감민셔 2019. 1. 14. 15:59

Spring MVC 구성 주요 컴포넌트  (스프링 컨테이너 등장인물들)


• DispatcherServlet     (모든 요청을 받아들이는 역할)

– Front Controller 

– 프론트 컨트롤러 로써 처음으로 요청 받아서 해당 요청이 어느 컨트롤러에 의해 처리될지 결정

• Controller (요청에 대한 실제처리를 하는 역할)

– 클라이언트 요청 처리를 수행하는 Controller. 

– 실제 요청을 처리하는 역할

• HandlerMapping (어떤 요청 들어왔을 때 어떤 컨트롤러가 처리할지 결정)

– 클라이언트의 요청을 처리할 Controller를 찾는 작업 처리

– 어떤요청을 어떤 컨트롤러가 수행할지 결정

• View 

– 응답하는 로직을 처리 

• ViewReslover 

– 응답할 View를 찾는 작업을 처리 

• ModelAndView  (응답에 사용할 데이터와 페이지의 정보의 집합)

– 응답할 View와 View에게 전달할 값을 저장하는 용도의 객체

– 컨트롤러의 처리 결과로서 응답에 사용할 데이터와 뷰에 대한 정보 집합


1.Web.xml

- servlet을 등록(dispatcher-servlet.xml)

- mapping해서 처리할것도 설정


<?xml version="1.0" encoding="UTF-8"?>         <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>Spring_member</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <!--listener를 상속받아서 구현한 모든 클래스는 리스너가 될수잇다 --> <!-- 이벤트 요청이 되면 설정 정보를 읽어들이는 부분 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </context-param> </web-app>


2. applicationContext.xml

- dataSource

- sqlSessionFactory

- mapperFactoryBean

- service클래스(scan,@service)

- 트랜잭션 설정


<?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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd"> <context:component-scan base-package="service"/> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/xe"/> <property name="username" value="root"/> <property name="password" value="mysql"/> </bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="mapperLocations" value="classpath*:/dao/mapper/**/*.xml"/> <property name="typeAliasesPackage" value="model"/> </bean> <bean id="memberDao" class="org.mybatis.spring.mapper.MapperFactoryBean"> <property name="sqlSessionFactory" ref="sqlSessionFactory"/> <property name="mapperInterface" value="dao/IMemberDao"/> </bean> <!-- txManager를 선언만 --> <!-- 트랙잭션을 선언해주는 이유는 바로 추가변경 이런것들을 관리해주기 위해서 --> <!-- 관리해주는 것 ==트랙잭션 이라서 --> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <!-- txManager 선언해준대 다가 --> <tx:advice transaction-manager="txManager" id="mytx"> <!-- attributes << 집어넣어준다 --> <tx:attributes> <!-- 모든 속성들을 (insert, selectAll 같은것들)--> <tx:method name="*"/> </tx:attributes> </tx:advice> <!-- service팩트리에 ~service를 실행한다.--> <aop:config> <aop:advisor advice-ref="mytx" pointcut="execution(public * service.*Service.*(..))"/> </aop:config> </beans>


3. dispatcher-servlet.xml

- 컴포넌트 스캔 설정

- internalResourceViewResolver등록

- view이름으로부터 jsp나 tiles연동을 위한 view 리턴

-기본적인 view 클래스

-prefix

-suffix

- mvc구성요소들

- (scan, @controller)


<?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: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/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">         <!-- dispatcherServlet이 사용할 스프링 컨테이너 설정파일 입니다. --> <!-- @RequestMapping 요청에 대해 어떤 Controller, 어떤 메소드가 처리할지를 맵핑하기 위한 어노테이션 --> <!--controller패키지에 대해 컴포넌트 스캔 --> <context:component-scan base-package="controller"/> <!--view 클레스 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="jsp/"></property> <!-- jsp / 패키지 안에 --> <property name="suffix" value=".jsp"></property> <!-- .jsp 로 시작되는애 찾아서 view 로 등록--> </bean> </beans>

설정 해줘야한다.



Comments