디펜던시 설정        
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
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>4.1.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.1.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.1.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.1.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>4.1.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>4.1.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.2.8</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.2.2</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.38</version>
        </dependency>
    </dependencies>
</project>
cs


◎ web.xml에 dispatchServlet을 서블릿으로 등록 *.do에 대해서 처리하도록 서블릿매핑 설정

◎ dispatcherServlet이 사용할 스프링 설정하일 생성

◎ controller패키지에 대해서 컴포넌트 스캔

◎ internalResouceViewResolver등록(prefix는 jsp파을들을 모아놓을 폴더경로, suffix는 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
<?xml version="1.0" encoding="UTF-8"?>

 
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
        </init-param>

        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>
 
</web-app>
cs


1
2
3
4
5
6
7
8
9
10
11
12
13


    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="jsp/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
</beans>
 
cs


- 루트컨테이너 등록하고 사용하기

1. web.xml에 컨텍스트 로더 리스너를 등록

1
2
3
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListner</listener-class>
    </listener>
cs


2. /WEB-INF/applicationContext.xml이름으로 스프링 설정 파일 작성

1
2
3
4
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>
cs


dataSource, sqlSessionFactory, mapperFactoryBean(Dao), service클래스들(컴포넌트스캔), 트랜잭션매니저

dispatcher-servlet.xml 서블릿 컨테이너 <- mvc구성요소들, 컨트롤러(컴포넌트스캔), 뷰리졸버,,,,


((((((( 리스너, 필터 참고 // http://wiki.gurubee.net/pages/viewpage.action?pageId=26740229  )))))

 web.xml에 ContextLoaderListener클래스를 리스너로 등록하면, 

/WEB-INF/applicationContext.xml을 설정파일로해서 스프링 컨테이너를 하나 빌드하고, 

1
    <context:component-scan base-package="service"></context:component-scan>
cs

해당컨테이너를 모든 서블릿 컨테이너가 공유할 수 있도록 세팅함 

- 컨트롤러에서 파라미터 받기

- 컨트롤러에서 request, session, response등 기본객체 사용하기

- 컨트롤러에서 ModelAndView외에 다양한 반환유형


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
package controller;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
 
import service.MemberService;
 
//서비스 주입받아서
//받은 리퀘스트를 처리하기에 필요한 서비스의 메소드를 호출해서 결과 획득
//결과 데이터랑 적절한 페이지 세팅해서 모덴앤뷰 리턴
 
@Controller
public class MemberController {
    @Autowired
    private MemberService service;
    
    @RequestMapping("test.do")
    public ModelAndView dummy(){
        ModelAndView mav = new ModelAndView();
        mav.addObject("msg",service.getData());
        mav.setViewName("test");
        return mav;
    }
    
}
//작성 후, service참조변수에 루트컨테이너에 등록되있는 MemberSerivce빈 객체를 주입
//test.do에서 서비스 객체의 getData함수호출의 결과를 싣고 test.jsp를 뷰 페이지로 지정
 
cs


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package service;
 
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
 
@Service
public class MemberService {
    public String getData(){
        return "What the Hell!";
        
        //작성후 루트컨테이너에 빈으로 등록
    }
}
//@Component : 이 에노테이션이 달려있는 클래스는 컴포턴트 스캔에 의해서 빈으로 등록이 됨
// 그때의 빈의 아이디는 클래스 첫글자 소문자
 
//@Controller, @Service, @Repository
//->모두 @Component의 아들들, 그러므로 얘네들도 컴포넌트스캔에 의해 빈 등록 됨
cs





' IOT 기반 응용 SW과정 > Web Programing' 카테고리의 다른 글

Day65 Spring-student  (0) 2016.06.21
Day64  (0) 2016.06.20
Day62 Spring MVC  (0) 2016.06.16
Day61  (0) 2016.06.15
Day60 Spring-AOP  (0) 2016.06.14

+ Recent posts