change Category and add category jsp files and add ExcelUtils(test)

This commit is contained in:
Kana
2024-01-04 16:23:39 +09:00
parent 629fb32e63
commit 06bac5a89b
7 changed files with 363 additions and 39 deletions

View File

@@ -5,38 +5,46 @@ import java.util.List;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model; import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.no1.wms.excel.ExcelUtils;
import jakarta.servlet.http.HttpServletResponse;
@Controller @Controller
public class CategoryController { public class CategoryController {
@Autowired @Autowired
CategoryService categoryService; CategoryService categoryService;
@Autowired
ExcelUtils excelUtils;
// 테스트 // 테스트
@GetMapping("/category/test") @GetMapping("/category/test")
public String testpage(Model m) { public String testPage(Model m) {
List<CategoryDto> dto = categoryService.selectAllCategory(); List<CategoryDto> dto = categoryService.selectAllCategory();
m.addAttribute("dto", dto); m.addAttribute("dto", dto);
return "category/test"; return "category/test";
} }
// 카테고리 리스트 출력 // 카테고리 리스트 출력
@GetMapping("/category/list") @GetMapping("/category/list")
public String list(@RequestParam(name="p", defaultValue = "1")int p, Model m) { public String list(@RequestParam(name = "p", defaultValue = "1") int p, Model m) {
// 서비스로 카테고리 목록 불러오는 메서드 작성 // 서비스로 카테고리 목록 불러오는 메서드 작성
List<CategoryDto> dto = categoryService.categoryList(p); List<CategoryDto> dto = categoryService.categoryList(p);
m.addAttribute("list", dto); m.addAttribute("list", dto);
return "category/list"; return "category/list";
} }
// 상세페이지 // 상세페이지
@PostMapping("/category/read/{kan_code}") @PostMapping("/category/read/{kan_code}")
public String read(@PathVariable String kan_code, Model m) { public String read(@PathVariable String kan_code, Model m) {
@@ -45,57 +53,78 @@ public class CategoryController {
m.addAttribute("dto", dto); m.addAttribute("dto", dto);
return "category/read"; return "category/read";
} }
// 생성 - 폼 // 생성 - 폼
@PostMapping("/category/create") @GetMapping("/category/create")
public String create() { public String create() {
return "category/create"; return "category/create";
} }
// 생성 - Ajax // 생성 - Ajax
@PostMapping("/category/create_process") @PostMapping("/category/create_process")
@ResponseBody
public int createProcess(CategoryDto dto, Model m) { public int createProcess(CategoryDto dto, Model m) {
int i = categoryService.createProcess(dto); int i = categoryService.createProcess(dto);
if(i == 1) { if (i == 1) {
return i; return i;
}else{ } else {
// ajax테스트후 결정 // ajax테스트후 결정
//m.addAttribute("dto", dto); // m.addAttribute("dto", dto);
return 0; return 0;
} }
} }
// 수정 - 폼 // 수정 - 폼
@PostMapping("/category/update/{kan_code}") @GetMapping("/category/update/{kan_code}")
public String update(@PathVariable String kan_code, Model m) { public String update(@PathVariable String kan_code, Model m) {
CategoryDto dto = categoryService.selectByKanCode(kan_code); CategoryDto dto = categoryService.selectByKanCode(kan_code);
m.addAttribute("dto", dto); m.addAttribute("dto", dto);
return "category/update"; return "category/update";
} }
// 수정 - Ajax // 수정 - Ajax
@PutMapping("/category//update_process") @PutMapping("/category//update_process")
@ResponseBody
public int updateProcess(CategoryDto dto, Model m) { public int updateProcess(CategoryDto dto, Model m) {
int i = categoryService.createProcess(dto); int i = categoryService.updateByKanCode(dto);
if(i == 1) { if (i == 1) {
return i; return i;
}else{ } else {
// ajax테스트후 결정 // ajax테스트후 결정
//m.addAttribute("dto", dto); // m.addAttribute("dto", dto);
return 0;
}
}
// 삭제
@DeleteMapping("/category/delete/{kan_code}")
@ResponseBody
public int delete(@PathVariable String kan_code) {
int i = categoryService.deactivateByKanCode(kan_code);
if (i == 1) {
return i;
} else {
// ajax테스트후 결정
// m.addAttribute("dto", dto);
return 0; return 0;
} }
} }
// 삭제 @GetMapping("/category/download")
public void downloadExcel(HttpServletResponse response) {
List<CategoryDto> dto = categoryService.selectAllCategory();
String excelFileName = "카테고리 테스트 파일";
String sheetName = "카테고리";
String[] columnName = {"KAN_CODE","대분류","중분류","소분류","세분류"};
excelUtils.downloadCategoryExcelFile(excelFileName, response, sheetName, columnName, dto);
}
// KAN코드 중복확인 메서드
public int chackKancode(String kan_code) {
return 0;
}

View File

@@ -11,6 +11,7 @@ public interface CategoryMapper {
CategoryDto selectByKanCode(String kan_code); CategoryDto selectByKanCode(String kan_code);
int createProcess(CategoryDto dto); int createProcess(CategoryDto dto);
List<CategoryDto> selectAllCategory(); List<CategoryDto> selectAllCategory();
int updateByKanCode(CategoryDto dto);
int deactivateByKanCode(String kan_code);
int activateByKanCode(String kan_code);
} }

View File

@@ -17,11 +17,11 @@ public class CategoryService {
// String start에 int값 넣고 String end에 int값 넣고 // String start에 int값 넣고 String end에 int값 넣고
// CategoryMapper에 있는 메서드 사용함. // CategoryMapper에 있는 메서드 사용함.
int start = 0; int start = 0;
int end = 0; int count = 10;
Map m = new HashMap<String, Object>(); Map m = new HashMap<String, Object>();
m.put("start", start); m.put("start", start);
m.put("end", end); m.put("count", count);
@@ -33,13 +33,26 @@ public class CategoryService {
} }
public int createProcess(CategoryDto dto) { public int createProcess(CategoryDto dto) {
mapper.createProcess(dto); return mapper.createProcess(dto);
return 1;
} }
public List<CategoryDto> selectAllCategory() { public List<CategoryDto> selectAllCategory() {
// 테스트용
return mapper.selectAllCategory(); return mapper.selectAllCategory();
} }
public int updateByKanCode(CategoryDto dto) {
return mapper.updateByKanCode(dto);
}
public int deactivateByKanCode(String kan_code) {
return mapper.deactivateByKanCode(kan_code);
}
public int activateByKanCode(String kan_code) {
return mapper.activateByKanCode(kan_code);
}
} }

View File

@@ -0,0 +1,78 @@
package com.no1.wms.excel;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Service;
import com.no1.wms.category.CategoryDto;
import jakarta.servlet.http.HttpServletResponse;
@Service
public class ExcelUtils {
public void downloadCategoryExcelFile(String excelFileName, HttpServletResponse response,
String sheetName, String[] columnName, List<CategoryDto> dto) {
String fileName = "";
try {
fileName = new String((excelFileName + ".xlsx").getBytes("utf-8"), "iso-8859-1");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
response.setContentType("ms-vnd/excel");
response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\";");
Workbook workbook = new HSSFWorkbook();
Sheet sheet = workbook.createSheet(sheetName);
Row row = null;
Cell cell = null;
int rowNum = 0;
row = sheet.createRow(rowNum);
for( int i = 0; i <= columnName.length -1; i++ ) {
cell = row.createCell(i);
cell.setCellValue(columnName[i]);
}
rowNum += 1;
//수정부분
makeCategoryBody(dto,row,sheet,cell,rowNum);
try {
workbook.write(response.getOutputStream());
workbook.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public void makeCategoryBody(List<CategoryDto> listdto, Row row, Sheet sheet,
Cell cell, int rowNum) {
//
for (int i = 0; i < listdto.size(); i++) {
row = sheet.createRow(rowNum++);
CategoryDto dto = listdto.get(i);
cell = row.createCell(0);
cell.setCellValue(dto.getKan_code());
cell = row.createCell(1);
cell.setCellValue(dto.getCls_nm_1());
cell = row.createCell(2);
cell.setCellValue(dto.getCls_nm_2());
cell = row.createCell(3);
cell.setCellValue(dto.getCls_nm_3());
cell = row.createCell(4);
cell.setCellValue(dto.getCls_nm_4());
}
}
}

View File

@@ -12,14 +12,26 @@
<!-- update --> <!-- update -->
<update id="updatebyCanCode" parameterType="CategoryDto"> <update id="updateByKanCode" parameterType="CategoryDto">
UPDATE product_category UPDATE product_category
SET cls_nm_1 = #{dto.cls_nm_1}, cls_nm_2 = #{dto.cls_nm_2},cls_nm_3 = #{dto.cls_nm_3}, cls_nm_4 = #{dto.cls_nm_4} SET cls_nm_1 = #{dto.cls_nm_1}, cls_nm_2 = #{dto.cls_nm_2},cls_nm_3 = #{dto.cls_nm_3}, cls_nm_4 = #{dto.cls_nm_4}
WHERE kan_code = {dto.kan_code} WHERE kan_code = {dto.kan_code}
</update> </update>
<!-- delete --> <update id="deactivateByKanCode" parameterType="String">
UPDATE product_category
SET activation = 0
WHERE kan_code = {kan_code}
</update>
<update id="activateByKanCode" parameterType="String">
UPDATE product_category
SET activation = 1
WHERE kan_code = {kan_code}
</update>
<!-- delete -->
<!-- select --> <!-- select -->
@@ -28,7 +40,7 @@
<select id="categoryList" parameterType="map" resultType="CategoryDto"> <select id="categoryList" parameterType="map" resultType="CategoryDto">
select * from product_category select * from product_category
order by kan_code desc limit #{start} , #{count} order by kan_code limit #{start} , #{count}
</select> </select>
<select id="selectByKanCode" parameterType="String" resultType="CategoryDto"> <select id="selectByKanCode" parameterType="String" resultType="CategoryDto">
@@ -40,4 +52,9 @@
</select> </select>
</mapper> </mapper>

View File

@@ -0,0 +1,68 @@
<%@ page contentType="text/html; charset=UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>카테고리 생성</title>
<style>
.ulTag {
padding: 0;
}
li {
display: inline-block;
width: 130px;
margin-right: 10px;
}
input {
display: inline-block;
}
#checkKan {
margin-left: 10px; /* .checkKan 버튼과 옆의 input 사이 간격 조절 */
}
</style>
</head>
<body>
<div class="header">
<h1>제품 카테고리 생성</h1>
</div>
<hr>
<div class="body">
<form id="createForm">
<div class="ulTag">
<ul>
<li>대분류</li>
<input type="text" name="cls_nm_1">
<br>
<li>중분류</li>
<input type="text" name="cls_nm_2">
<br>
<li>소분류</li>
<input type="text" name="cls_nm_3">
<br>
<li>세분류</li>
<input type="text" name="cls_nm_4">
<br>
<li>KAN 분류코드</li>
<input type="text" name="kan_code">
<button id="checkKan">중복확인</button>
<span id="chackDiv"></span>
</ul>
</div>
<div class="Btn">
<button id="commit">생성</button>
<button id="cancel">취소</button>
</div>
</form>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$("#checkKan").click(function() {
})
</script>
</div>
</body>
</html>

View File

@@ -0,0 +1,118 @@
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>제품 카테고리</title>
<style type="text/css">
.header {
text-align: center;
}
.body {
width: 80%;
margin: 20px auto;
}
.searchDropdown {
text-align: center;
display: inline-block;
margin-right: 10px;
}
select {
padding: 8px;
}
.search{
position: relative;
left: 70%;
}
.searchInput {
display: inline-block;
padding: 8px;
}
.searchButton {
display: inline-block;
}
table {
width: 100%;
border-collapse: collapse;
text-align: center;
}
</style>
</head>
<body>
<div class="header">
<h1>제품 카테고리 관리</h1>
</div>
<hr>
<div class="body">
<div class="search">
<form action="categorysearch">
<div class="searchDropdown" id="searchDropdown">
<select>
<option value="0"></option>
<option value="1"></option>
<option value="2"></option>
</select>
</div>
<div class="searchInput">
<input type="text" name="searchInput" id="searchInput"
placeholder="검색어를 입력하세요" />
</div>
<div class="searchButton">
<button id="searchButton">검색</button>
</div>
</div>
</form>
<div class="table">
<table>
<tr>
<th>KAN코드</th>
<th>대분류</th>
<th>중분류</th>
<th>소분류</th>
<th>세분류</th>
</tr>
<c:forEach items="${list }" var="dto">
<tr>
<td>${dto.kan_code }</td>
<td>${dto.cls_nm_1 }</td>
<td>${dto.cls_nm_2 }</td>
<td>${dto.cls_nm_3 }</td>
<td>${dto.cls_nm_4 }</td>
</tr>
</c:forEach>
</table>
</div>
<br>
<div class="uploadExcel">
<img alt="엑셀다운로드 이미지" src="엑셀다운로드 이미지">
<button id="uploadExcel" value="업로드">업로드</button>
</div>
<div class="paging">
<!-- 페이징 -->
</div>
<div class="createButton">
<button id="createButton" value="생성">생성</button>
</div>
</div>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$("#createButton").click(function(){
$(location).attr("href", "/category/create")
})
</script>
</body>
</html>