본문 바로가기
개발 공부 Today I Learned

[국비 58일차 TIL] 스프링 다시 만들기 3

by 개발자신입 2024. 2. 15.
반응형

20240215 스프링 다시 만들기 로그

 

프로젝트 설정

해당 프로젝트 우클릭해서 설정 - Project Facets에서 Dynamic Web Module을 4.0으로, java를 11로 변경

web.xml

아래의 코드 추가

<welcome-file-list>
      <welcome-file>/WEB-INF/views/index.jsp</welcome-file>
   </welcome-file-list>
	<error-page>
		<exception-type>java.lang.Throwable</exception-type>
		<location>/WEB-INF/views/error/error.jsp</location>
	</error-page>
	<error-page>
		<error-code>404</error-code>
		<location>/WEB-INF/views/error/404.jsp</location>
	</error-page>
	<error-page>
		<error-code>500</error-code>
		<location>/WEB-INF/views/error/500.jsp</location>
	</error-page>
	<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>

pom.xml

<!-- log찍기 -->
      <dependency>
         <groupId>org.lazyluke</groupId>
         <artifactId>log4jdbc-remix</artifactId>
         <version>0.2.7</version>
      </dependency>

      <!-- db+ -->
      <dependency>
         <groupId>commons-dbcp</groupId>
         <artifactId>commons-dbcp</artifactId>
         <version>1.4</version>
      </dependency>

 

 

1. lombok.jar 파일을 sts실행 파일이 있는 폴더에 넣고 실행시켜주기.

2. lombok 코드를 pom.xml에 추가하기

 


db-context.xml

<property name="mapperLocations" value="classpath:/mybatis/mappers/*-mapper.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">

	<!-- DB 접속 정보 : 첫 줄만 변경하기 -->
<bean id="dataSourceSpied" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"
		<property name="username" value="접속 아이디"/>
		<property name="password" value="접속 비밀번호"/>
		<property name="url" value="jdbc:mariadb:// 접속 주소 "/>
		<property name="driverClassName" value="org.mariadb.jdbc.Driver"/>
	</bean>

	
	<bean id="dataSource" class="net.sf.log4jdbc.Log4jdbcProxyDataSource">
      <constructor-arg ref="dataSourceSpied" />
      <property name="logFormatter">
         <bean class="net.sf.log4jdbc.tools.Log4JdbcCustomFormatter">
         <property name="loggingType" value="MULTI_LINE" />
         <property name="sqlPrefix" value="SQL : " />
      </bean>
      </property>
   </bean>
	
	<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource"/> <!-- 객체 = ref -->
		<property name="mapperLocations" value="classpath:/mybatis/mappers/*-mapper.xml"/>
		<property name="configLocation" value="classpath:/mybatis/config/mybatis-config.xml"/>
	</bean>
	
	<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
		<constructor-arg name="sqlSessionFactory" ref="sqlSessionFactoryBean"/>
	</bean>
</beans>

log4j2.xml

src/main/resources에 파일 통째로 추가

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration PUBLIC "-//APACHE//DTD LOG4J 1.2//EN" "http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/xml/doc-files/log4j.dtd">
<Configuration>
	<Appenders>
		<Console name="console" target="SYSTEM_OUT">
			<PatternLayout pattern="%d %5p [%c] %m%n" />
		</Console>
		<!--추가된 부분 -->
		<Console name="console-infolog"
			class="org.apache.log4j.ConsoleAppender">
			<layout class="org.apache.log4j.PatternLayout">
				<param name="ConversionPattern" value="%d %5p %m%n" />
			</layout>
		</Console>
	</Appenders>
	<Loggers>
		<Logger name="java.sql" level="info" additivity="false">
			<AppenderRef ref="console" />
		</Logger>
		<Logger name="egovframework" level="DEBUG" additivity="false">
			<AppenderRef ref="console" />
		</Logger>
		<!-- log SQL with timing information, post execution -->
		<Logger name="jdbc.sqltiming" level="info" additivity="false">
			<AppenderRef ref="console" />
		</Logger>
		<Logger name="org.springframework" level="DEBUG"
			additivity="false">
			<AppenderRef ref="console" />
		</Logger>
		<!-- Query Loggers -->
		<Logger name="jdbc.sqlonly" additivity="false">
			<level value="DEBUG" />
			<appender-ref ref="console-infolog" />
		</Logger>
		<logger name="java.sql.ResultSet" additivity="false">
			<level value="OFF" />
		</logger>
		<Root level="DEBUG">
			<AppenderRef ref="console" />
		</Root>
	</Loggers>
</Configuration>

servlet-context.xml

	<!-- Handles HTTP GET requests for /resources/** by efficiently serving 
		up static resources in the ${webappRoot}/resources directory -->
	<resources mapping="/resources/**" location="/resources/" />
	<resources mapping="/assets/**" location="/resources/assets/" />
	<resources mapping="/css/**" location="/resources/css/" />
	<resources mapping="/js/**" location="/resources/js/" />
    
    
    	<!-- 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="org.코드네임.*" />
	
	<beans:import resource="classpath:/spring/*-context.xml"/>

index.jsp 연결하기 

favicon.ico 설정하기


menu.jsp

메뉴 (Navigation)을 menu.jsp 로 이동시키기.

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<!-- Navigation-->
	<nav class="navbar navbar-expand-lg navbar-dark fixed-top bg-dark" id="mainNav">
		<div class="container">
			<a class="navbar-brand" href="./index"><img
				src="assets/android-icon-192x192.png" alt="..." /></a>
			<button class="navbar-toggler" type="button"
				data-bs-toggle="collapse" data-bs-target="#navbarResponsive"
				aria-controls="navbarResponsive" aria-expanded="false"
				aria-label="Toggle navigation">
				Menu <i class="fas fa-bars ms-1"></i>
			</button>
			<div class="collapse navbar-collapse" id="navbarResponsive">
				<ul class="navbar-nav text-uppercase ms-auto py-4 py-lg-0">
					<li class="nav-item"><a class="nav-link" href="./board">게시판</a></li>
					<li class="nav-item"><a class="nav-link" href="./notice">공지사항</a></li>
					<li class="nav-item"><a class="nav-link" href="./login">로그인</a></li>
				</ul>
			</div>
		</div>
	</nav>

index.jsp에 nav가 있던 부분에 import하기

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

	<!-- Navigation 코드 이동 : menu.jsp -->
	<c:import url="menu.jsp"/>

board.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
<meta name="description" content="" />
<meta name="author" content="" />
<title>게시판</title>
<!-- Favicon-->
<link rel="shortcut icon" href="/path/to/favicon.ico">
<link rel="apple-touch-icon" sizes="57x57" href="assets/apple-icon-57x57.png">
<link rel="apple-touch-icon" sizes="60x60" href="assets/apple-icon-60x60.png">
<link rel="apple-touch-icon" sizes="72x72" href="assets/apple-icon-72x72.png">
<link rel="apple-touch-icon" sizes="76x76" href="assets/apple-icon-76x76.png">
<link rel="apple-touch-icon" sizes="114x114" href="assets/apple-icon-114x114.png">
<link rel="apple-touch-icon" sizes="120x120" href="assets/apple-icon-120x120.png">
<link rel="apple-touch-icon" sizes="144x144" href="assets/apple-icon-144x144.png">
<link rel="apple-touch-icon" sizes="152x152" href="assets/apple-icon-152x152.png">
<link rel="apple-touch-icon" sizes="180x180" href="assets/apple-icon-180x180.png">
<link rel="icon" type="image/png" sizes="192x192" href="assets/android-icon-192x192.png">
<link rel="icon" type="image/png" sizes="32x32"	href="assets/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="96x96"	href="assets/favicon-96x96.png">
<link rel="icon" type="image/png" sizes="16x16"	href="assets/favicon-16x16.png">
<link rel="manifest" href="/manifest.json">
<meta name="msapplication-TileColor" content="#ffffff">
<meta name="msapplication-TileImage" content="/ms-icon-144x144.png">
<meta name="theme-color" content="#ffffff">
<!-- Font Awesome icons (free version)-->
<script src="https://use.fontawesome.com/releases/v6.3.0/js/all.js"	crossorigin="anonymous"></script>
<!-- Google fonts-->
<link href="https://fonts.googleapis.com/css?family=Montserrat:400,700"	rel="stylesheet" type="text/css" />
<link href="https://fonts.googleapis.com/css?family=Roboto+Slab:400,100,300,700" rel="stylesheet" type="text/css" />
<!-- Core theme CSS (includes Bootstrap)-->
<link href="css/styles.css" rel="stylesheet" />
<link href="css/board.css" rel="stylesheet" />
</head>
<body id="page-top">
	<!-- Navigation 코드 이동 : menu.jsp -->
	<c:import url="menu.jsp" />

	<!-- 게시판 -->
	<br><br>
	<section class="page-section" id="services">
		<div class="container">
			<div class="text-center">
				<h2 class="section-heading text-uppercase">게시판</h2>
			</div>
			<div class="row text-center">
				<table class="table table-hover">
					<thead>
						<tr>
							<th>번호</th>
							<th>제목</th>
							<th>작성자</th>
							<th>작성일</th>
							<th>조회수</th>
						</tr>
					</thead>
					<tbody>
						<c:forEach items="${list }" var="row">
							<tr>
								<td>${row.board_no }</td>
								<td class="title">
								<a href="./detail?no=${row.board_no }">${row.board_title }[${row.comment }] </a>
								</td>
								<td>${row.board_write }</td>
								<td>${row.board_date }</td>
								<td>${row.board_count }</td>
							</tr>
						</c:forEach>
					</tbody>
				</table>
			</div>
		</div>
	</section>
	<!-- Bootstrap core JS-->
	<script	src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js"></script>
	<!-- Core theme JS-->
	<script src="js/scripts.js"></script>
	<script src="https://cdn.startbootstrap.com/sb-forms-latest.js"></script>
</body>
</html>

BoardController.java

		@GetMapping("/detail")
		public String detail(HttpServletRequest request) {
			// no 잡기
			String no = request.getParameter("no");
			System.out.println(no);
			
			return "detail";
		}

 


Util.java

* 위치 : org.-----.util - Util.java

@ controller, service, repository 외의 기능을 하는 것들은 component로 사용함.

 

@Component
public class Util {
	public int str2Int(String str) {
		try {
			return Integer.parseInt(str);
		} catch (Exception e) {
			return 0;
		}
	}
}

 

반응형

댓글