1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
| @Controller public class MainController { static Map<Integer, Article> map = new HashMap();
static { for (int i = 0; i < 10; i++) { map.put(i, getArticle(i)); } }
@RequestMapping(value = "/", method = RequestMethod.GET) public String index() { return "index"; }
@RequestMapping(value = "/add", method = RequestMethod.GET) public String addPage(Model model) { model.addAttribute("article", new Article()); return "add_page"; }
@RequestMapping(value = "/articles", method = RequestMethod.GET) public String getArticleList(Model model) { List<Article> list = new ArrayList<>(); System.out.println(map); for (Map.Entry<Integer, Article> entry : map.entrySet()) { if (!entry.getValue().isDeleteFlag()) { continue; } list.add(entry.getValue()); } model.addAttribute("articles", list); return "articles"; }
@RequestMapping(value = "/articles/{id}", method = RequestMethod.GET) public String getArticle(Model model, @PathVariable("id") int id) { Article article = map.get(id); model.addAttribute("article", article); return "article"; }
@RequestMapping(value = "/deleteArticle/{id}", method = RequestMethod.GET) public String deleteArticle(Model model, @PathVariable("id") int id) { Article article = map.get(id); article.setDeleteFlag(false); return "delete_success"; }
@RequestMapping(value = "/addArticle", method = RequestMethod.POST) public String addArticle(Model model, @ModelAttribute(value = "article") Article article) { map.put(article.getId(), article); return "add_success"; }
@RequestMapping(value = "/update/{id}", method = RequestMethod.GET) public String update(Model model, @PathVariable("id") int id) { Article article = map.get(id); model.addAttribute("article", article); return "update";
}
@RequestMapping(value = "/updateArticle",method = RequestMethod.POST) public String updateArticle(Model model, @ModelAttribute(value = "article") Article article){ Article article1 = map.get(article.getId()); article1.setTitle(article.getTitle()); article1.setType(article.getType()); article1.setContent(article.getContent()); return "update_success"; }
static public Article getArticle(int i) { Article article = new Article(); article.setId(i); article.setTitle("题目" + i); article.setType("类型" + i); article.setContent("正文" + i); article.setDeleteFlag(true); return article; } }
|