• spring xml파일 설정

    2017. 9. 21. 17:00

    by. 위지원

    mvc,servlet의존성 추가를 하고 진행..http://weejw.tistory.com/113



    나는  spring을 처음 접하면서 제일 어려웠던게 대체 페이지가 어떤 경로로 열리는것인가? 였는데 jsp할때는 그냥 뚝딱 넣어두면 그이름으로 뚝딱...

    사실 뒤에 어떠한 설정이 있을테지만.. 자동으로 되었어서.....난이게 너무어려웠다. 한번 공부해보자


    일단 전체적인 모양은



    xml의 설정 파일에 대해서 알아보자 출처 : http://nanstrong.tistory.com/231


    우선 플젝에 보면 4가지가 있다.

    -pom.xml

    -web.xml

    -root-context.xml

    -servlet-context.xml


    보통 이 파일들의 경로는


    pom,web같은 경우는 원래 있던곳에 그대로 있을것이고 나머지는


    -webapp

    WEB-INF

    -spring

    -appServlet

    -servlet-context.xml

    -root-context.xml


    이렇게 되어있다.하나하나 알아보자면

    web.xml : 서버의 시작지점

    root-context : 프로젝트의 어플리케이션 영역 설정 

    servlet-context : 서블릿 영역을 설정


    *작은 프로젝트 같은경우에는 root,servlet을 나누지 않아도 상관없지만 굳이 나누는 이유는 jsp scope와 관련되어있다 한다.

    자 예를들어 a,b 두사용자가 있을때 어떤 jsp에 대해서 어떤자원은 한 사용자에 있어서 차단하고 어떤 자원은 두사용자 모두에게 공유된다하자.

    이럴때 root-context에 공용으로 사용하는 자원을 설정하는것이다 ( 이를테면 디비같은것 )


    pom.xml :많이 써봣으니 .... 음 메이븐관리해주는 칭규 


    자 그럼 구동과정을 알아보자 출처 : http://doublesprogramming.tistory.com/84


    servlet-context.xml  파일 

    java 파일 

    jsp 파일

    web.xml 파일


    이렇게 4가지가 있을 때 어떻게 돌아가느냐 하면 

    위에서 알아본봐와 같이 시작지점인 

    1.web.xml을 먼저 들른다 : 클라이언트의 요청을 받아 들인다


    2.그다음에 web.xml의 dispatcherServlet가 클라이언트의 요청을 핸들링하여 servlet-context.xml로 가서  HandlertMapping Controller로 검색해서 요청을 처리할 수 있는 컨트롤러를 찾아 컨트롤러를 사용하여 요청을 처리해준다


    **distpathcerServlet이란 출처 : http://egloos.zum.com/springmvc/v/504151#type=comment&page=3 ( 출처에가면 훨씬 자세하다 )

    model controller view  이 세파트를 조합하여 브라우저로 출력하는게 역할인 클래스이다.

    -클라이언트 -> 디스패처서블릿 : 클라이언트가  url로 접근하여 정보를 요청한다

    -디스패처서블릿 -> 핸들러매핑 : 디스패처가 핸들러매핑한테 요청이랑 매핑된 컨트롤러가 있나요? 라고 물어본다

    -핸들러매핑 -> 컨트롤러 : 핸들러매핑이 컨트롤러에게 해주세요 하고 말한다

    -컨트롤러 -> 디스패처서블릿 : 요청을 처리하고 결과를 출력할 뷰의 이름을 알려준다

    -디스패처서블릿 -> viewResolver  : 컨트롤러가 준 뷰이름이 있나요? 라고 물어본다

    -viewResolver -> 뷰 :  Resolver는 뷰에게 뷰를 준다

    -뷰 -> 디스패처서블릿 : 뷰는 처리결과가 포함된 뷰를 디스패처에게 준다.

    -디스패처서블릿->클라이언트 : 결과를 준다



    3.java에서 이 요청을 처리해준다음에 jsp를 리턴해서 jsp파일이 view에 출력되는것이다.



    servlet-context.xml


    <?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"
        xsi:schemaLocation="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">
        <annotation-driven />
        <resources mapping="/resources/**" location="/resources/" />
        <beans:bean
            class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <beans:property name="prefix" value="/WEB-INF/views/" />
            <beans:property name="suffix" value=".jsp" />
        </beans:bean>
        <context:component-scan base-package="com.spring.web" />

    </beans:beans>



    root-context.xml


    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">
           
    </beans>



    web.xml


    <?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">
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/root-context.xml</param-value>
        </context-param>
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
        <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</param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
        </servlet>
        <servlet-mapping>
            <servlet-name>appServlet</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>

    </web-app>




    설정이 잘되었는지 테스트해보자


    이렇게 간단하게 return으로 home을 주는 메서드를 하나 만들고




    해당 home.jsp는 view 아래에 있다. 내용은 별거 없다.





    그리고서 실행해보면

    아주 잘된다^0^!!!



    만약안되면  여러가지 이유가 있다고 하는데 이부분을 주의하면 될것같다 나는 여기서 해결봤다.



      <beans:bean
            class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <beans:property name="prefix" value="/WEB-INF/views/" />
            <beans:property name="suffix" value=".jsp" />
        </beans:bean>
        <context:component-scan base-package="com.spring.web" /> #위의 프로젝트 경로 기준으로 "com.cslab.home"으로 변경해서 했다.



                  


    조금더 응용해서 해봐도 별탈 없다 ㅎㅎㅎㅎㅎ


    '2017년 > Spring' 카테고리의 다른 글

    스프링 퀵 스타트 2일차  (0) 2017.10.11
    스프링 퀵 스타트 1일차  (0) 2017.09.27
    tiles를 알아보자  (0) 2017.09.22
    spring으로 화면에 HelloWorld를 띄어보자  (0) 2017.09.21
    의존성 추가 페이지 링크  (0) 2017.09.19

    대화의 장 💬