Stept 2 Done em Spring

This commit is contained in:
2026-10-13 15:20:46 +09:00
parent 80102c7bf9
commit cca4aa5f79
6 changed files with 246 additions and 75 deletions

View File

@@ -0,0 +1,7 @@
package com.example.rhizome.app.domain.service;
import com.example.rhizome.app.domain.model.Department;
public interface InsertDepartment {
void insert(Department department);
}

View File

@@ -0,0 +1,29 @@
package com.example.rhizome.app.domain.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.example.rhizome.app.domain.model.Department;
import com.example.rhizome.app.domain.service.InsertDepartment;
import com.example.rhizome.app.infra.entity.DepartmentEntity;
import com.example.rhizome.app.infra.repository.DepartmentRepository;
@Service
@Transactional
public class InsertDepartmentImpl implements InsertDepartment {
DepartmentRepository departmentRepository;
@Autowired
public InsertDepartmentImpl(DepartmentRepository departmentRepository) {
this.departmentRepository = departmentRepository;
}
@Override
public void insert(Department department) {
DepartmentEntity entity = new DepartmentEntity();
entity.setNmDepartment(department.getNmDepartment());
departmentRepository.save(entity);
}
}

View File

@@ -0,0 +1,78 @@
package com.example.rhizome.app.presentation.controller;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.example.rhizome.aop.aspect.SessionCheck;
import com.example.rhizome.app.domain.model.Department;
import com.example.rhizome.app.domain.service.InsertDepartment;
import com.example.rhizome.app.presentation.controller.pageproperty.PageReturnAttributeKeyword;
import com.example.rhizome.app.presentation.controller.pageproperty.TransitionTargetPageNameKeyword;
import com.example.rhizome.app.presentation.form.DepartmentForm;
import com.example.rhizome.app.presentation.form.MessageForm;
@Controller
public class InsertDepartmentController {
private final static Logger LOGGER = Logger.getLogger(InsertDepartmentController.class.getName());
private final static String SEVERE_ERROR_MESSAGE = "エラーが発生しました!";
HttpSession httpSession;
InsertDepartment insertDepartment;
@Autowired
public InsertDepartmentController(HttpSession httpSession, InsertDepartment insertDepartment) {
this.httpSession = httpSession;
this.insertDepartment = insertDepartment;
}
/**
* 部署登録入力画面を表示する
*/
@SessionCheck
@RequestMapping(value = "/insertDepartmentInput", method = {RequestMethod.GET, RequestMethod.POST})
public String showInsertDepartment(Model model) {
try {
model.addAttribute("departmentForm", new DepartmentForm());
return "insertDepartmentInput";
} catch (Exception e) {
httpSession.invalidate();
e.printStackTrace();
LOGGER.log(Level.SEVERE, SEVERE_ERROR_MESSAGE, e);
return TransitionTargetPageNameKeyword.ERROR_HTML;
}
}
/**
* 部署情報の登録を実行する
*/
@SessionCheck
@PostMapping("/insertDepartmentResult")
public String insertDepartmentResult(@ModelAttribute("departmentForm") DepartmentForm departmentForm, Model model) {
final String INSERT_COMPLETE = "登録しました。";
try {
Department department = new Department(null, departmentForm.getNmDepartment());
insertDepartment.insert(department);
model.addAttribute(PageReturnAttributeKeyword.MESSAGE_INFO, new MessageForm(INSERT_COMPLETE));
return "forward:/selectDepartment";
} catch (Exception e) {
httpSession.invalidate();
e.printStackTrace();
LOGGER.log(Level.SEVERE, SEVERE_ERROR_MESSAGE, e);
return TransitionTargetPageNameKeyword.ERROR_HTML;
}
}
}

View File

@@ -1,10 +1,76 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
<!-- 外部のスクリプトファイルを読み込む -->
<script th:src="@{/js/common.js}"></script>
<!-- CSSの読み込み -->
<link rel="stylesheet" th:href="@{/css/all.css}">
<link rel=stylesheet th:href="@{/css/style.css}" type="text/css">
<title>部署情報登録</title>
</head>
<body>
<h1>
部署情報登録
<div class="rightSection">
<span class="loginInfo">ログイン者名:<span th:text="${session.loginUser.nmEmployee}"></span></span>
<button class="logoutButton" onclick="logoutSubmit()">ログアウト</button>
</div>
</h1>
<form action="/insertDepartmentResult" method="post" onsubmit="return confirm('本当によろしいですか?');"
th:object="${departmentForm}">
<div class="msgBox error" th:if="${messageError != null}">
<span class="error" th:each="message : ${messageError.message}" th:text="${message}"></span>
</div>
<table>
<tr>
<th class="header">部署ID</th>
<td>
<label>
<input class="readOnly" readonly="readonly" type="text"value="自動で設定されます" />
</label>
</td>
</tr>
<tr>
<th class="header">部署名</th>
<td>
<label>
<input maxlength="50" placeholder="部署名" required="required" th:field="*{nmDepartment}"
type="text" />
</label>
</td>
</tr>
<tr>
<td class="btnArea" colspan="2">
<input type="submit" value="登録" />
</td>
</tr>
</table>
</form>
<a href="javascript:selectSubmit()">一覧に戻る</a>
</body>
<script>
function selectSubmit() {
const form = document.createElement('form');
form.setAttribute('method', 'post');
form.setAttribute('action', 'selectDepartment');
// 動的送信用のHidden
const inputField = document.createElement("input");
inputField.setAttribute("type", "hidden");
inputField.setAttribute("value", "selectDepartment");
form.appendChild(inputField);
// フォームをDOMに追加
document.body.appendChild(form);
form.submit();
}
</script>
</html>

View File

@@ -97,6 +97,7 @@
form.submit();
}
// 部署削除用のJavaScript
function deleteDepartment(idDepartment) {
const form = document.createElement('form');

View File

@@ -12,18 +12,15 @@
</head>
<body>
<h1>
<h1>
部署情報更新
<div class="rightSection">
<span class="loginInfo">ログイン者名:<span th:text="${session.loginUser.nmEmployee}"></span></span>
<button class="logoutButton" onclick="logoutSubmit()">ログアウト</button>
</div>
</h1>
<form action="/updateDepartmentResult"
method="post"
onsubmit="return confirm('本当によろしいですか?');"
th:object="${departmentForm}"
>
</h1>
<form action="/updateDepartmentResult" method="post" onsubmit="return confirm('本当によろしいですか?');"
th:object="${departmentForm}">
<div class="msgBox error" th:if="${messageError != null}">
<span class="error" th:each="message : ${messageError.message}" th:text="${message}"></span>
</div>
@@ -32,11 +29,7 @@
<th class="header">部署ID</th>
<td>
<label>
<input class="readOnly"
readonly="readonly"
th:field="*{idDepartment}"
type="text"
/>
<input class="readOnly" readonly="readonly" th:field="*{idDepartment}" type="text" />
</label>
</td>
</tr>
@@ -44,23 +37,19 @@
<th class="header">部署名</th>
<td>
<label>
<input maxlength="50"
placeholder="部署名"
required="required"
th:field="*{nmDepartment}"
type="text"
/>
<input maxlength="50" placeholder="部署名" required="required" th:field="*{nmDepartment}"
type="text" />
</label>
</td>
</tr>
<tr>
<td class="btnArea" colspan="2">
<input type="submit" value="更新"/>
<input type="submit" value="更新" />
</td>
</tr>
</table>
</form>
<a href="javascript:selectSubmit()">一覧に戻る</a>
</form>
<a href="javascript:selectSubmit()">一覧に戻る</a>
</body>
<script>
@@ -82,4 +71,5 @@
form.submit();
}
</script>
</html>