웹 개발/스프링

스프링(2) - 프로젝트 구조

SolartheNomad 2023. 3. 25. 14:05

👨‍💻 스프링 MVC 프로젝트 구조 

 

🐥 서블릿 관련 폴더 (src/main/Java)

- 자바 파일을 관리함

-  src/main/Java 폴더에 등록

- 개발자가 만든 예외 클래스나 기타 유틸 클래스 파일 등이 들어감

 

🐥 웹 관련 폴더 (src/main/webapp)

- 웹 파일을 관리함

- 웹과 관련된 JSP, 리소스, 스프링 MVC 환경 설정 파일, 웹 프로젝트 환경 설정 파일 등이 들어감

 

 resources 폴더

- 웹에 관련된 이미지, 자바스크립트, CSS 등 정적 리소스 파일을 관리

 

 spring 폴더

-  (Bean) 객체들을 등록하는 서블릿 설정 파일인 스프링 MVC 설정 파일(기본형: 서블릿 이름-servlet.xml)을 관리-  스프링 시큐리티 설정 파일 같은 스프링 관련 설정 파일들을 저장함

 

views 폴더

- 웹 페이지인 JSP 파일을 관리함

 

src/main/webapp/WEB-INF 폴더

 

ㄴweb.xml 파일

- 웹 프로젝트의 설정 파일

- 리스너, 서블릿 필터 등을 설정할 수 있도록 함 

- 웹 프로젝트의 배포 설명자/배치 기술서(deployment descriptor)라고 불림 

- 웹 프로젝트가 배포되는 데 이용되는 XML 형식의 자바 웹 애플리케이션 환경 설정 부분

 

<web-app> </web-app> 안에 환경 설정 관련 태그들이 들어감 

 

📌 네임스페이스 

코드에서 이름은 같지만 내용이 전혀 다른 요소와 충돌하지 않도록, 즉 이런 요소를 구별하는 데 사용함

 

📌 스키마 

- 코드의 구조와 요소, 속성의 관계를 정의하여 다양한 자료형을 사용할 수 있도록 정의된 문서 구조를 의미함 

 

<?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">

🔗 xmlns="http://java.sun.com/xml/ns/javaee"

- 기본 네임스페이스 선언

- xmlns 속성은 기본 XML 스키마 네임 스페이스를 명시함

 

🔗 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 

- 인스턴스 네임 스페이스 URI 선언

-  xmlns:xsi 속성은 인스턴스 네임 스페이스 URI를 지정함

- 대부분 XML 스키마로 사용되는 표준인 W3C(World Wide Web Consortium) XML 스키마를 가져와 표현함

 

🔗 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
                        http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

 

Oracle Access Manager Operation Error

Oracle Access Manager Operation Error The WebGate plug-in is unable to contact any Access Servers. Contact your website administrator to remedy this problem.

www.oracle.com

-  xsi:schemaLocation 속성은 참조하고자 하는 인스턴스 문서의 URL을 지정

-  두 개의 속성 값은 공백으로 구분

-  첫 번째 URL : 기본 네임 스페이스와 동일. 사용할 네임스페이스와 동일(xmlns와 동일하다.)

- 두 번째 URL :  참조할 스키마 파일 이름

 

 

📌 루트 컨텍스트 설정 

- 공통 빈(Service, Repository(DAO), DB, Log 등)을 설정

-  View 지원을 제외한 bean을 설정함

 

- <context-param></context-param>

기본 설정 외에 사용자가 직접 제어하는 XML 파일을 지정하기

<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
    //스프링 MVC 설정 파일 등록하기 
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/root-context.xml</param-value>
    </context-param>

<!-- Creates the Spring Container shared by all Servlets and Filters -->
    // 스프링 MVC 파일 정보 읽기
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

🔗 스프링 MVC 설정 파일 등록하기

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/root-context.xml</param-value>
    </context-param>

-   사용자가 직접 제어하는 XML 파일 목록을 지정함

  root-context.xml : 모든 서블릿과 필터에서 사용되는 루트 스프링 컨테이너(애플리케이션 컨텍스트, Ioc 컨테이너) 설정

 

🔗 스프링 MC 파일 정보 읽기

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

 루트 컨텍스트에 설정된 정보들을 모든 서블릿과 필터에 공유함

 

📌 서블릿 컨텍스트 

- 블릿 하나가 서블릿 컨테이너와 통신할 때 사용하는 메서드들을 가지고 있는 클래스

- 디스패처 서블릿이 모든 요청을 받고 요청 URL을 처리하는 컨트롤러에 매핑할 수 있도록 함

 

<!-- 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</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>

🔗 서블릿 이름, 클래스 등록

    <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-name> : 서블릿 이름 지정 

-  <servlet-class> : DispatcherServlet  클래스 설정

<init-param>  : = <context-parm>

- <load-on-startup> : 

 

 서블릿 매핑 경로 패턴 설정 

    <servlet-mapping>
        <servlet-name>appServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

- 웹 브라우저에서 요청되는 URL에 대한 디스패처 서블릿이 서블릿에 매핑함

- <url-pattern> 요소에 서블릿 매핑을 위해 URL 패턴을 설정함 

 

 

root-context.xml

-  모든 컨텍스트에서 공유되어 사용됨 

뷰(JSP 웹 페이지)와 관련 없는 빈 객체를 설정

 서비스, 저장소, 데이터베이스, 로그 등 웹 애플리케이션의 비즈니스 로직을 위한 컨텍스트를 설정한다. 

<?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">

    <!-- Root Context: defines shared resources visible to all other web components -->
</beans>

 

servlet-context.xml

서블릿 컨텍스트에서만 사용

 웹 요청을 직접 처리할 컨트롤러의 매핑을 설정(HandlerMapping) 하기 기능, 

-  뷰를 어떻게 처리할지 설정(ViewResolver) 기능 

 

🔗 컨트롤러 매핑하기 

요청 URL을 처리하는 컨트롤러에 매핑할 수 있도록 설정하기

-  이 설정으로 요청 URL과 같은 컨트롤러의 @RequestMapping 애너테이션에 지정된 URL을 매핑할 수 있게 됨 

 

<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />

<annotation-driven>

 @Controller, @RequestMapping 같은 애너테이션을 사용할 때 필요한 빈 객체들과 핸들러 매핑과 핸들러 어댑터의 빈 객체을 자동으로 등록

 

<annotation-driven> 요소를 사용하지 않으려면 핸들러 매핑과 핸들러 어댑터의 빈 객체를 내가 직접 등록해야 한다.

<!-- HandlerMapping -->
<beans:bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>

<!-- HandlerAdapter -->
<beans:bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>

🔗 정적 리소스 설정하기

-요청에 대해 JS, CSS 이미지 등 리소스 파일을 매핑하기를 설정하는 곳 

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

<resources> 

- 서버에서 앞서 처리될 필요가 없는 정적 리소스 파일을 처리하는 역할

 

ㄴpom.xml 파일 

- 메이븐 관련 환경 설정 파일

- 필요한 외부 라이브러리를 불러오는 곳

 

 

home.jsp 파일 실행 과정