Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package cs544.blog.controller; import cs544.blog.entities.Post; import cs544.blog.entities.User; import cs544.blog.service.IBlogService; import javax.annotation.Resource; import javax.validation.Valid; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.validation.BindingResult; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.servlet.mvc.support.RedirectAttributes; @Controller public class MainController { @Resource private IBlogService blogService; @RequestMapping("/") public String redirectRoot() { return "redirect:mainPage"; } @RequestMapping("/login") public String redirectMain() { return "redirect:mainPage"; } @RequestMapping(value = "/mainPage", method = RequestMethod.GET) public String getMainPage(Model model) { String username = SecurityContextHolder.getContext().getAuthentication().getName(); System.err.println("username " + username); // boolean isLogged = true; // if (username.equals("anonymousUser")){ // isLogged =false; // } model.addAttribute("posts", blogService.getAllPost()); // System.err.println(userService.getAllUser() ); //model.addAttribute("isLogged",isLogged); model.addAttribute("username", username); return "mainPage"; } @RequestMapping(value = "/users", method = RequestMethod.GET) public String getUsers(Model model) { model.addAttribute("users", blogService.getAllUser()); System.err.println("Users" + blogService.getAllUser()); return "userList"; } @RequestMapping(value = "/userPost", method = RequestMethod.GET) public String getUserPage(Model model) { String username = SecurityContextHolder.getContext().getAuthentication().getName(); boolean isLogged = true; if (username.isEmpty()) { isLogged = false; } User user = blogService.getUser(username); model.addAttribute("userPosts", blogService.getUserPosts(user.getId())); model.addAttribute("isLogged", isLogged); return "userPost"; } @RequestMapping(value = "/createPost", method = RequestMethod.GET) public String getCreatePostPage(@ModelAttribute("post") Post post) { return "createPost"; } @RequestMapping(value = "/userPost", method = RequestMethod.POST) public String showUserPage(@Valid Post post, BindingResult result, RedirectAttributes re) { String view = "redirect:userPost"; if (!result.hasErrors()) { String username = SecurityContextHolder.getContext().getAuthentication().getName(); User user = blogService.getUser(username); blogService.createPost(user.getId(), post.getTitle(), post.getContent()); } else { view = "createPost"; } return view; } @RequestMapping(value = "/editPost/{id}", method = RequestMethod.GET) public String getUserPost(@PathVariable long id, Model model) { Post post = blogService.getPost(id); model.addAttribute("post", post); return "postUpdate"; } @RequestMapping(value = "/editPost/{id}", method = RequestMethod.POST) public String updateUserPost(@Valid Post post, BindingResult result, RedirectAttributes re, @PathVariable long id) { String view = "redirect:/userPost"; if (!result.hasErrors()) { blogService.updatePost(id, post); } else { view = "/editPost/{id}"; } return view; } @RequestMapping(value = "/registerUser", method = RequestMethod.GET) public String registerUser(@ModelAttribute("user") User user) { // model.addAttribute("user", new User()); return "registrationPage"; } @RequestMapping(value = "/registerUser", method = RequestMethod.POST) public String saveUser(@Valid User user, String retypepassword, BindingResult result, RedirectAttributes re) { String view = "redirect:/login"; if (!result.hasErrors()) { if (blogService.getUser(user.getUsername()) != null) { return "registrationPage"; } if (!user.getPassword().equals(retypepassword)) { return "registrationPage"; } blogService.createUser(user.getUsername(), user.getPassword()); } else { view = "registrationPage"; } return view; // if() //blogService.createUser(user.getUsername(), user.getPassword()); // model.addAttribute("user", new User()); //return "redirect:login"; } }