###### tags: `Template Notes` `Java` # WOS - Part 4 Starter Template --- - Now we can get all and post and even show our validation errors. Next we need to finish our CRUD opperation. - Get One - Edit - Delete ## Get One: - This is for displaying the view and edit pages ### jsp page showing all the link ``` <a href="/pet/${ pet.id }/view">View</a> ``` ### view jsp - However you chose to call the variable in the controller is how you will call the parts here ``` ${ pet.name } ``` ### edit jsp - Here we will have a form that will be prefilled with the current data ### Controller file edits for get one: - These are the edits for view and or edit using a get one ``` @GetMapping("pet/{id}/view") public String viewPet(@PathVariable Long id, Model model) { model.addAttribute("pet", petService.getOne(id)) return "profile.jsp"; } ``` ### Service file edits for get one: ``` public Pet getOne(Long id) { return petRepository.findById(id).orElse(null); } ``` ## Updating: - Here we will need to add the required functions and pages to render a form that is prepopulated with the current data for the single instance and then allow us to hit submit and send the changes to the database ### jsp file: ``` <form:form action="/pet/${ pet.id }/update" method="POST" modelAttribute="editPet"> <input type="hidden" name="_method" value="put" /> <section> <form:label path="name" class="form-label">Name</form:label> <form:input path="name" class="form-control" type="text" value="${ pet.name }" /> <form:errors path="name" class="text-bg-danger" /> </section> <section> <form:label path="species" class="form-label">Species</form:label> <form:input path="species" class="form-control" type="text" value="${ pet.name }" /> <form:errors path="species" class="text-danger" /> </section> <section> <form:label class="form-label" path="gender">Gender</form:label> <section> <form:label path="gender" class="form-label">Female</form:label> <form:radiobutton class="form-check-input" lable="Female" path="gender" value="1"/> </section> <section> <form:label path="gender" class="form-label">Male</form:label> <form:radiobutton class="form-check-input" lable="Male" path="gender" value="0"/> </section> </section> <section> <form:label path="age" class="form-label">Age</form:label> <form:input path="age" class="form-control" type="number" value="${ pet.name }" /> <form:errors path="age" class="text-danger" /> </section> <button class="btn btn-primary">Submit</button> </form:form> ``` ### Controller file: - The rendering of the edit page looks just like the view with the change of the mapping we just need to now add the update function ``` @PutMapping("pet/{id}/update") public String updatePet(@Valid @ModelAttribute("editPet") Pet editPet, BindingResult result, Model model) { if(result.hasErrors()) { model.addAttribute("editPet"); return "editPet.jsp"; } petService.update(editPet); return "redirect:/"; } ``` ### service file: - Since we are using the same getOne function for the rendering of the edit page we just need to add the update function ``` public Pet updae(Pet editPet) { return petRepository.save(editPet); } ``` ## Deleting: - Here we are going to create the way to delete a single item from the database ### jsp file: - Convention want you to put a link like our edit and view links however that can allow items to be deleted via the address bar. - Instead we will use a form here ``` <form action="/pet/${ id }/delete" method="POST"> <input type="hidden" name="_method" value="delete" /> <input type="submit" value="Delete" class="btn btn-warning" /> </form> ``` ### Controller file: ``` @DeleteMapping("/pet/{id}/delete") public String deletePet(@PathVariable Long id) { petService.deleteOne(id); return "redirect:/"; } ``` ### Service file: ``` public void deleteOne(Long id) { petRepository.deleteById(id); } ```