記錄

Spring+Mybatis+Tiles 1) pom.xml/web.xml/root-context.xml/servlet-context.xml/Tiles.xml 본문

Web/Spring framework

Spring+Mybatis+Tiles 1) pom.xml/web.xml/root-context.xml/servlet-context.xml/Tiles.xml

surhommejk 2018. 5. 5. 12:40

pom.xml

root-context.xml

servlet-context.xml

Tiles.xml

web.xml




pom.xml

<!-- 네임스페이스 -->
<?xml version="1.0" encoding="UTF-8"?>
<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/maven-v4_0_0.xsd">


<!-- 프로젝트 기본 정보(비 기술적 정보) -->
    <modelVersion>4.0.0</modelVersion>
    <groupId>kr.or</groupId> <!-- 구분자로 사용 -->
    <artifactId>bit</artifactId> <!-- 구분자 & 회사이름 -->
    <name>PracticeSpring</name><!-- 프로젝트 이름 -->
    <packaging>war</packaging>
    <version>1.0.0-BUILD-SNAPSHOT</version>


<!-- 프로젝트 기본 정보(기술적 정보) -->
    <properties>
        <java-version>1.6</java-version> <!-- 사용 자바 버전 -->
<!-- 사용 스프링 프레임워크 버전 정보 -->
        <org.springframework-version>3.1.1.RELEASE</org.springframework-version>
        
<!-- PARC에서 개발한 자바 프로그래밍 언어용 관점 지향 프로그래밍 (AOP) 확장 기능 -->
<org.aspectj-version>1.6.10</org.aspectj-version>
        
<!-- 로깅 관련 모듈 -->
<org.slf4j-version>1.6.6</org.slf4j-version>
    </properties>


<!-- orcle 저작권 문제해결 위해 oracle repository 설정 -->
<!-- 아래에서 dependency로 oracle jdbc 부르기 위한 선행 작업-->
    <repositories>
<repository>
<id>oracle</id>
<name>ORACLE JDBC Repository</name>
<url>http://maven.jahia.org/maven2</url>
</repository>
    </repositories>


<!-- 의존 주입 -->
    <dependencies>


        <!-- Spring -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>

<!-- 위 properties에서 만든 org.sp~-version을 call -->
            <version>${org.springframework-version}</version>
            <exclusions>
                <!-- Exclude Commons Logging in favor of SLF4j -->
                <exclusion>
                    <groupId>commons-logging</groupId>
                    <artifactId>commons-logging</artifactId>
                 </exclusion>
            </exclusions>
        </dependency>


<!-- spring web MVC -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${org.springframework-version}</version>
        </dependency>
                

        <!-- AspectJ -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>${org.aspectj-version}</version>
        </dependency>   
        

        <!-- Logging -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${org.slf4j-version}</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>jcl-over-slf4j</artifactId>
            <version>${org.slf4j-version}</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>${org.slf4j-version}</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.15</version>
            <exclusions>
                <exclusion>
                    <groupId>javax.mail</groupId>
                    <artifactId>mail</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>javax.jms</groupId>
                    <artifactId>jms</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>com.sun.jdmk</groupId>
                    <artifactId>jmxtools</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>com.sun.jmx</groupId>
                    <artifactId>jmxri</artifactId>
                </exclusion>
            </exclusions>
            <scope>runtime</scope>
        </dependency>


        <!-- @Inject -->
        <dependency>
            <groupId>javax.inject</groupId>
            <artifactId>javax.inject</artifactId>
            <version>1</version>
        </dependency>
                

        <!-- Servlet -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
    
<!-- 위 dependency들은 기본 생성 Spring Legacy project 기본 생성 dependency-->
<!------------------------------------------------------------------------------------>
<!-- 아래부터는 자주 쓰는 dependency를 추가 한 것 -->
        
<!-- Tiles -->
        <dependency>
        <groupId>org.apache.tiles</groupId>
        <artifactId>tiles-core</artifactId>
        <version>2.2.2</version>
        </dependency>
        <dependency>
        <groupId>org.apache.tiles</groupId>
        <artifactId>tiles-jsp</artifactId>
        <version>2.2.2</version>
        </dependency>
        
<!-- JSP UI JSTL LIB -->
        <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
        </dependency>
<!-- Spring Mybatis 사용 -->
        <dependency>
          <groupId>org.mybatis</groupId>
          <artifactId>mybatis</artifactId>
          <version>3.1.1</version>
      </dependency>
    <dependency>
          <groupId>org.mybatis</groupId>
          <artifactId>mybatis-spring</artifactId>
          <version>1.2.2</version>
      </dependency>
      
<!-- jdbc를 사용하기 위한 기본 dependency -->
<!-- mysql이든 oracle이든 jdbc 쓸거면 무조건 필요한 dependency -->
        <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>${org.springframework-version}</version>
        </dependency>

<!-- oracle jdbc를 위한 dependency -->
<!-- 위에서 repository 설정을 해뒀기 때문에 injection이 가능 -->
      <dependency>
        <groupId>com.oracle</groupId>
        <artifactId>ojdbc6</artifactId>
        <version>12.1.0.1</version>
        <scope>runtime</scope>
        </dependency>

        <!-- Test -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.7</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

<build>
<plugins>

<!-- Maven 사용을 위한 plug-in -->
<plugin>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.9</version>
<configuration>

<additionalProjectnatures>
<projectnature>org.springframework.ide.
eclipse.core.springnature</projectnature>
</additionalProjectnatures>
<additionalBuildcommands>
<buildcommand>org.springframework.ide.
eclipse.core.springbuilder</buildcommand>
</additionalBuildcommands>
<downloadSources>true</downloadSources>
<downloadJavadocs>true</downloadJavadocs>
</configuration>
</plugin>

<!-- Maven 사용을 위한 plug-in -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<compilerArgument>-Xlint:all</compilerArgument>
<showWarnings>true</showWarnings>
<showDeprecation>true</showDeprecation>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<configuration>
<mainClass>org.test.int1.Main</mainClass>
</configuration>
</plugin>
</plugins>

</build>
</project>









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

    <!-- 모든 서블릿과 필터에서 공유하는 Root Spring Container 설정 -->
<context-param>
<!-- param-name은 재량껏 쓸 수 있다 -->
        <param-name>contextConfigLocation</param-name>

<!-- 경로 설정은 정확해야 한다 -->
<!-- Root Spring Container 로 사용할 xml의 경로 설정 -->
        <param-value>/WEB-INF/spring/root-context.xml</param-value>
    </context-param>
    
    <!-- 모든 서블릿과 필터에서 공유하는 Spring Container 생성 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- application requests 실행을 위한 DispatcherServlet 등록-->
    <servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

<!-- DispatcherServlet로 사용할 .xml 경로 설정 -->
        <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>
        
<!-- 위에서 설정한 DispatcherServlet가 받을 요청 패턴 등록 -->
<!-- 위에서 이미 servlet 등록을 해뒀기 때문에 가능 -->
    <servlet-mapping>
        <servlet-name>appServlet</servlet-name>

<!-- 여기서 .htm이든 .do든 원하는 패턴을 등록 -->
        <url-pattern>.do</url-pattern>
    </servlet-mapping>

<!-- 여기까지가 Spring Legacy project 생성시 기본적으로 설정되는 사항들 -->
<!-------------------------------------------------------------------->
<!-- 아래부터는 원하는 추가적인 세팅 -->

<!-- 한글 처리를 위한 필터 -->
    <filter-mapping>
        <filter-name>EncodingFilter</filter-name>
<!-- 모든 것을 한글화 하기 위해서 /* 으로 패턴 설정 -->
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    

    <!-- 에러페이지 처리 -->
<error-page>
<error-code>404</error-code>
<location>/error/404error.jsp</location>
</error-page>
    <!-- 에러페이지 처리 -->
<error-page>
<error-code>500</error-code>
<location>/error/500error.jsp</location>
</error-page>

<!-- +a 에러페이지 sample은 jsp 카테고리에 업로드 되어 있음 -->
</web-app>






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">
    
    <!-- Root Context는 모든 웹 컴포넌트에서 공유될 수 있는 자원을 정의하는 공간이다 -->   
<!-- jdbc 자원을 bean으로 선언하고 접속 환경을 구성 -->
<bean id="driverManagerDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@localhost:1521:XE" />
<property name="username" value="bituser" />
<property name="password" value="1004" />
</bean>

<!-- Mybatis 사용을 위한 Bean 설정으로 pom.xml에서 dependency 설정을 이미 했기에 가능 -->
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="driverManagerDataSource"></property>
<!-- mapper 설정으로 경로를 반드시 정확하게 지정해주어야 한다 -->
<property name="mapperLocations" value="classpath*:kr/or/bit/dao/*xml" />
</bean>

<!-- Mybatis 사용을 위한 Bean 설정으로 pom.xml에서 dependency 설정을 이미 했기에 가능 -->
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactoryBean"/>
</bean>

</beans>






servlet-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">

<!-- @Autowired, @Required, @Resource, @Configuration 과 같은 어노테이션을 인식하기 위한 코드 -->
<!-- context:annotation-config 태그는 어노테이션과 관련해서 다음의 BeanPostProcessor를 함께 등록 한다.

        @Required(RequiedAnnotationBeanPostProcessor)
        @Autowired(AutowiredAnnotationBeanPostProcessor)
        @Resource, @PostConstruct, @PreDestory(CommonAnnotationBeanPostProcessor)
        @Configuration(ConfigurationClassPostProcessor)
        그 외 Repository, Service, Controller 포함 -->

<!-- https://blog.naver.com/skout90/220971378833 참고! -->
<context:annotation-config />

<!-- base-package 범위 내에서 어노테이션이 적용된 클래스를 bean으로 등록 -->
<context:component-scan base-package="kr.or.bit.controller" />

<!-- tile 기능 사용을 위한 코드 -->
<bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
     <property name="definitions">
        
         <list>
               <!-- Tiles.xml의 경로 설정 -->
             <value>/WEB-INF/spring/appServlet/Tiles.xml</value>
         </list>
     </property>
</bean>

<!-- view가 결국 Tiles를 거쳐서 나갈 것이므로 urlBasedViewResolver가 필요하다 -->
<bean id="urlBasedViewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
   <property name="viewClass" value="org.springframework.web.servlet.view.tiles2.TilesView" />
</bean>     
    
</beans>








Tiles.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE tiles-definitions PUBLIC
"-//Apache Software Foundation//DTD Tiles Configuration 2.1//EN"
"http://tiles.apache.org/dtds/tiles-config_2_1.dtd">

<tiles-definitions>
<!-- Dispatcher에 의해 던져지는 content 페이지를 찾아주는 mapping -->
<!-- definition의 name이 " *.* " 로 들어오면 {1}과 {2}가 활용되면서
mapping이 조금 더 복잡해지지만 일단 기본 원리는 똑같다 -->
<definition name="emp.*" template="/WEB-INF/views/inc/layout.jsp">
     <put-attribute name="header" value="/WEB-INF/views/inc/header.jsp" />
     <put-attribute name="footer" value="/WEB-INF/views/inc/footer.jsp" />
     <put-attribute name="content" value="/WEB-INF/views/{1}.jsp" />
</definition>
</tiles-definitions>








※ component-scan/annotation-config/annotation-driven 차이점에 유의


참고 : https://blog.naver.com/skout90/220971378833





Comments