Created
March 11, 2018 18:15
-
-
Save darbyluv2code/9d01c3c3e1a7f51f4493a339f261dfbe to your computer and use it in GitHub Desktop.
CRM App: How to show Alert Message to user after Successfully Saved Record to db or Update transaction to db
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.luv2code.springdemo.controller; | |
| import java.util.List; | |
| import org.springframework.beans.factory.annotation.Autowired; | |
| import org.springframework.stereotype.Controller; | |
| import org.springframework.ui.Model; | |
| import org.springframework.web.bind.annotation.GetMapping; | |
| import org.springframework.web.bind.annotation.ModelAttribute; | |
| import org.springframework.web.bind.annotation.PostMapping; | |
| import org.springframework.web.bind.annotation.RequestMapping; | |
| import org.springframework.web.bind.annotation.RequestParam; | |
| import com.luv2code.springdemo.entity.Customer; | |
| import com.luv2code.springdemo.service.CustomerService; | |
| @Controller | |
| @RequestMapping("/customer") | |
| public class CustomerController { | |
| // need to inject our customer service | |
| @Autowired | |
| private CustomerService customerService; | |
| @GetMapping("/list") | |
| public String listCustomers(Model theModel) { | |
| // get customers from the service | |
| List<Customer> theCustomers = customerService.getCustomers(); | |
| // add the customers to the model | |
| theModel.addAttribute("customers", theCustomers); | |
| return "list-customers"; | |
| } | |
| @GetMapping("/showFormForAdd") | |
| public String showFormForAdd(Model theModel) { | |
| // create model attribute to bind form data | |
| Customer theCustomer = new Customer(); | |
| theModel.addAttribute("customer", theCustomer); | |
| return "customer-form"; | |
| } | |
| @PostMapping("/saveCustomer") | |
| public String saveCustomer(@ModelAttribute("customer") Customer theCustomer) { | |
| // save the customer using our service | |
| customerService.saveCustomer(theCustomer); | |
| return "redirect:/customer/list?addUpateSuccess=true"; | |
| } | |
| @GetMapping("/showFormForUpdate") | |
| public String showFormForUpdate(@RequestParam("customerId") int theId, | |
| Model theModel) { | |
| // get the customer from our service | |
| Customer theCustomer = customerService.getCustomer(theId); | |
| // set customer as a model attribute to pre-populate the form | |
| theModel.addAttribute("customer", theCustomer); | |
| // send over to our form | |
| return "customer-form"; | |
| } | |
| @GetMapping("/delete") | |
| public String deleteCustomer(@RequestParam("customerId") int theId) { | |
| // delete the customer | |
| customerService.deleteCustomer(theId); | |
| return "redirect:/customer/list"; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> | |
| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <title>List Customers</title> | |
| <!-- reference our style sheet --> | |
| <link type="text/css" | |
| rel="stylesheet" | |
| href="${pageContext.request.contextPath}/resources/css/style.css" /> | |
| </head> | |
| <body> | |
| <div id="wrapper"> | |
| <div id="header"> | |
| <h2>CRM - Customer Relationship Manager</h2> | |
| </div> | |
| </div> | |
| <div id="container"> | |
| <div id="content"> | |
| <!-- put new button: Add Customer --> | |
| <input type="button" value="Add Customer" | |
| onclick="window.location.href='showFormForAdd'; return false;" | |
| class="add-button" | |
| /> | |
| <!-- add our html table here --> | |
| <table> | |
| <tr> | |
| <th>First Name</th> | |
| <th>Last Name</th> | |
| <th>Email</th> | |
| <th>Action</th> | |
| </tr> | |
| <!-- loop over and print our customers --> | |
| <c:forEach var="tempCustomer" items="${customers}"> | |
| <!-- construct an "update" link with customer id --> | |
| <c:url var="updateLink" value="/customer/showFormForUpdate"> | |
| <c:param name="customerId" value="${tempCustomer.id}" /> | |
| </c:url> | |
| <!-- construct an "delete" link with customer id --> | |
| <c:url var="deleteLink" value="/customer/delete"> | |
| <c:param name="customerId" value="${tempCustomer.id}" /> | |
| </c:url> | |
| <tr> | |
| <td> ${tempCustomer.firstName} </td> | |
| <td> ${tempCustomer.lastName} </td> | |
| <td> ${tempCustomer.email} </td> | |
| <td> | |
| <!-- display the update link --> | |
| <a href="${updateLink}">Update</a> | |
| | | |
| <a href="${deleteLink}" | |
| onclick="if (!(confirm('Are you sure you want to delete this customer?'))) return false">Delete</a> | |
| </td> | |
| </tr> | |
| </c:forEach> | |
| </table> | |
| </div> | |
| </div> | |
| <!-- show Alert Message to user after Successfully Saved Record to db or Update transaction to db --> | |
| <c:if test="${param.addUpateSuccess}"> | |
| <script> | |
| alert("The customer was saved in the database successfully."); | |
| </script> | |
| </c:if> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment