본문 바로가기
풀스택/spring 다시 복습 처음부터

Spring 기초 설정(hellospring예제)

by woohyun22 2019. 6. 7.

예제는 hellospring1


Dynamic Web Project -> Maven Project로 convert 하기(예제ppt 참고)


다 만들어두고 하면 제일 좋지만 어떤 부분을 넣어서 어떤것이 되는지 아는게 제일 좋다.

수정이나 오류를 잡기가 편하기 때문에  - 당연한소리


먼저

src/WEB-INF/spring-servlet.xml

src/WEB-INF/web.xml

hellospring/pom.xml


web.xml과 pom.xml은 원래 있고, spring project를 만들때는 위에 spring-servlet를 만들어줘야됨.



- src/WEB-INF/spring-servlet.xml


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?xml version="1.0" encoding="UTF-8"?>
<beans
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:p="http://www.springframework.org/schema/p" 
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.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">
    <context:annotation-config />
    <!-- 베이스 패키지 지정  -->
    <context:component-scan base-package="com.douzone.hellospring.controller" />
</beans>
cs


베이스 패키지를 지정한다. 말 그대로 저패키지를 베이스로 한다. 



- hellospring/pom.xml


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
49
50
51
52
53
54
55
56
57
58
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.douzone</groupId>
    <artifactId>hellospring</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>  
       
    <properties>  
        <!-- 라이브러리 버전 프로퍼티로 관리 -->
        <org.springframework-version>4.2.1.RELEASE</org.springframework-version>
    </properties
    
    <dependencies
    
        <!-- Spring Core Library -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${org.springframework-version}</version>
        </dependency>
        
        <!-- Spring Web Library -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${org.springframework-version}</version>
        </dependency>
        
        <!-- Spring MVC Library -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${org.springframework-version}</version>
        </dependency>
        
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.2.1</version>
                <configuration>
                    <warSourceDirectory>webapp</warSourceDirectory>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
cs


주석잡은것처럼 의미하고, 오류가 났을때 없다고 하면 맞게 넣어주거나, 처음에 그냥 다 넣어주는게

오류 없이 제일 좋은거같다, 버전별 차이가 있는데 구글링을 하자


또한 제일 중요한점은 dependencies와 properties의 위치인데 이상하게 넣으면

후에 쓰일 @Controller등 어노테이션들을 찾아서 가져올수 없다.


그냥 만들어진 그대로 쓰는게 제일 좋은거같다.



src/WEB-INF/web.xml


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
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
    id="WebApp_ID" version="3.1">
    <display-name>hellospring</display-name>
    
    
    <!-- encoding filter(한글 처리,utf-8) -->
    <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>
 
 
    <!-- Front Controller(DispatchServlet에 대한 서블릿 매핑 추가)-->
    <servlet>
        <servlet-name>spring</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    </servlet>
 
    <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
 
</web-app>
cs


이하 동문, 한글처리 해주는 부분있다.

728x90

댓글