헤르메스 LIFE

[Spring] 개발환경 구축 - Hello World 본문

Spring Framework

[Spring] 개발환경 구축 - Hello World

헤르메스의날개 2022. 12. 20. 02:07
728x90

SimpleSpring4.zip
0.02MB

Spring 개발환경을 갑자기 구축하려고 하다보니, 막막합니다.

하나 하나 구축해 보겠습니다.


개발환경

Spring 4.3.30.RELEASE

MAVEN 3.8.4


1. New > Maven Project

2. Use default Workspace Location -> 체크 풀고, 경로를 생성합니다.

3. 다시 체크 해줍니다.

4. Group Id, Artifact Id 셋팅

5. Maven 프로젝트가 생성된 모습입니다.


빨간색이 보입니다. 오류 겠죠.

고쳐보겠습니다.

1. SimpleSpring4 프로젝트 > Properties > Project Facets - Default

2. SimpleSpring4 프로젝트 > Properties > Project Facets - Runtimes

3. SimpleSpring4 프로젝트 > Properties > Java Build Path > Libraties > J2SE-1.5 > Remove

4. SimpleSpring4 프로젝트 > Properties > Java Build Path > Libraties > Add Library

4. Alternate JRE > 설치된 jdk 를 선택합니다.

5. jdk 1.8.0_202 버전이 선택되었습니다.

6. Maven > Update Project 선

오류가 사라졌습니다.


pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>Spring4MVC</groupId>
    <artifactId>Spring4MVC</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>

    <properties>
        <springframework.version>4.3.30.RELEASE</springframework.version>
        <ch.qos.logback-version>1.2.9</ch.qos.logback-version>
        <org.slf4j-version>1.7.36</org.slf4j-version>
    </properties>

    <dependencies>
        <!-- Spring -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${springframework.version}</version>
            <exclusions>
                <!-- Exclude Commons Logging in favor of SLF4j -->
                <exclusion>
                    <groupId>commons-logging</groupId>
                    <artifactId>commons-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${springframework.version}</version>
        </dependency>

        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.2</version>
        </dependency>

        <!-- slf4j -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${org.slf4j-version}</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>jul-to-slf4j</artifactId>
            <version>${org.slf4j-version}</version>
        </dependency>
        <!-- logback -->
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-core</artifactId>
            <version>${ch.qos.logback-version}</version>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>${ch.qos.logback-version}</version>
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-api</artifactId><!-- logback을 slf4j-api에서 사용함을 의미 -->
                </exclusion>
            </exclusions>
            <scope>runtime</scope><!-- runtime 시 사용함 -->
        </dependency>


        <!-- For user input validation -->
        <dependency>
            <groupId>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
            <version>1.1.0.Final</version>
        </dependency>

        <!-- File Upload -->
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3.1</version>
        </dependency>
        
        <!-- Thumbnail -->
        <dependency>
            <groupId>org.imgscalr</groupId>
            <artifactId>imgscalr-lib</artifactId>
            <version>4.2</version>
        </dependency>
        
        <!-- Servlet -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.2.3</version>
            </plugin>
        </plugins>
    </build>
</project>

web.xml

<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.oracle.com/webfolder/technetwork/jsc/xml/ns/javaee 
 http://www.oracle.com/webfolder/technetwork/jsc/xml/ns/javaee/web-app_3_1.xsd"
    version="3.1">

    <disply-name>SIMPLE-SPRING4</disply-name>

    <!-- Processes application requests -->
    <!-- HTTP 프로토콜로 들어오는 모든 요청을 가장 먼저 받아 적합한 컨트롤러에 위임해주는 프론트 컨트롤러(Front Controller) -->
    <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/config/DispatcherServlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

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

    <!-- Spring에서 지원하는 Request에 대한 파라메터 값을 인코딩하는 필터 -->
    <filter>
        <filter-name>encodingFilter</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>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!-- Session 시간 지정(Spring) / 분 -->
    <!-- HttpOnly는 자바스크립트의 document.cookie를 이용 해 쿠키에 접속하는 것을 막는 옵션이다. -->
    <session-config>
        <session-timeout>30</session-timeout>
        <cookie-config>
            <name>JSESSIONID</name>
            <path>/</path>
            <http-only>true</http-only>
        </cookie-config>
    </session-config>

    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

DispatcherServlet.xml

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc" 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-4.0.xsd
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">

    <mvc:annotation-driven />

    <!-- 빈 설정을 어노테이션 기반으로 하겠다는 설정태그 -->
    <context:component-scan base-package="simple">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
    </context:component-scan>

    <!-- CSS, JS, 이미지 등의 자원은 거의 변하지 않기 때문에, 웹 브라우저에 캐시를 하면
         네트워크 사용량, 서버 사용량, 웹 브라우저의 반응 속도 등을 개선할 수 있다. -->
    <mvc:resources mapping="/static/**" location="/static/" />
    
    <!--
     mvc:default-servlet-handler
     요청 경로와 일치하는 컨트롤러를 찾는다.
     컨트롤러가 존재하지 않으면, 디폴트 서블릿 핸들러에 전달한다.
     DispatcherSerlvet이 처리하지 못한 요청을 DefaultSerlvet에게 넘겨주는 역할을 하는 핸들러
     *.css와 같은 컨트롤러에 매핑되어 있지 않은 URL 요청은 최종적으로 Default Servlet에 전달되어 처리하는 역할
     
     
     web.xml 파일의 dispatcher 가 / 의 경우 설정한다.
     <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    -->
    <mvc:default-servlet-handler />

    <bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/view/" />
        <property name="suffix" value=".jsp" />
    </bean>

</beans>

728x90

'Spring Framework' 카테고리의 다른 글

[Spring] 개발환경 구축 - MessagesSource  (0) 2022.12.22
[Spring] 개발환경 구축 - Logback  (2) 2022.12.21
[Maven] 로컬 Dependency  (0) 2022.12.16
[log4j2] Log4j2 설정 참고  (0) 2022.12.11
[Redis] SpringBoot + Redis  (0) 2022.11.23