Advertisement
Guest User

Untitled

a guest
May 2nd, 2019
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.00 KB | None | 0 0
  1.  
  2.  
  3. <!DOCTYPE html>
  4.  
  5. <html xmlns:th="http://www.thymeleaf.org">
  6.  
  7. <head>
  8. <meta charset="UTF-8"></meta>
  9. <title>CRUD</title>
  10. <link href="../bootstrap/css/bootstrap.min.css" rel="stylesheet" />
  11. </head>
  12. <body>
  13. <h1>Edit or Delete User</h1>
  14.  
  15. <table class="table table-hover">
  16. <thead>
  17. <tr>
  18. <th>Id</th>
  19. <th>User</th>
  20. </tr>
  21. </thead>
  22.  
  23. <td><span th:text=${user.id}></span></td>
  24. <td><span th:text=${user.name}></span></td>
  25.  
  26. </table>
  27.  
  28. <hr>
  29.  
  30.  
  31. <form action="../update" method="post">
  32.  
  33. <div id="userEdit"></div>
  34.  
  35. <div>
  36. <label for="userpassword">Password:</label> <input type="text"
  37. id="userpassword" name="user_password" /> <input type="hidden"
  38. th:value="${user.id}" name="id" />
  39. </div>
  40. <div class="button">
  41. <button type="submit">Update</button>
  42. </div>
  43. </form>
  44.  
  45. <hr>
  46.  
  47. <form action="../delete" method="delete">
  48. <div class="button">
  49. <input type="hidden" th:value="${user.id}" name="id" />
  50. <button type="submit">Delete</button>
  51. </div>
  52. </form>
  53. </body>
  54. </html>
  55.  
  56. package com.br.crud.controllers;
  57.  
  58. import java.util.List;
  59.  
  60. import org.springframework.beans.factory.annotation.Autowired;
  61. import org.springframework.context.annotation.ComponentScan;
  62. import org.springframework.stereotype.Controller;
  63. import org.springframework.ui.Model;
  64. import org.springframework.web.bind.annotation.ModelAttribute;
  65. import org.springframework.web.bind.annotation.PathVariable;
  66. import org.springframework.web.bind.annotation.RequestMapping;
  67. import org.springframework.web.bind.annotation.RequestMethod;
  68. import org.springframework.web.bind.annotation.RequestParam;
  69. import org.springframework.web.servlet.ModelAndView;
  70. import org.springframework.web.servlet.View;
  71. import org.springframework.web.servlet.mvc.support.RedirectAttributes;
  72.  
  73. import com.br.crud.dao.UserDAO;
  74. import com.br.crud.model.User;
  75. import com.fasterxml.jackson.annotation.JsonCreator.Mode;
  76.  
  77. @ComponentScan
  78. @Controller
  79. public class UserController {
  80.  
  81. @Autowired
  82. private UserDAO dao;
  83.  
  84. @RequestMapping(value = "/", method = RequestMethod.GET)
  85. public String index() {
  86.  
  87. return "index";
  88.  
  89. }
  90.  
  91. @RequestMapping(value = "/", method = RequestMethod.POST)
  92. public ModelAndView login(@RequestParam("user_name") String name, @RequestParam("user_password") String password) {
  93.  
  94. for (int i = 0; i < dao.listUsers().size(); i++) {
  95.  
  96. if (dao.listUsers().get(i).getName().equals(name)
  97. && dao.listUsers().get(i).getPassword().equals(password)) {
  98.  
  99. return new ModelAndView("redirect:crud");
  100. }
  101. }
  102.  
  103. return new ModelAndView("index");
  104.  
  105. }
  106.  
  107. @RequestMapping(value = "crud", method = RequestMethod.GET)
  108. public String userList(Model model) {
  109. List<User> users = dao.listUsers();
  110. model.addAttribute("users", users);
  111.  
  112. return "userList";
  113.  
  114. }
  115.  
  116. @RequestMapping(value = "crud", method = RequestMethod.POST)
  117. public ModelAndView saveUser(@RequestParam("user_name") String name, @RequestParam("user_password") String password,
  118. RedirectAttributes redirectAttributes) {
  119.  
  120. User user = new User(name, password);
  121. dao.save(user);
  122. return new ModelAndView("redirect:crud");
  123.  
  124. }
  125.  
  126. @RequestMapping(value = "edit/{id}")
  127. public ModelAndView edit(@PathVariable("id") Long id) {
  128. ModelAndView modelAndView = new ModelAndView("editUser");
  129. User user = new User();
  130. user = dao.find(id);
  131. modelAndView.addObject("user", user);
  132. return modelAndView;
  133.  
  134. }
  135.  
  136. @RequestMapping(value = "update", method = RequestMethod.POST)
  137. public ModelAndView updateUser(@RequestParam("id") Long id, @RequestParam("user_password") String newPassword) {
  138. User user = dao.find(id);
  139. User newUser = dao.find(user.getId());
  140. newUser.setPassword(newPassword);
  141. dao.save(newUser);
  142.  
  143. return new ModelAndView("redirect:crud");
  144. }
  145.  
  146. @RequestMapping(value = "delete", method = RequestMethod.DELETE)
  147. public ModelAndView deleteUser(@RequestParam("id") Long id) {
  148. User deletedUser = dao.find(id);
  149. dao.delete(deletedUser);
  150. return new ModelAndView("redirect:crud");
  151.  
  152. }
  153.  
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement