Guest User

Untitled

a guest
Nov 1st, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.75 KB | None | 0 0
  1. @Entity
  2. public class Users {
  3. @Id
  4. @GeneratedValue(strategy=GenerationType.AUTO)
  5. private Long id;
  6.  
  7. @Column(nullable=false, length=30)
  8. private String username;
  9.  
  10. @Column(nullable=false)
  11. private String hashPass;
  12.  
  13. // Unique email
  14. @Column(nullable=false, unique=true)
  15. private String email;
  16.  
  17. @Column(nullable=false)
  18. private String role;
  19.  
  20. @OneToMany(mappedBy="author", cascade=CascadeType.ALL)
  21. private Set<Post> posts= new HashSet<>();
  22.  
  23. public Users(){
  24.  
  25. }
  26.  
  27. public Users(String username){
  28. this.username=username;
  29. }
  30.  
  31. public Users(String username, String hashPass, String email, String role) {
  32. super();
  33. this.username = username;
  34. this.hashPass = hashPass;
  35. this.email = email;
  36. this.role = role;
  37. }
  38.  
  39. public Long getId() {
  40. return id;
  41. }
  42.  
  43. public void setId(Long id) {
  44. this.id = id;
  45. }
  46.  
  47. public String getUsername() {
  48. return username;
  49. }
  50.  
  51. public void setUsername(String username) {
  52. this.username = username;
  53. }
  54.  
  55. public String getHashPass() {
  56. return hashPass;
  57. }
  58.  
  59. public void setHashPass(String hashPass) {
  60. this.hashPass = hashPass;
  61. }
  62.  
  63. public String getEmail() {
  64. return email;
  65. }
  66.  
  67. public void setEmail(String email) {
  68. this.email = email;
  69. }
  70.  
  71. public Set<Post> getPosts() {
  72. return posts;
  73. }
  74.  
  75. public void setPosts(Set<Post> posts) {
  76. this.posts = posts;
  77. }
  78.  
  79.  
  80. public String getRole() {
  81. return role;
  82. }
  83.  
  84. public void setRole(String role) {
  85. this.role = role;
  86. }
  87.  
  88. @Override
  89. public String toString() {
  90. return "Users [id=" + id + ", username=" + username + ", hashPass=" + hashPass + ", email=" + email + ", role="
  91. + role + ", posts=" + posts + "]";
  92. }
  93. }
  94.  
  95. @Entity
  96. public class Post {
  97. @Id
  98. @GeneratedValue(strategy=GenerationType.AUTO)
  99. private Long id;
  100.  
  101. @Column(nullable=false, length=300)
  102. private String title;
  103.  
  104. @Column(nullable=false)
  105. private String body;
  106.  
  107. @ManyToOne(optional=false, fetch=FetchType.LAZY)
  108. @JoinColumn
  109. private Users author;
  110.  
  111. @Column(nullable=false)
  112. private Date date= new Date();
  113.  
  114. public Post(){
  115.  
  116. }
  117.  
  118. public Post(String title, String body, Users author){
  119. super();
  120. this.title=title;
  121. this.body=body;
  122. this.author=author;
  123. }
  124.  
  125. public Long getId() {
  126. return id;
  127. }
  128.  
  129. public void setId(Long id) {
  130. this.id = id;
  131. }
  132.  
  133. public String getTitle() {
  134. return title;
  135. }
  136.  
  137. public void setTitle(String title) {
  138. this.title = title;
  139. }
  140.  
  141. public String getBody() {
  142. return body;
  143. }
  144.  
  145. public void setBody(String body) {
  146. this.body = body;
  147. }
  148.  
  149. public Users getAuthor() {
  150. return author;
  151. }
  152.  
  153. public void setAuthor(Users author) {
  154. this.author = author;
  155. }
  156.  
  157. public Date getDate() {
  158. return date;
  159. }
  160.  
  161. public void setDate(Date date) {
  162. this.date = date;
  163. }
  164.  
  165. @Override
  166. public String toString() {
  167. return "Post [id=" + id + ", title=" + title + ", body=" + body + ", author=" + author + ", date=" + date + "]";
  168. }
  169.  
  170.  
  171. }
  172.  
  173. // POST CONTROLLER
  174. //Add Posts
  175. @RequestMapping(value="/posts/create", method=RequestMethod.GET)
  176. public String addPost(String username, Model model){
  177. model.addAttribute("add", new Post());
  178. model.addAttribute("author", uRepo.findByUsername(username));
  179. return "posts/create";
  180.  
  181. }
  182. //Save post
  183. @RequestMapping(value="/posts/savePost", method=RequestMethod.POST)
  184. public String savePost(Post post){
  185. pRepo.save(post);
  186. return "redirect:/";
  187. }
  188.  
  189. <div class="container">
  190. <h1>Add Post</h1>
  191. <form th:object="${add}" th:action="@{savePost}" action="#" method="post" class="form-horizontal">
  192. <div class="form-group">
  193. <label class="control-label col-sm-2" for="title">Title</label>
  194. <div class="col-sm-10"><input type="text" id="title" th:field="*{title}" /></div>
  195. </div>
  196.  
  197. <div class="form-group">
  198. <label class="control-label col-sm-2" for="body">Body:</label>
  199. <div class="col-sm-10">
  200. <textarea rows="10" id="body" th:field="*{body}"></textarea>
  201. </div>
  202. </div>
  203.  
  204. <div class="form-group">
  205. <label class="control-label col-sm-2" for="author">Author:</label>
  206. <div class="col-sm-10">
  207. <p th:inline="text" id="author" th:field="*{author}">[[${#httpServletRequest.remoteUser}]] <span th:sec:authentication="name"></span></p>
  208. </div>
  209. </div>
  210. <br/>
  211. <input type="submit" value="Save" class="btn btn-info"></input>
  212. </form>
  213. </div>
Add Comment
Please, Sign In to add comment