[SPRING] SpringMVC 홈페이지만들기 1

🐷Jinie (juniorDeveloper)·2020년 12월 4일
1

JSP/MVC/SPRING

목록 보기
63/81
  • 2-layer + JDBCTemplate 를 모두 적용해서 홈페이지를 만드는 프로젝트를 시작한다.

1. 새로운 SpringFramework 프로젝트 기본설정

  • 환경설정 및 서버설정을 완료하고
    Test.jsp파일을 만들어서 서버구동에 문제가 없는지 확인한다.
  • Tomcat Module 변경

2. 프로젝트 전체구조 잡기


3. web.xml 설정하기

  • presentation-layer
  • business-layer

4. pom.xml 설정하기

  • pom.xml에 필요한 maven :
    Aspectj, SpringJDBC, commons-dbcp, ojdbc, java version 과 spring version 맞추기
<?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 https://maven.apache.org/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.springbook</groupId>
	<artifactId>biz</artifactId>
	<name>SpringMVC1204</name>
	<packaging>war</packaging>
	<version>1.0.0-BUILD-SNAPSHOT</version>
	<properties>
		<java-version>1.8</java-version>
		<org.springframework-version>4.2.4.RELEASE</org.springframework-version>
		<org.aspectj-version>1.6.10</org.aspectj-version>
		<org.slf4j-version>1.6.6</org.slf4j-version>
	</properties>
	<repositories>
		<repository>
			<id>public</id>
			<url>https://repo1.maven.org/maven2/</url>
		</repository>
	</repositories>
	<dependencies>
		<!-- https://mvnrepository.com/artifact/com.oracle.database.jdbc/ojdbc8 -->
		<dependency>
		    <groupId>com.oracle.database.jdbc</groupId>
		    <artifactId>ojdbc8</artifactId>
		    <version>12.2.0.1</version>
		</dependency>
		<!-- Spring -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-JDBC</artifactId>
			<version>${org.springframework-version}</version>
		</dependency>
			<dependency>
			<groupId>commons-dbcp</groupId>
			<artifactId>commons-dbcp</artifactId>
			<version>1.4</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<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>
		<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>
		<dependency>
			<groupId>org.aspectj</groupId>
			<artifactId>aspectjweaver</artifactId>
			<version>1.8.8</version>
		</dependency>	
		<dependency>
			<groupId>org.aspectj</groupId>
			<artifactId>aspectjtools</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>
	
		<!-- Test -->
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.7</version>
			<scope>test</scope>
		</dependency>        
	</dependencies>
    <build>
        <plugins>
            <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>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.5.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</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>

5. xml 파일 설정

  • jungbo-layer.xml 과 applicationContext.xml 파일에 스캐너 설정을 진행한다.
  1. junbo-layer.xml
  2. applicationContext.xml

6. SpringJDBC Template 연동하기

  • database.properties
jdbc.driver=oracle.jdbc.OracleDriver
jdbc.url=jdbc:oracle:thin:@//localhost:1521/xe
jdbc.username=system
jdbc.password=1234

  • applicationContext.xml
<!-- DataSource 설정하기 -->
<context:property-placeholder location="classpath:config/database.properties" />
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
		<property name="driverClassName" value="${jdbc.driver}" />
		<property name="url" value="${jdbc.url}"/>
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
	</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
      <property name="dataSource" ref="dataSource" />
</bean>

7. AOP로 트랜잭션 설정하기

<!--  트랜잭션 설정하기  -->
<bean  id="txManager"  class="org.springframework.jdbc.datasource.DataSourceTransactionManager" >
   <property name="dataSource"   ref="dataSource" />
</bean>

<tx:advice  id="txAdvice"  transaction-manager="txManager" >
	<tx:attributes>
		<tx:method name="get*"  read-only="true" />
		<tx:method name="*" />
	</tx:attributes>
</tx:advice>
<aop:config>
	<aop:pointcut id="txPointcut"  expression="execution(* com.springbook.biz..ServiceImpl.*(..) )"   />
	<aop:advisor  pointcut-ref="txPointcut"  advice-ref="txAdvice"/>
</aop:config>


8. 설정한 값이 잘 실행되는지 확인하기

  • test.jsp -> testController -> result 경로로 잘 도는지 확인!
  1. test.jsp 에서 test.do 링크를 만든다.
  2. jungboController에 test method만들기
  3. 최종 목적지인 result.jsp 만들기
  4. 실행결과 확인하기


  • ServiceImpl이 잘 실행되는지 확인! (비즈니스단이 잘 실행되는지 확인하기)
  1. ServiceImpl에서 생성자만들기
  2. ServiceImpl 생성이 잘 되는지 확인

    web.xml 의 listener가 자동으로 applicationContext의 스캐너를 올려실행하기때문에
    ServiceImpl의 생성자는 별도 처리없이 생성된다.
  3. ServiceImpl에 테스트 메서드를 넣어 문제없이 메서드까지 사용가능한지 확인한다.
  4. Controller에 어노테이션을 이용해 실행
  5. 실행결과확인
  6. DAO test 메서드를 이용해서 실행되는지 확인



참고자료!!

profile
ᴘᴇᴛɪᴛs ᴅᴇ́ᴠᴇʟᴏᴘᴘᴇᴜʀ. ᴘʀᴏɢʀᴀᴍᴍᴀᴛɪᴏɴ = ᴘʟᴀɪsɪʀ 💕

0개의 댓글