Guest User

Untitled

a guest
Mar 24th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.56 KB | None | 0 0
  1. @RequestMapping(value = "/add" , method = RequestMethod.GET)
  2. public String add(Model model) {
  3. model.addAttribute("employee",new Employee());
  4. model.addAttribute("certificates",certificateService.getList());
  5. return "add";
  6. }
  7.  
  8. @RequestMapping(value = "/add" , method = RequestMethod.POST)
  9. public String addSave(@ModelAttribute("employee")Employee employee) {
  10. System.out.println(employee);
  11. return "list";
  12. }
  13.  
  14. @Entity
  15. @Table(name = "employee")
  16. public class Employee {
  17.  
  18. @Id
  19. @GeneratedValue(strategy = GenerationType.IDENTITY)
  20. @Column(name = "ID")
  21. private int id;
  22.  
  23. @Column(name = "Name")
  24. private String name ;
  25.  
  26.  
  27. @ManyToMany(fetch = FetchType.EAGER)
  28. @JoinTable(name="emp_cert",
  29. joinColumns={@JoinColumn(name="employee_id")},
  30. inverseJoinColumns={@JoinColumn(name="certificate_id")})
  31. private List<Certificate> certificates;
  32.  
  33.  
  34.  
  35. public Employee() {
  36. if(certificates == null)
  37. certificates = new ArrayList<>();
  38. }
  39.  
  40. public int getId() {
  41. return id;
  42. }
  43.  
  44. public void setId(int id) {
  45. this.id = id;
  46. }
  47.  
  48. public String getName() {
  49. return name;
  50. }
  51.  
  52.  
  53. public void setName(String name) {
  54. this.name = name;
  55. }
  56.  
  57.  
  58. public List<Certificate> getCertificates() {
  59. return certificates;
  60. }
  61.  
  62.  
  63. public void setCertificates(List<Certificate> certificates) {
  64. this.certificates = certificates;
  65. }
  66.  
  67. @Override
  68. public String toString() {
  69. return "Employee [id=" + id + ", name=" + name + "certificates size = "+certificates.size()+" ]";
  70. }
  71.  
  72.  
  73. @Entity
  74. @Table(name = "certificate")
  75. public class Certificate {
  76.  
  77. @Id
  78. @GeneratedValue(strategy = GenerationType.IDENTITY)
  79. @Column(name = "Id")
  80. private int id;
  81.  
  82. @Column(name = "name")
  83. private String name ;
  84.  
  85.  
  86. @ManyToMany(mappedBy="certificates")
  87. private List<Employee> employees ;
  88.  
  89.  
  90. public Certificate() {
  91. if(employees == null)
  92. employees = new ArrayList<>();
  93. }
  94.  
  95. public int getId() {
  96. return id;
  97. }
  98.  
  99. public void setId(int id) {
  100. this.id = id;
  101. }
  102.  
  103. public String getName() {
  104. return name;
  105. }
  106.  
  107. public void setName(String name) {
  108. this.name = name;
  109. }
  110.  
  111. public List<Employee> getEmployees() {
  112. return employees;
  113. }
  114.  
  115. public void setEmployees(List<Employee> employees) {
  116. this.employees = employees;
  117. }
  118.  
  119. @Override
  120. public int hashCode() {
  121. final int prime = 31;
  122. int result = 1;
  123. result = prime * result + id;
  124. return result;
  125. }
  126.  
  127. @Override
  128. public boolean equals(Object obj) {
  129. if (this == obj)
  130. return true;
  131. if (obj == null)
  132. return false;
  133. if (getClass() != obj.getClass())
  134. return false;
  135. Certificate other = (Certificate) obj;
  136. if (id != other.id)
  137. return false;
  138. return true;
  139. }
  140.  
  141. <form action="#" th:action="@{/employee/add}" th:object="${employee}" method="post">
  142. <table>
  143. <tr>
  144. <td>Name</td>
  145. <td><input type="text" th:field="*{name}"></td>
  146. </tr>
  147. <tr>
  148. <td>Certificate</td>
  149. <td>
  150. <th:block th:each="certificate , stat : ${certificates}">
  151. <input type="checkbox" th:field="*{certificates}" name="certificates" th:value="${certificate.id]}"/>
  152. <label th:text="${certificate.name}" ></label>
  153. </th:block>
  154. </td>
  155. </tr>
  156. <tr>
  157. <td colspan="2">
  158. <input type="submit" value="Add"/></td>
  159. </tr>
  160. </table>
  161. </form>
  162.  
  163. <tr>
  164. <td>Certificate</td>
  165. <td>
  166. <th:block th:each="certificate : ${certificates}">
  167. <input type="checkbox" name="cers" th:value="${certificate.id}"/>
  168. <label th:text="${certificate.name}"></label>
  169. </th:block>
  170. </td>
  171. </tr>
  172.  
  173. @RequestMapping(value = "/add" , method = RequestMethod.POST)
  174. public String addSave(
  175. @ModelAttribute("employee")Employee employee ,
  176. @RequestParam(value = "cers" , required = false) int[] cers ,
  177. BindingResult bindingResult , Model model) {
  178.  
  179. if(cers != null) {
  180. Certificate certificate = null ;
  181. for (int i = 0; i < cers.length; i++) {
  182.  
  183. if(certificateService.isFound(cers[i])) {
  184. certificate = new Certificate();
  185. certificate.setId(cers[i]);
  186. employee.getCertificates().add(certificate);
  187. }
  188. }
  189. for (int i = 0; i < employee.getCertificates().size(); i++) {
  190. System.out.println(employee.getCertificates().get(i));
  191. }
  192. }
  193.  
  194. <input type="checkbox" th:field="*{certificates}" name="certificates" th:value="${certificate.id]}"/>
  195.  
  196. <input type="checkbox" th:field="*{certificates}" name="certificates" th:value="${certificate.id}"/>
Add Comment
Please, Sign In to add comment