Java tutorial
/* * Copyright 2005-2013 shopxx.net. All rights reserved. * Support: http://www.shopxx.net * License: http://www.shopxx.net/license */ package net.groupbuy.controller.admin; import java.util.HashSet; import javax.annotation.Resource; import net.groupbuy.Message; import net.groupbuy.Pageable; import net.groupbuy.entity.Article; import net.groupbuy.entity.Tag; import net.groupbuy.entity.Tag.Type; import net.groupbuy.service.ArticleCategoryService; import net.groupbuy.service.ArticleService; import net.groupbuy.service.TagService; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.servlet.mvc.support.RedirectAttributes; /** * Controller - * * @author SHOP++ Team * @version 3.0 */ @Controller("adminArticleController") @RequestMapping("/admin/article") public class ArticleController extends BaseController { @Resource(name = "articleServiceImpl") private ArticleService articleService; @Resource(name = "articleCategoryServiceImpl") private ArticleCategoryService articleCategoryService; @Resource(name = "tagServiceImpl") private TagService tagService; /** * */ @RequestMapping(value = "/add", method = RequestMethod.GET) public String add(ModelMap model) { model.addAttribute("articleCategoryTree", articleCategoryService.findTree()); model.addAttribute("tags", tagService.findList(Type.article)); return "/admin/article/add"; } /** * ? */ @RequestMapping(value = "/save", method = RequestMethod.POST) public String save(Article article, Long articleCategoryId, Long[] tagIds, RedirectAttributes redirectAttributes) { article.setArticleCategory(articleCategoryService.find(articleCategoryId)); article.setTags(new HashSet<Tag>(tagService.findList(tagIds))); if (!isValid(article)) { return ERROR_VIEW; } article.setHits(0L); article.setPageNumber(null); articleService.save(article); addFlashMessage(redirectAttributes, SUCCESS_MESSAGE); return "redirect:list.jhtml"; } /** * */ @RequestMapping(value = "/edit", method = RequestMethod.GET) public String edit(Long id, ModelMap model) { model.addAttribute("articleCategoryTree", articleCategoryService.findTree()); model.addAttribute("tags", tagService.findList(Type.article)); model.addAttribute("article", articleService.find(id)); return "/admin/article/edit"; } /** * */ @RequestMapping(value = "/update", method = RequestMethod.POST) public String update(Article article, Long articleCategoryId, Long[] tagIds, RedirectAttributes redirectAttributes) { article.setArticleCategory(articleCategoryService.find(articleCategoryId)); article.setTags(new HashSet<Tag>(tagService.findList(tagIds))); if (!isValid(article)) { return ERROR_VIEW; } articleService.update(article, "hits", "pageNumber"); addFlashMessage(redirectAttributes, SUCCESS_MESSAGE); return "redirect:list.jhtml"; } /** * */ @RequestMapping(value = "/list", method = RequestMethod.GET) public String list(Pageable pageable, ModelMap model) { model.addAttribute("page", articleService.findPage(pageable)); return "/admin/article/list"; } /** * */ @RequestMapping(value = "/delete", method = RequestMethod.POST) public @ResponseBody Message delete(Long[] ids) { articleService.delete(ids); return SUCCESS_MESSAGE; } }