반응형
❤ 김춘장이의 위키백과 - 나만의 공부 기록 Tistory ❤
스프링프레임워크 파일 구조 정리
그제부터 게시판을 만들고 있었다
선생님의 소스코드를 똑같이 해도 되긴 했지만.
처음부터 다시 구축하고 싶었다.
초기세팅 파일의 기록을 남겨놓고자 한다 🙏
파일 구조
프로젝트 생성 후 pom.xml 파일설정
<properties>
<java-version>1.8</java-version>
<org.springframework-version>4.3.22.RELEASE</org.springframework-version>
<org.aspectj-version>1.8.9</org.aspectj-version>
<org.slf4j-version>1.6.6</org.slf4j-version>
</properties>
<dependencies>
<!-- Spring -->
<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>
<!-- 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>
<!-- 추가할 라이브러리를 설정합니다 -->
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc8</artifactId>
<version>12.2.0.1</version>
</dependency>
<!-- MyBatis 연동 라이브러리 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.8</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-dbcp/commons-dbcp -->
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
</dependencies>
<build>
<finalName>com.human.task</finalName>
<plugins>
<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>
web.xml파일 설정
필터 역할 : encoding 작업
<?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 https://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Processes application requests -->
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<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>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 문자 인코딩 시작 -->
<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>
<!-- 문자 인코딩 끝 -->
</web-app>
servlet-context.xml 파일 설정
디스펙처서블릿
리소스 매핑 및 뷰리졸브 빈 등록, 뷰 jsp 경로 지정, 컴포넌트 스캔 명시
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />
<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<context:component-scan base-package="com.human.task"/>
</beans:beans>
root-context.xml 파일 설정
DB관련 서비스단인 설정파일
mybatis의 데이터소스와 jdbc설정 세션팩토리 경로 설정 밑 세션 등록
컴포넌트 스캔 - dao, service
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<!-- Root Context: defines shared resources visible to all other web components -->
<!-- 오라클 been -->
<bean
class="org.springframework.jdbc.datasource.DriverManagerDataSource" id="dataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property>
<property name="url" value="jdbc:oracle:thin:@localhost:1521:xe"></property>
<property name="username" value="system"></property>
<property name="password" value="oracle"></property>
</bean>
<!-- factory bean 쿼리문 공장 -->
<bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFactory"> <!-- 쿼리문을 담당 -->
<property name="dataSource" ref="dataSource"> <!-- ref 의존관계 --> </property>
<property name="mapperLocations" value="mapper/*mapper.xml"></property>
</bean>
<!-- sqlsessrionTemlate spring과 연동하기 위한 객체 -->
<bean class="org.mybatis.spring.SqlSessionTemplate" id="sqlSession">
<constructor-arg index="0" ref="sqlSessionFactory"></constructor-arg> </bean>
<context:component-scan base-package="com.human.task.dao"/>
<context:component-scan base-package="com.human.task.service"/>
</beans>
Context-Path 설정
- 톰캣설정에서 진행
jsp파일에 데이터 넣기
<!-- 컨트롤러에서 보낸 리스트를 반복하여 tr단위로 출력하면 된다 -->
<c:forEach items="${list}" var="list">
<tr>
<th scope="row" style="text-align: center">${list.contactNum}</th>
<td style="text-align: center">${list.contactWriter}</td>
<td style="text-align: center">
<a href="/showPost?contactNum=${list.contactNum}">${list.contactTitle}</a>
</td>
<td style="text-align: center">${list.contactDate}</td>
<td style="text-align: center">
<form action="/selectPost" method="post">
<button type="submit" name="contactNum" value="${list.contactNum}"> 수정 </button>
</form>
</td>
<td style="text-align: center">
<form action="/deletePost" method="post">
<button type="submit" name="contactNum" value="${list.contactNum}"> 삭제 </button>
</form>
</td>
</tr>
</c:forEach>
상담게시판으로 진입 시 DB에 저장되어 있는 게시글들을 가져와야 했다.
그리고 가져온 게시글을 화면에 뿌려줘야 하는데 이것을 table형식으로 된 부분으로
적용하여 jsp파일에 대입되도록 해야 했다.
가장 중요한 부분은
이 부분이다
이 문법은 JSTL문법 사용한다는 의미이고 forEach문을 통해 반복문을 진행한다
<!-- jstl 문법을 사용한다 -->
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
상단에 해당 소스를 꼭 적어줘야 한다
내 소스에는 list.title부분에 list.contactNum이 같이 들어가져 있는데
글의 상세 보기를 눌렀을 때 게시글 번호를 알아야 하므로 같이 삽입되도록 진행했다
실행하면 이렇게 나온다
제목을 클릭하면 게시글의 상세로 이동이 가능하다
전체적인 파일은 추후 올리도록 하겠다
곧 마지막 프로젝트를 향해 달라겨가고있다!
슬슬 준비를 해야 할 것 같다
반응형
'🌱 𝐅𝐫𝐚𝐦𝐞𝐰𝐨𝐫𝐤 > ⠀⠀⠀⠀ Sᴘʀɪɴɢ' 카테고리의 다른 글
20240102🌱 마지막 프로젝트를 위해 (0) | 2024.01.03 |
---|---|
20231229🌱 눈이 오는 새해의 스프링 (0) | 2024.01.01 |
20231227🌱 즐겨봐요 봄날의 스프링 (0) | 2023.12.28 |
20231226🌱 다시 한번 시작해요 스프링 (0) | 2023.12.27 |
20231222🌱 Bean, Container, DI, Mapper, Mybatis (1) | 2023.12.27 |