티스토리 뷰
** spring-mvc **
환경설정
1. spring lib 를 WEB-INF/lib 추가
2. src 아래 log4j 설정파일을 위치 시킨다.
3. web.xml(Deployment Descriptor)에서
Spring의 DispatcherServlet을 등록
--> springmvc config xml을 직접 명시하거나
명시하지 않으면 DispatcherServlet 의 Servlet name을
이용해 springmvc 자신의 설정파일을 찾게 된다.
ex)DispatcherServlet의 servlet name이
dispatcher 이면 디폴트로 dispatcher-servlet.xml을 찾는다.
4. springmvc config xml을 구현
HandlerMapping 정의
ViewResolver 정의
구현한 컨트롤러 빈을 정의
spring mvc
DispatcherServlet <-> HandlerMapping <-> Controller(Interface) <-> model <-> DBCP(DB)
↑ 비영속성 | 영속성
MultiActionController service | dao
↑ |
TestMultiController |
(User Controller) |
find(), insert() |
[실습]
config package
customer.xml
1
2
3
4
5
6
7
8
9
10
11 |
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMap PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-2.dtd">
<sqlMap namespace="customer">
<select id="find" resultClass="hashmap">
select * from customer where ssn=#value#
</select>
<select id="findCustomerBySsn" resultClass="model.CustomerVO">
SELECT * FROM CUSTOMER WHERE ssn = #value#
</select>
</sqlMap> |
SqlMapConfig.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14 |
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMapConfig
PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
<!-- 여러 sql 정의 xml 을 구분해서 사용하기 위해
네임 스페이스를 사용한다.
-->
<settings useStatementNamespaces="true"/>
<!-- spring에서 dbcp 정보를 di하므로 필요없음. -->
<!-- SQL 정의 XML을 링크 -->
<!-- <sqlMap resource="config/member.xml"/> -->
<sqlMap resource="config/customer.xml"/>
</sqlMapConfig> |
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 |
package controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.CustomerService;
import model.CustomerVO;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
public class CustomerController extends MultiActionController {
private CustomerService customerService;
public CustomerController(CustomerService customerService) {
super();
this.customerService = customerService;
System.out.println("controller injection..."+customerService);
}
public ModelAndView findCustomer(HttpServletRequest request,
HttpServletResponse response, CustomerVO cvo) throws Exception{
String ssn = cvo.getSsn();
CustomerVO vo = customerService.findCustomerBySsn(ssn);
ModelAndView mav = new ModelAndView();
if(vo != null){
mav.setViewName("find_ok");
mav.addObject("info", vo);
}else{
mav.setViewName("find_fail");
}
return mav;
}
}
|
1
2
3
4
5
6
7
8
9 |
package model;
import org.springframework.orm.ibatis.SqlMapClientTemplate;
public interface CustomerDao {
public void setSqlMapClientTemplate(SqlMapClientTemplate sqlMapClientTemplate);
public CustomerVO findCustomerBySsn(String ssn) throws Exception;
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 |
package model;
import org.springframework.orm.ibatis.SqlMapClientTemplate;
public class CustomerDaoImpl implements CustomerDao {
private SqlMapClientTemplate sqlMapClientTemplate;
@Override
public void setSqlMapClientTemplate(
SqlMapClientTemplate sqlMapClientTemplate) {
this.sqlMapClientTemplate = sqlMapClientTemplate;
System.out.println("dao injection..."+sqlMapClientTemplate);
}
@Override
public CustomerVO findCustomerBySsn(String ssn) throws Exception {
return (CustomerVO) sqlMapClientTemplate.queryForObject("customer.findCustomerBySsn", ssn);
}
}
|
1
2
3
4
5
6
7 |
package model;
public interface CustomerService {
public void setCustomerDao(CustomerDao customerDao);
public CustomerVO findCustomerBySsn(String ssn) throws Exception;
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 |
package model;
public class CustomerServiceImpl implements CustomerService {
private CustomerDao customerDao;
@Override
public void setCustomerDao(CustomerDao customerDao) {
this.customerDao = customerDao;
System.out.println("service injection..."+customerDao);
}
@Override
public CustomerVO findCustomerBySsn(String ssn) throws Exception {
return customerDao.findCustomerBySsn(ssn);
}
}
|
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 |
package model;
public class CustomerVO {
private String ssn;
private String name;
private String address;
public String getSsn() {
return ssn;
}
public void setSsn(String ssn) {
this.ssn = ssn;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "CustomerVO [ssn=" + ssn + ", name=" + name + ", address="
+ address + "]";
}
}
|
log4j.properties
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 |
log4j.rootLogger = warn,stdout
log4j.additivity.controller=false
log4j.category.controller=debug,stdout
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p ({%t} %F[%M]:%L) [%d] - %m%n
log4j.appender.dailyfile = org.apache.log4j.DailyRollingFileAppender
log4j.appender.dailyfile.File = C:\\java-kosta\\logfile.log
log4j.appender.dailyfile.Append = true
log4j.appender.dailyfile.DatePattern='.'yyyy-MM-dd
log4j.appender.dailyfile.layout = org.apache.log4j.PatternLayout
log4j.appender.dailyfile.layout.ConversionPattern=%5p ({%t} %F[%M]:%L) [%d] - %m%n |
WEB-INF 폴더 아래
dispatcher-servlet.xml
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
69
70
71
72
73
74 |
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"
>
<!-- HandlerMapping : 클라이언트 요청에 대응하는 컨트롤러 매핑 -->
<bean id="handlerMapping"
class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping">
</bean>
<!-- ViewResolver : 컨트롤러 수행 후 view 를 찾는 방식에 대한 정의 ModelAndView -> ok 로 셋팅하면
/ok.jsp 를 찾는다. -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<bean id="methodNameResolver"
class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">
<property name="paramName">
<value>command</value>
</property>
</bean>
<!-- dataSource -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName">
<value>oracle.jdbc.driver.OracleDriver</value>
</property>
<property name="url">
<value>jdbc:oracle:thin:@127.0.0.1:1521:xe</value>
</property>
<property name="username">
<value>spring</value>
</property>
<property name="password">
<value>oracle</value>
</property>
<property name="maxActive">
<value>30</value>
</property>
</bean>
<!-- ibatis SqlMapClient -->
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<!-- ibatis 설정파일을 한 곳에 위치시킨다. -->
<property name="configLocation">
<value>classpath:config/SqlMapConfig.xml</value>
</property>
</bean>
<!-- ibatis와 연동하기 위한 Spring에서 제공하는 템플릿 오브젝트: 이후 spring aop transaction 등을
위해 사용 -->
<bean id="sqlMapClientTemplate" class="org.springframework.orm.ibatis.SqlMapClientTemplate">
<property name="sqlMapClient" ref="sqlMapClient" />
</bean>
<!-- 1. Dao 2. Service 3. Controller -->
<bean id="customerDao" class="model.CustomerDaoImpl">
<property name="sqlMapClientTemplate" ref="sqlMapClientTemplate"></property>
</bean>
<bean id="customerService" class="model.CustomerServiceImpl">
<property name="customerDao" ref="customerDao"></property>
</bean>
<bean name="/customer.do" class="controller.CustomerController">
<constructor-arg>
<ref bean="customerService"/>
</constructor-arg>
<property name="methodNameResolver" ref="methodNameResolver"></property>
</bean>
</beans> |
web.xml
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 |
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>springmvc1</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>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!-- .do로 요청되는 모든 클라이언트의 요청은 spring DispatcherSerlvet이 처리한다.
별개의 spring config xml에 대한 설정이 없는 경우
[서블릿이름]-servlet.xml로 찾게 된다.
-->
<!-- 모든 요청에 대한 인코딩 처리를 Spring이 제공하는 필터에서 처리 -->
<filter>
<filter-name>encodeFilter</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>encodeFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app> |
'Framework > Spring' 카테고리의 다른 글
[Spring & ibatis] 수업 10일-Ajax (0) | 2013.05.03 |
---|---|
[Spring & ibatis] 수업 9일-Spring MVC,ibatis (0) | 2013.05.02 |
[Spring & ibatis] 수업 7일-2(web) (0) | 2013.04.30 |
[Spring & ibatis] 수업 7일-1(ibatis 끝) (0) | 2013.04.30 |
[Spring & ibatis] 수업 6일 (0) | 2013.04.29 |