@PostMapping("/sketches/add")
public String addSketch(
@Valid Sketch sketch,
BindingResult result,
ModelMap model,
RedirectAttributes redirectAttributes) {
redirectAttributes.addFlashAttribute("message", "Failed");
redirectAttributes.addFlashAttribute("alertClass", "alert-danger");
if (result.hasErrors()) {
return "admin/add-sketch";
}
sketchRepository.save(sketch);
redirectAttributes.addFlashAttribute("message", "Success!");
redirectAttributes.addFlashAttribute("alertClass", "alert-success");
return "redirect:/admin/sketches/add";
}
<form action="#" th:action="@{/admin/sketches/add}" th:object="${sketch}" method="post" id="save-sketch">
<div class="add-sketch">
<div class="title">
<div class="title-text">
<p>Title
<span th:if="${#fields.hasErrors('title')}" th:errors="*{title}" class="error"></span>
</p>
<input type="text" class="title-input" th:field="*{title}">
</div>
<div class="title-file">
<label for="file-input">
<img th:src="@{/images/add image.png}" alt="upload image">
</label>
<input id="file-input" type="file"/>
</div>
<span id="image-text">No file chosen, yet</span>
</div>
<!--SKETCH TEXT====================================-->
<div class="sketch-text">
<p>Sketch Text
<span th:if="${#fields.hasErrors('text')}" th:errors="*{text}" class="error"></span>
</p>
<textarea name="sketch-area" id="" cols="55" rows="6" th:field="*{text}"></textarea>
</div>
<!--DROPDOWN-->
<div class="select">
<select name="select-category" id="select-category" th:field="*{category}">
<option value="Buttons">Buttons</option>
<option value="Potentiometers">Potentiometers</option>
<option value="Encoders">Encoders</option>
<option value="Leds">Leds</option>
</select>
</div>
<span th:if="${#fields.hasErrors('category')}" th:errors="*{category}" class="error"></span>
<!--ADD GIF=====================================-->
<input type="file" id="gif-file" hidden="hidden">
<button type="button" id="gif-button">Add GIF<i class="fas fa-image"></i></button>
<span id="gif-text">No file chosen, yet</span>
<div class="save-sketch">
<button type="submit" class="save-sketch-button">Save Sketch</button>
</div>
<div th:if="${message}" th:class="${'alert ' + alertClass}" class="success-message animated slideInDown">
<div class="success-content">
<i class="fas fa-check-circle"></i>
<p th:text="${message}"></p>
</div>
</div>
</div>
</form>
<div th:if="${message}" th:class="${'alert ' + alertClass}" class="success-message animated slideInDown">
<div class="success-content">
<i class="fas fa-check-circle"></i>
<p th:text="${message}"></p>
</div>
</div>
@ResponseBody
, либо создайте контроллер и добавьте аннотацию @RestController
</body>
добавьте js код для аякс.String
или например, Map<String, String>
с нужными данными.<div class="notify" style="display:none;"></div>
show()
(видимым).@keyframes
@PostMapping("/sketches/add")
public String addSketch(
@Valid Sketch sketch,
BindingResult result,
RedirectAttributes redirectAttributes) {
redirectAttributes.addFlashAttribute("action", "save");
if (result.hasErrors()) {
return "admin/add-sketch";
}
sketchRepository.save(sketch);
return "redirect:/admin/sketches/add";
}
<link rel="stylesheet" th:href="@{/libs/toastr/toastr.min.css}">
<script th:src="@{/libs/jquery/jquery-3.4.1.min.js}"></script>
<script defer th:src="@{/libs/toastr/toastr.min.js}"></script>
<script defer th:src="@{/js/add-sketch.js}"></script>
/*TOASTR OPTIONS*/
toastr.options = {
"closeButton": false,
"debug": false,
"newestOnTop": false,
"progressBar": false,
"positionClass": "toast-bottom-right",
"preventDuplicates": false,
"onclick": null,
"showDuration": "300",
"hideDuration": "1000",
"timeOut": "2000",
"extendedTimeOut": "1000",
"showEasing": "swing",
"hideEasing": "linear",
"showMethod": "fadeIn",
"hideMethod": "fadeOut"
};
<script th:inline="javascript">
$(document).ready(function () {
var action = [[${action}]];
if (action === 'save') {
toastr["success"]("Success!")
}
});
</script>