Reputation: 721
I would like to add success notification after user submitted the form.
I've already checked out the Pnotify js library -https://sciactive.com/pnotify/, which looks nice. But no idea how I can use in my CRUD project. I'm using Spring Boot and Thymeleaf for a frontend.
I checked docs:
<button class="btn btn-default source" onclick="new PNotify({
title: 'Regular Success',
text: 'That thing that you were trying to do worked!',
type: 'success',
styling: 'bootstrap3'
});">Success</button>
This is my form:
<form action="#" th:action="@{/contractors-save}"
th:object="${contractor}"
method="post" class="form-horizontal form-label-left">
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12">Name</label>
<div class="col-md-5 col-sm-5 col-xs-12">
<input type="text" th:field="*{name}" class="form-control"
placeholder="name"/>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12">Description</label>
<div class="col-md-5 col-sm-5 col-xs-12">
<input type="text" th:field="*{description}"
class="form-control"
placeholder="description"/>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default"
data-dismiss="modal">
Close
</button>
<input type="submit" value="Save" class="btn btn-success">
</div>
The notification is showing on click action, in my case I need to show after form submission. Could you advise and share with your expertise? Maybe other alternatives.
Upvotes: 3
Views: 5557
Reputation: 721
I followed PRG pattern, as @Minar Mahmud suggested, will share with my implementation:
Controller:
@RequestMapping(value = "/contractor-save", method = RequestMethod.POST)
public String saveContractor(@ModelAttribute(value = "contractor") Contractor contractor,
RedirectAttributes redirectAttributes) {
Contractor savedContractor = contractorService.save(contractor);
redirectAttributes.addFlashAttribute("notification",
String.format("Contractor \"%s\" successfully saved", savedContractor.getName()));
redirectAttributes.addFlashAttribute("action", "save");
return "redirect:/contractors";
}
And in HTML file, showing notification on page load with javascript:
<script th:inline="javascript">
$(document).ready(function () {
var notification = /*[[${notification}]]*/ "";
var action = /*[[${action}]]*/ "";
if (action === 'save') {
new PNotify({
title: 'Save contractor',
text: notification,
type: 'success',
styling: 'bootstrap3',
delay: 3000
});
} else if (action === 'remove') {
new PNotify({
title: 'Remove contractor',
text: notification,
type: 'success',
styling: 'bootstrap3',
delay: 3000
});
} else if (action === 'update') {
new PNotify({
title: 'Edit contractor',
text: notification,
type: 'success',
styling: 'bootstrap3',
delay: 3000
});
}
});
Please fill free to comment. I would like to know is it ok for production? Just to be sure that I'm not reinventing the wheel.
Upvotes: 1
Reputation: 2665
Here is an alternative.
You can follow PRG
pattern and use RedirectAttributes
to add flash attributes.
For example:
@RequestMapping(value = "/contractor", method = RequestMethod.POST)
public String handle(@Valid @ModelAttribute Contractor contractor,
BindingResult result,
RedirectAttributes redirectAttributes) {
// Save contactor ...
redirectAttributes.addFlashAttribute("message", "Successful!");
return "redirect:/someGetUrl";
}
And just show this message
in the view rendered by /someGetUrl
handler.
Upvotes: 2