맛동산

스프링 핵심만 정리(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정보를 입력하여 연결 가능

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<?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>¬
¬
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX


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

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

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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?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>¬
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX


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

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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?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>¬
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX


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

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

Comments