Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package app.controller;
- import org.springframework.beans.factory.annotation.Autowired;
- 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.ModelAndView;
- import app.model.Article;
- import app.service.IArticleService;
- import app.service.IPhotoService;
- import app.service.IUserService;
- import app.utils.UserManager;
- @Controller
- public class ArticleController {
- @Autowired
- IUserService userService;
- @Autowired
- UserManager userManager;
- @Autowired
- IPhotoService photoService;
- IArticleService articleService;
- @Autowired(required=true)
- public void setArticleService(IArticleService articleService) {
- this.articleService = articleService;
- }
- @RequestMapping(value = {"/admin/articles"}, method = RequestMethod.GET)
- public ModelAndView listUsers(Model model) {
- model.addAttribute("article", new Article());
- model.addAttribute("listArticles", this.articleService.getArticleList());
- //model.addAttribute("listArticles", articleService.getArticleById(1));
- return new ModelAndView("article");
- }
- //For add and update post both
- @RequestMapping(value= "/admin/articles/add", method = RequestMethod.POST)
- public String addUser(@ModelAttribute("article") Article a, BindingResult result){
- a.setUser(userManager.getLoggedUser());
- a.setPhoto(photoService.getPhotoById(1));
- if(a.getId() == 0){
- //new post, add it
- this.articleService.addArticle(a);
- }else{
- //existing post, call update
- this.articleService.updateArticle(a);
- }
- return "redirect:/admin/articles";
- }
- @RequestMapping("/admin/articles/removeArticle/{id}")
- public String removeUser(@PathVariable("id") int id){
- this.articleService.removeArticle(id);
- return "redirect:/admin/articles";
- }
- @RequestMapping("/admin/articles/editArticle/{id}")
- public String editUser(@PathVariable("id") int id, Model model){
- model.addAttribute("article", this.articleService.getArticleById(id));
- model.addAttribute("listArticles", this.articleService.getArticleList());
- return "articles";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement