맛동산

스프링 핵심만 정리(ing) 본문

Web/Spring

스프링 핵심만 정리(ing)

오지고지리고알파고포켓몬고 2017. 5. 11. 20:48

- spring 기본구조

Main(view) -> Controller 계층 -> Service 계층 -> Dao 계층 -> (DB)계층


Controller - @Controller

Service - @Service

DAO - @Repository <- interface DAO, DAOImpl implements DAO 꼴로 의존성

DTO(VO) - @Component


- 자바 프로젝트에서 jdbc(chap04)

src/main/resources/스프링설정파일.xml을 아래와 같이 구성하면(main에서 똑같이 불러옴)

src/main/resources/config/jdbc.properties 에 db정보를 입력하여 연결 가능

<?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-3.2.xsd">

	<!-- jdbc 속성을 저장한 파일을 대상으로 빈 생성 -->
	<bean
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
			<value>classpath:config/jdbc.properties</value>
		</property>
	</bean>

	<!-- 1. Data Source : Oracle 디비 테이블 연동(JDBC방식) -->
	<bean id="dataSource"
		class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<!-- JDBC Oracle 드라이버 클래스명 설정 -->
		<property name="driverClassName" value="${jdbc.driver}" />
		<!-- JDBC Oracle 접속 문자열 설정 -->
		<property name="url" value="${jdbc.url}" />
		<!-- Oracle 유저 ID 설정 -->
		<property name="username" value="${jdbc.username}" />
		<!-- Oracle 패스워드 설정 -->
		<property name="password" value="${jdbc.password}" />
	</bean>

	<!-- 2. JdbcTemplate 객체 생성(dataSource 이용) -->
	<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
		<constructor-arg ref="dataSource" />
	</bean>

	<!-- 3. 주입 대상 클래스 : <context:component-scan> 태그 적용 -->
	<context:component-scan base-package="jdbc.exam"/>
</beans>


- Spring MVC 프로젝트 구조(chap05)

src/main/webapp/WEB-INF/spring/appServlet/servlet-context.xml

-> 스프링 컨테이너에 쓰던 정보를 씀

-> component-scan, /WEB-INF/views/*.jsp, 예외처리 등 기입

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:beans="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:websocket="http://www.springframework.org/schema/websocket"
	xsi:schemaLocation="http://www.springframework.org/schema/websocket http://www.springframework.org/schema/websocket/spring-websocket-4.1.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
		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.xsd">

	<!-- DispatcherServlet Context: defines this servlet's request-processing 
		infrastructure -->

	<!-- Web 계층 환경설정 파일 -->
	<!-- Enables the Spring MVC @Controller programming model -->
	<annotation-driven />

	<context:component-scan base-package="spring.mvc" />
	

	
	<!-- <websocket:handlers>
        <websocket:mapping path="/websocket/echo.do" handler="myHandler"/>
    </websocket:handlers>

	<beans:bean id="myHandler" class="spring.mvc.socket.SocketHandler"/> -->
	

	<!-- Handles HTTP GET requests for /resources/** by efficiently serving 
		up static resources in the ${webappRoot}/resources directory -->
	<resources mapping="/resources/**" location="/resources/" />

	<!-- Resolves views selected for rendering by @Controllers to .jsp resources 
		in the /WEB-INF/views directory -->
	<beans:bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<!-- view페이지의 시작 폴더라는 것을 의미 -->
		<beans:property name="prefix" value="/WEB-INF/views/" />
		<!-- 확장자 -->
		<beans:property name="suffix" value=".jsp" />
	</beans:bean>

	<!-- 예외 처리 -->

	<beans:bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
		<!-- 애노테이션을 이용한 예외처리가 있을 경우, 우선 순위 적용 -->
		<!-- 1. 애노테이션, 2. Global 설정 -->
		<beans:property name="order" value="1" />
		<beans:property name="exceptionMappings">
			<beans:props>
				<beans:prop key="java.lang.ArithmeticException">
					<!-- 산술적 예외 발생 시 : error/mathException.jsp 실행 -->
					error/mathException
				</beans:prop>
				
				<beans:prop key="java.lang.Exception">
					<!-- 일반적인 예외 발생 시 : error/exception.jsp 실행 -->
					error/exception
				</beans:prop>
			</beans:props>
		</beans:property>
	</beans:bean>
	
	
	
</beans:beans>


src/main/webapp/WEB-INF/web.xml(chap05)

-> 페이지 이동시였나? 아무튼 전체적 encoding 명시

-> *.do를 서블릿에 매핑해줄 수 있음

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

	<!-- 문자셋 인코딩 지정 -->
	<filter>
		<filter-name>CharacterEncodingFilter</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>
		<init-param>
			<param-name>forceEncoding</param-name>
			<param-value>true</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>CharacterEncodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	<!-- 문자셋 인코딩 지정 끝 -->
	
	<!-- The definition of the Root Spring Container shared by all Servlets 
		and Filters -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/spring/root-context.xml</param-value>
	</context-param>
	<!-- 루트는 webapp -->

	<!-- Creates the Spring Container shared by all Servlets and Filters -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

	<!-- Processes application requests -->
	<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/spring/appServlet/servlet-context.xml
				/WEB-INF/spring/appServlet/ws-config.xml	
			</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>appServlet</servlet-name>
		<!-- <url-pattern>/</url-pattern> -->
		<url-pattern>*.do</url-pattern>
	</servlet-mapping>

</web-app>


static한 파일은 src/main/webapp/resources 하위에 넣고

<link rel="stylesheet" type="text/css" href="resources/css/main.css"> 꼴로 호출할 수 있음

Comments