Advertisement
Guest User

Untitled

a guest
Nov 25th, 2015
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.58 KB | None | 0 0
  1. package app.controller;
  2.  
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.stereotype.Controller;
  5. import org.springframework.ui.Model;
  6. import org.springframework.validation.BindingResult;
  7. import org.springframework.web.bind.annotation.ModelAttribute;
  8. import org.springframework.web.bind.annotation.PathVariable;
  9. import org.springframework.web.bind.annotation.RequestMapping;
  10. import org.springframework.web.bind.annotation.RequestMethod;
  11. import org.springframework.web.servlet.ModelAndView;
  12.  
  13. import app.model.Article;
  14. import app.service.IArticleService;
  15. import app.service.IPhotoService;
  16. import app.service.IUserService;
  17. import app.utils.UserManager;
  18.  
  19. @Controller
  20. public class ArticleController {
  21.  
  22.     @Autowired
  23.     IUserService userService;
  24.    
  25.     @Autowired
  26.     UserManager userManager;
  27.     @Autowired
  28.     IPhotoService photoService;
  29.    
  30.     IArticleService articleService;
  31.    
  32.     @Autowired(required=true)
  33.     public void setArticleService(IArticleService articleService) {
  34.         this.articleService = articleService;
  35.     }
  36.  
  37.     @RequestMapping(value = {"/admin/articles"}, method = RequestMethod.GET)
  38.     public ModelAndView listUsers(Model model) {
  39.         model.addAttribute("article", new Article());
  40.         model.addAttribute("listArticles", this.articleService.getArticleList());
  41.         //model.addAttribute("listArticles", articleService.getArticleById(1));
  42.  
  43.         return new ModelAndView("article");
  44.     }
  45.      
  46.     //For add and update post both
  47.     @RequestMapping(value= "/admin/articles/add", method = RequestMethod.POST)
  48.     public String addUser(@ModelAttribute("article") Article a, BindingResult result){
  49.         a.setUser(userManager.getLoggedUser());
  50.         a.setPhoto(photoService.getPhotoById(1));
  51.         if(a.getId() == 0){
  52.             //new post, add it
  53.             this.articleService.addArticle(a);
  54.         }else{
  55.             //existing post, call update
  56.             this.articleService.updateArticle(a);
  57.         }
  58.          
  59.         return "redirect:/admin/articles";
  60.     }
  61.      
  62.     @RequestMapping("/admin/articles/removeArticle/{id}")
  63.     public String removeUser(@PathVariable("id") int id){
  64.          
  65.         this.articleService.removeArticle(id);
  66.         return "redirect:/admin/articles";
  67.     }
  68.  
  69.     @RequestMapping("/admin/articles/editArticle/{id}")
  70.     public String editUser(@PathVariable("id") int id, Model model){
  71.         model.addAttribute("article", this.articleService.getArticleById(id));
  72.         model.addAttribute("listArticles", this.articleService.getArticleList());
  73.         return "articles";
  74.     }
  75.    
  76.    
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement