728x90
★ 프로젝트 생성
New > Spring Legacy Project > Spring MVC Project > "Code" > "com.test.code" > Finish
- 기본 설정 + MyBatis
■ 데이터베이스 생성
create table tblCode (
seq number primary key, --번호(PK)
subject varchar2(500) not null, --제목(설명)
code varchar2(2000) not null, --코드조각
regdate date default sysdate not null,
language varchar2(50) not null --언어 종류
);
create sequence seqCode;
■ 파일 생성
"com.test.controller" > CodeController.java
"com.test.persistence" > CodeDAO.java(I)
> CodeDAOImpl.java(C)
"com.test.domain" > CodeDTO.java
views > "list.jsp"
> "view.jsp"
> "add.jsp"
- src/main/resources > mapper > code.xml
★ 기능 별 Coading
■ 추가 기능
------------------------------- CodeController.java
@PostMapping("/addok.do")
public String addok(CodeDTO dto) {
int result = dao.add(dto);
if(result == 1) {
return "redirect:/list.do";
} else {
return "redirect:/add.do";
}
}
------------------------------- CodeDAO.java
int add(CodeDTO dto);
------------------------------- CodeDAOImpl.java
@Override
public int add(CodeDTO dto) {
return template.insert("code.add", dto);
}
------------------------------- code.xml
<insert id="add">
insert into tblCode (seq, subject, code, regdate, language)
values (seqCode.nextVal, #{subject}, #{code}, default, #{language})
</insert>
■ 리스트 목록 띄우기 기능
------------------------------- CodeController.java
@GetMapping("/list.do")
public String list(Model model) {
List<CodeDTO> list = dao.list();
model.addAttribute("list", list);
return "list";
}
------------------------------- CodeDAO.java
List<CodeDTO> list();
------------------------------- CodeDAOImpl.java
@Override
public List<CodeDTO> list() {
return template.selectList("code.list");
}
------------------------------- code.xml
<select id="list" resultType="com.test.domain.CodeDTO">
select
seq, subject, regdate, language
from tblCode
order by seq desc
</select>
■ 상세 보기 기능
------------------------------- CodeController.java
@GetMapping("/view.do")
public String view(Model model, String seq) {
CodeDTO dto = dao.view(seq);
model.addAttribute("dto", dto);
return "view";
}
------------------------------- CodeDAO.java
CodeDTO view(String seq);
------------------------------- CodeDAOImpl.java
@Override
public CodeDTO view(String seq) {
return template.selectOne("code.view", seq);
}
------------------------------- code.xml
<select id="view" resultType="com.test.domain.CodeDTO">
select
*
from tblCode
where seq = #{seq}
</select>
■ JSP 파일
- list.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Code</title>
<link rel="stylesheet" href="https://me2.do/5BvBFJ57">
<style>
.main {
display: grid;
grid-template-columns: 1fr 1fr 1fr;
}
.item {
width: 235px;
border: 1px solid #AAA;
margin: 10px;
cursor: pointer;
}
.item > div:nth-child(1) {
padding: .5rem;
padding-left: 1rem;
}
.item > div:nth-child(2) {
min-height: 150px;
display: flex;
justify-content: center;
align-items: center;
border-top: 1px solid #AAA;
border-bottom: 1px solid #AAA;
}
.item > div:nth-child(3) {
padding: .5rem;
font-size: 14px;
text-align: right;
}
.filter {
text-align : right;
padding-right : 2rem;
}
.filter input {
background-color : #FFF;
}
</style>
</head>
<body>
<!-- -->
<h1>Code <small>목록보기</small></h1>
<div class="filter">
<input type="button" value="JAVA" data-language="JAVA" data-selected="0" data-color="tomato">
<input type="button" value="SQL" data-language="SQL" data-selected="0" data-color="gold">
<input type="button" value="HTML" data-language="HTML" data-selected="0" data-color="cornflowerblue">
</div>
<div class="main">
<c:forEach items="${list}" var="dto">
<div class="item" onclick="location.href='/view.do?seq=${dto.seq}';">
<c:choose>
<c:when test="${dto.language == 'JAVA'}">
<c:set var="color" value="tomato" />
</c:when>
<c:when test="${dto.language == 'SQL'}">
<c:set var="color" value="gold" />
</c:when>
<c:when test="${dto.language == 'HTML'}">
<c:set var="color" value="cornflowerblue" />
</c:when>
</c:choose>
<div style="background-color:${color}">${dto.language}</div>
<div>${dto.subject}</div>
<div>${dto.regdate}</div>
</div>
</c:forEach>
</div>
<div>
<button type="button" class="add" onclick="location.href='/add.do';">
추가하기
</button>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.4/jquery.min.js"></script>
<script>
//list.do?language=java&language=sql&language=html
let param = '';
$(".filter > input").click(function() {
if ($(this).attr("data-selected") == '0') {
$(this).attr("data-selected", '1');
$(this).css("background-color", $(this).data("color"));
} else {
$(this).attr("data-selected", '0');
$(this).css("background-color", '#FFF');
}
console.clear();
$('.filter > input').each((index, item) => {
if ($(item).attr("data-selected") == '1') {
console.log($(item).data('language'));
param += "language=" + $(item).data("language") + '&';
}
});
location.href = 'list.do?' + param;
});
<c:forEach items="${language}" var="lang">
$('.filter input[data-language=${lang}]').attr("data-selected", "1");
$('.filter input[data-language=${lang}]').css("background-color", $('.filter input[data-language=${lang}]').data("color"));
</c:forEach>
</script>
</body>
</html>
- add.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link rel="stylesheet" href="https://me2.do/5BvBFJ57">
<style>
</style>
</head>
<body>
<!-- -->
<h1>Code <small>추가하기</small></h1>
<form method="POST" action="/addok.do">
<table class="vertical">
<tr>
<th>제목</th>
<td><input type="text" name="subject" required class="full"></td>
</tr>
<tr>
<th>코드</th>
<td><textarea name="code" required class="full"></textarea></td>
</tr>
<tr>
<th>언어</th>
<td>
<select name="language">
<option value="java">JAVA</option>
<option value="sql">SQL</option>
<option value="html">HTML</option>
</select>
</td>
</tr>
</table>
<div>
<button type="submit" class="add">추가하기</button>
<button type="button" class="back" onclick="location.href='/list.do';">돌아가기</button>
</div>
</form>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.4/jquery.min.js"></script>
<script>
</script>
</body>
</html>
- view.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link rel="stylesheet" href="https://me2.do/5BvBFJ57">
<link rel="stylesheet" href="resources/highlight/styles/default.min.css">
<style>
#code {
margin: 1rem;
border: 1px solid var(--border-color);
}
#code * {
font-family: consolas;
}
</style>
</head>
<body>
<!-- -->
<!-- -->
<h1>Code <small>상세보기</small></h1>
<table>
<tr>
<th>번호</th>
<td>${dto.seq}</td>
<th>날짜</th>
<td>${dto.regdate}</td>
</tr>
<tr>
<td colspan="4">${dto.subject}</td>
</tr>
</table>
<pre id="code"><code class="language-${dto.language}"><c:out value="${dto.code}" /></code></pre>
<div>
<button type="button" class="back" onclick="location.href='/list.do';">돌아가기</button>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.4/jquery.min.js"></script>
<script src="/resources/highlight/highlight.min.js"></script>
<script>
hljs.highlightAll();
</script>
</body>
</html>
728x90
'Spring' 카테고리의 다른 글
Spring STEP 8 - 파일 업로드 (0) | 2023.06.19 |
---|---|
Spring STEP 7 - Tiles (2) | 2023.06.16 |
Spring STEP 5 - MyBatis 응용 (1) | 2023.06.15 |
Spring STEP 4 - Spring MVC (0) | 2023.06.14 |
Spring STEP 3 - DI & AOP (0) | 2023.06.14 |