2020-12-17 TIL

1 minute read

gradle 실행

  • 웹 프로젝트 폴더가 생성된다.
  • 웹 리소스 파일을 저장할 폴더를 준비한다.

welcome-file-list

  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>default.htm</welcome-file>
  </welcome-file-list>
  • 주소창에 주소를 http://localhost:9999/bitcamp-spring-webmvc/ 라고 치고 엔터를 쳤을 때 welcome-file-list에 있는 순서대로 webapp을 뒤져서 화면을 띄운다.

라이브러리 추가

sevlet 프로그래밍 적용

plugins {
    id 'java'
    id 'eclipse-wtp'
    id 'war'
    // war 가 없으면 배포가 안되고 배치도 안된다.
}

repositories {
    jcenter()
}

dependencies {
    // compileOnly?
    // - 프로그래밍 하는 동안에만 사용하고 배치할 때는 제외하는 라이브러리를 가리킨다.
    // - 프로그램이 배치되는 런타입 서버(예: 실행 중인 톰캣 서버)에서 
    //   라이브러리를 제공하는 경우 이 옵션으로 프로젝트에 추가한다.
    // => Servlet API 라이브러리
    //compileOnly group: 'javax.servlet', name: 'javax.servlet-api', version: '4.0.1'
    
    // providedCompile?
    // - compileOnly 처럼 컴파일 할 때만 사용한다.
    // - 배포 파일에는 포함하지 않는다.
    // - 단 이 옵션은 'war' 플러그인 사용시에만 사용할 수 있다.
    providedCompile group: 'javax.servlet', name: 'javax.servlet-api', version: '4.0.1'

    // implementation?
    // - 컴파일 할 때 사용한다.
    // - 배포 파일에도 포함된다.
    // => JSTL 명세를 구현한 라이브러리
    implementation group: 'javax.servlet', name: 'jstl', version: '1.2'

    implementation 'com.google.guava:guava:28.2-jre'

    // testImplementation?
    // - 단위 테스트를 수행할 때만 사용한다. 배치에 포함되지 않는다.
    testImplementation 'junit:junit:4.12'
}

Spring webmvc 라이브러리 추가

plugins {
    id 'java'
    id 'eclipse-wtp'
    id 'war'
}

repositories {
    jcenter()
}

dependencies {

    providedCompile group: 'javax.servlet', name: 'javax.servlet-api', version: '4.0.1'

    implementation group: 'javax.servlet', name: 'jstl', version: '1.2'

    // log4j 2.x 라이브러리
    implementation 'org.apache.logging.log4j:log4j-core:2.14.0'

    // Spring WebMVC 프레임워크 라이브러리
    implementation 'org.springframework:spring-webmvc:5.3.2'

    implementation 'com.google.guava:guava:28.2-jre'
    testImplementation 'junit:junit:4.12'
}
  • 로그4j 는 콘솔창에 로그를 다 출력해준다.

app-servlet.xml 의 위치

  • 일반 웹 디렉토리는 클라이언트에서 접근할 수 있기 때문에 설정 정보가 노출될 위험이 있다.
  • 절대로 일반 웹 디렉토리에 두지 말자.

mvc:annotation-driven/

  • https://velog.io/@hanblueblue/Spring-mvcannotation-driven
  • https://okky.kr/article/289577?note=970772
  • mvc 관련 annotation을 처리한다.

contextLoaderListener와 dispatcherServlet의 관계

Spring WebMVC의 프론트 컨트롤러 역할을 수행할 서블릿 지정

  • Java Config로 IoC 컨테이너를 지정할 때는 기본 IoC 컨테이너를 교체해야 한다.
    • 다음과 같이 초기화 파라미터를 통해 DispatcherServlet이 사용할 IoC 컨테이너 클래스와 Java Config 클래스를 설정한다.
  • 별도로 다운로드 받은 라이브러리를 사용하고 싶을 때 build.gradle에서 라이브러리 주소를 준다.

선언적 방식

  • xml이나 애노테이션을 적어놓으면 서블릿 컨테이너가 적혀있는대로 객체를 생성해서 등록한다.
<web-app>

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

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

    <servlet>
        <servlet-name>app</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value></param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>app</servlet-name>
        <url-pattern>/app/*</url-pattern>
    </servlet-mapping>

</web-app>

프로그래머틱 방식ß

  • 개발자가 직접 서블릿을 만들어서 등록하는 것
public class MyWebApplicationInitializer implements WebApplicationInitializer {

    @Override
    public void onStartup(ServletContext servletContext) {

        // Load Spring web application configuration
        AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
        context.register(AppConfig.class);

        // Create and register the DispatcherServlet
        DispatcherServlet servlet = new DispatcherServlet(context);
        ServletRegistration.Dynamic registration = servletContext.addServlet("app", servlet);
        registration.setLoadOnStartup(1);
        registration.addMapping("/app/*");
    }
}

Categories:

Updated: