Skip to content

Commit

Permalink
Confirmation messages displayed for 'Add, edit, or update' actions
Browse files Browse the repository at this point in the history
  • Loading branch information
shadab-stack authored and dsyer committed Jan 31, 2024
1 parent 6fe21e5 commit 2fe9613
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ImportRuntimeHints;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;

import java.util.Locale;

/**
* PetClinic Spring Boot Application.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
Expand All @@ -34,6 +34,7 @@
import org.springframework.web.servlet.ModelAndView;

import jakarta.validation.Valid;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

/**
* @author Juergen Hoeller
Expand Down Expand Up @@ -70,12 +71,14 @@ public String initCreationForm(Map<String, Object> model) {
}

@PostMapping("/owners/new")
public String processCreationForm(@Valid Owner owner, BindingResult result) {
public String processCreationForm(@Valid Owner owner, BindingResult result, RedirectAttributes redirectAttributes) {
if (result.hasErrors()) {
redirectAttributes.addFlashAttribute("error", "There was an error in creating the owner.");
return VIEWS_OWNER_CREATE_OR_UPDATE_FORM;
}

this.owners.save(owner);
redirectAttributes.addFlashAttribute("message", "New Owner Created");
return "redirect:/owners/" + owner.getId();
}

Expand Down Expand Up @@ -133,14 +136,16 @@ public String initUpdateOwnerForm(@PathVariable("ownerId") int ownerId, Model mo
}

@PostMapping("/owners/{ownerId}/edit")
public String processUpdateOwnerForm(@Valid Owner owner, BindingResult result,
@PathVariable("ownerId") int ownerId) {
public String processUpdateOwnerForm(@Valid Owner owner, BindingResult result, @PathVariable("ownerId") int ownerId,
RedirectAttributes redirectAttributes) {
if (result.hasErrors()) {
redirectAttributes.addFlashAttribute("error", "There was an error in updating the owner.");
return VIEWS_OWNER_CREATE_OR_UPDATE_FORM;
}

owner.setId(ownerId);
this.owners.save(owner);
redirectAttributes.addFlashAttribute("message", "Owner Values Updated");
return "redirect:/owners/{ownerId}";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.springframework.web.bind.annotation.RequestMapping;

import jakarta.validation.Valid;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

/**
* @author Juergen Hoeller
Expand Down Expand Up @@ -98,7 +99,7 @@ public String initCreationForm(Owner owner, ModelMap model) {
}

@PostMapping("/pets/new")
public String processCreationForm(Owner owner, @Valid Pet pet, BindingResult result, ModelMap model) {
public String processCreationForm(Owner owner, @Valid Pet pet, BindingResult result, ModelMap model, RedirectAttributes redirectAttributes) {
if (StringUtils.hasText(pet.getName()) && pet.isNew() && owner.getPet(pet.getName(), true) != null) {
result.rejectValue("name", "duplicate", "already exists");
}
Expand All @@ -115,18 +116,19 @@ public String processCreationForm(Owner owner, @Valid Pet pet, BindingResult res
}

this.owners.save(owner);
redirectAttributes.addFlashAttribute("message", "New Pet has been Added");
return "redirect:/owners/{ownerId}";
}

@GetMapping("/pets/{petId}/edit")
public String initUpdateForm(Owner owner, @PathVariable("petId") int petId, ModelMap model) {
public String initUpdateForm(Owner owner, @PathVariable("petId") int petId, ModelMap model,RedirectAttributes redirectAttributes) {
Pet pet = owner.getPet(petId);
model.put("pet", pet);
return VIEWS_PETS_CREATE_OR_UPDATE_FORM;
}

@PostMapping("/pets/{petId}/edit")
public String processUpdateForm(@Valid Pet pet, BindingResult result, Owner owner, ModelMap model) {
public String processUpdateForm(@Valid Pet pet, BindingResult result, Owner owner, ModelMap model,RedirectAttributes redirectAttributes) {

String petName = pet.getName();

Expand All @@ -150,6 +152,7 @@ public String processUpdateForm(@Valid Pet pet, BindingResult result, Owner owne

owner.addPet(pet);
this.owners.save(owner);
redirectAttributes.addFlashAttribute("message", "Pet details has been edited");
return "redirect:/owners/{ownerId}";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.springframework.web.bind.annotation.PostMapping;

import jakarta.validation.Valid;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

/**
* @author Juergen Hoeller
Expand Down Expand Up @@ -81,13 +82,14 @@ public String initNewVisitForm() {
// called
@PostMapping("/owners/{ownerId}/pets/{petId}/visits/new")
public String processNewVisitForm(@ModelAttribute Owner owner, @PathVariable int petId, @Valid Visit visit,
BindingResult result) {
BindingResult result, RedirectAttributes redirectAttributes) {
if (result.hasErrors()) {
return "pets/createOrUpdateVisitForm";
}

owner.addVisit(petId, visit);
this.owners.save(owner);
redirectAttributes.addFlashAttribute("message", "Your vist has been boked");
return "redirect:/owners/{ownerId}";
}

Expand Down
29 changes: 26 additions & 3 deletions src/main/resources/templates/owners/ownerDetails.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,18 @@


<h2>Owner Information</h2>



<div th:if="${message}" class="alert alert-success" id="success-message">
<span th:text="${message}"></span>
</div>

<div th:if="${error}" class="alert alert-danger" id="error-message">
<span th:text="${error}"></span>
</div>




<table class="table table-striped" th:object="${owner}">
<tr>
<th>Name</th>
Expand Down Expand Up @@ -73,7 +83,20 @@ <h2>Pets and Visits</h2>
</tr>

</table>

<script>
// Function to hide the success and error messages after 3 seconds
function hideMessages() {
setTimeout(function() {
document.getElementById("success-message").style.display = "none";
document.getElementById("error-message").style.display = "none";
}, 3000); // 3000 milliseconds (3 seconds)
}

// Call the function to hide messages
hideMessages();
</script>

</body>


</html>

0 comments on commit 2fe9613

Please sign in to comment.