我正在进行一个简单的Spring项目,该项目允许餐馆使用Thymeleaf HTML表单将菜肴添加到数据库中。然后,用户可以根据一系列的标准搜索它们。
我还使餐馆能够在数据库中添加/更新它们的开放时间--使用OpeningHoursServiceImp中的以下方法
保存--允许最初添加时数
@PostMapping("/saveOpeningHour")
public String saveOpeningHour(@ModelAttribute("openinghour")OpeningHour openinghour) {
openingHourService.saveOpeningHourWithUserId(openinghour);
return "redirect:/dish";
}更新-它检索开放时间以允许使用上述保存方法编辑和重新保存
@GetMapping("/showOpeningForUpdate/{openingHourID}")
public String showOpeningForUpdate(@PathVariable(value = "openingHourID") long openingHourID, Model model) {
OpeningHour openingHour = openingHourService.getOpeningHourByOpeningHourID(openingHourID);
model.addAttribute("openinghour", openingHour);
return "opening_hours_update"
}当允许餐馆添加菜肴时,这些方法效果很好,因为一家餐馆可以添加许多菜式(一对多的关系)。
然而,开放时间和餐厅之间的关系是一对一的,因此,如果餐厅所有者增加了2组开放时间,应用程序就会崩溃。
在提交了一组开放时间后,是否有方法禁用“添加”按钮?还是在保存方法中正确处理错误?

我尝试在“添加开放时间表单”中禁用“保存”,使用
onsubmit="myButton.disabled = true; return true;` 但是它不起作用(我想是因为它是一个web应用程序,并且当页面被刷新时,它会变成非禁用的吗?)
我对春天很陌生,觉得很难。任何帮助都将不胜感激!
请参阅我的html查看/编辑/增加开放时间-
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<title>Dish Management System</title>
<link rel="stylesheet"
href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
</head>
<body>
<br>
<br>
<br>
<br>
<div class="col-sm-10 offset-sm-1 text-center">
<h1>Opening Hours</h1>
</div>
<nav class="navbar navbar-expand-md navbar-dark fixed-top bg-dark">
<a class="navbar-brand" href="#">WhatToEat</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarCollapse" aria-controls="navbarCollapse" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarCollapse">
<ul class="navbar-nav mr-auto">
<li class="nav-item active">
<a class="nav-link" href="/home">Home <span class="sr-only">(current)</span></a>
</li>
</ul>
<form class = "form-inline my-2 my-lg-0" action="#" th:action="@{/logout}" method="POST">
<button class= "btn btn-outline-danger my-2 my-sm btn-sm" type= "submit"> logout </button>
</form>
</div>
</nav>
<table border="1" class="table table-striped table-responsive-md">
<thead>
<tr>
<th>Opening Monday</th>
<th>Opening Tuesday</th>
<th>Opening Wednesday</th>
</tr>
</thead>
<tbody>
<tr th:each="openinghour : ${listopeninghours}">
<td th:text="${openinghour.openingHourMon}"></td>
<td th:text="${openinghour.openingHourTue}"></td>
<td th:text="${openinghour.openingHourWed}"></td>
<td><a th:href="@{/showOpeningForUpdate/{id}(id=${openinghour.openingHourID})}"
class="btn btn-primary">Update</a>
</tr>
</tbody>
</table>
<div class="col-sm-10 offset-sm-1 text-center">
<a th:href="@{/openinghour}"
class="btn btn-primary btn-sm mb-3" > Add Opening Hour </a>
</div>
</body>请参阅我的html表格以增加/节省开放时间-
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="ISO-8859-1">
<title>Opening Hours</title>
<link rel="stylesheet"
href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<nav class="navbar navbar-expand-md navbar-dark fixed-top bg-dark">
<a class="navbar-brand" href="#">WhatToEat</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarCollapse" aria-controls="navbarCollapse" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarCollapse">
<ul class="navbar-nav mr-auto">
<li class="nav-item active">
<a class="nav-link" href="/home">Home <span class="sr-only">(current)</span></a>
</li>
</ul>
<form class = "form-inline my-2 my-lg-0" action="#" th:action="@{/logout}" method="POST">
<button class= "btn btn-outline-danger my-2 my-sm btn-sm" type= "submit"> logout </button>
</form>
</div>
</nav>
<br>
<br>
<form action="#" th:action="@{/saveOpeningHour}" th:object="${openinghour}" method="POST">
<div class="col-sm-10 offset-sm-1 text-center">
<div class="container">
<br>
<h2>Business Hours</h2>
<hr>
<div class="row">
<div class="col">
Closing Hours
<hr>
<input type="text" th:field="*{openingHourMon}"
placeholder="Monday" class="form-control mb-4 col-10">
<input type="text" th:field="*{openingHourTue}"
placeholder="Tuesday" class="form-control mb-4 col-10">
<input type="text" th:field="*{openingHourWed}"
placeholder="Wednesday" class="form-control mb-4 col-10">
<input type="text" th:field="*{openingHourThurs}"
placeholder="Thursday" class="form-control mb-4 col-10">
<input type="text" th:field="*{openingHourFri}"
placeholder="Friday" class="form-control mb-4 col-10">
<input type="text" th:field="*{openingHourSat}"
placeholder="Saturday" class="form-control mb-4 col-10">
<input type="text" th:field="*{openingHourSun}"
placeholder="Sunday" class="form-control mb-4 col-10">
</div>
<div class="col">
Opening Hours
<hr>
<input type="text" th:field="*{closingHourMon}"
placeholder="Monday" class="form-control mb-4 col-10">
<input type="text" th:field="*{closingHourTue}"
placeholder="Tuesday" class="form-control mb-4 col-10">
<input type="text" th:field="*{closingHourWed}"
placeholder="Wednesday" class="form-control mb-4 col-10">
<input type="text" th:field="*{closingHourThurs}"
placeholder="Thursday" class="form-control mb-4 col-10">
<input type="text" th:field="*{closingHourFri}"
placeholder="Friday" class="form-control mb-4 col-10">
<input type="text" th:field="*{closingHourSat}"
placeholder="Saturday" class="form-control mb-4 col-10">
<input type="text" th:field="*{closingHourSun}"
placeholder="Sunday" class="form-control mb-4 col-10">
</div>
</div>
</div>
<button type="submit" name ="myButton" class="btn btn-info col-4" >Save</button>
<br>
<a th:href="@{/open}"> Back to Opening Hours</a>
</div>
</form>
</body>
</html>发布于 2021-12-20 19:48:05
在加载页面以管理开放时间时,可以检查列表listopeninghours是否有开放时间(这意味着listopeninghours不是null,列表listopeninghours的大小不是零),然后禁用添加打开时间按钮。
代码如下所示。
<button th:disabled="${listopeninghours != null && #lists.size(listopeninghours) != 0}" onclick="goToOpeningHours()">Add Opening Hour</button>
<script>
function goToOpeningHours(){
window.location.href = /*[[ @{/openinghour} ]]*/ '';
}
</script>th:disabled负责禁用按钮。
https://stackoverflow.com/questions/70425786
复制相似问题