博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SpringMVC修改功能
阅读量:6221 次
发布时间:2019-06-21

本文共 7489 字,大约阅读时间需要 24 分钟。

articleList.jsp

 

<%@  taglib  uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> All Articles

List Articles

Add Article
Article ID Article Name Article Desc Added Date
delete edit

DeleteArticle.java

从上个页面获取ID值,并且赋给ids

1 package net.roseindia.controller; 2 import net.roseindia.model.Article; 3 import net.roseindia.service.ArticleService; 4  5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.stereotype.Controller; 7 import org.springframework.ui.Model; 8 import org.springframework.validation.BindingResult; 9 import org.springframework.web.bind.annotation.ModelAttribute;10 import org.springframework.web.bind.annotation.PathVariable;11 import org.springframework.web.bind.annotation.RequestMapping;12 import org.springframework.web.bind.annotation.RequestMethod;13 import org.springframework.web.bind.annotation.RequestParam;14 import org.springframework.web.servlet.ModelAndView;15 16 17 @Controller18 @RequestMapping("/articles")19 public class DeleteController {20     @Autowired21       private ArticleService articleService;22 23       @RequestMapping(value="/delete")24       public String deleteService(@RequestParam("ID")  final Integer ids) {25           System.out.println("delete"+"ID="+ids);26           articleService.deleteService(ids);27 28           return "redirect:/articles.html";29           30       }31       @RequestMapping(value="/edit")32       public ModelAndView editSerivie(@RequestParam("ID") final Integer ids,33               @ModelAttribute("article") Article article,BindingResult result,final Model model){34          System.out.println("edit ID"+ids);35          model.addAttribute("articleId",ids);36          return new ModelAndView("editArticle");37         38           39       }40       /**41       @RequestMapping(value = "/add", method = RequestMethod.GET)42       public ModelAndView addArticle(@ModelAttribute("article") Article article,43           BindingResult result) {44         return new ModelAndView("addArticle");45       }**/46 }

把ids给articleId

articleId作为模型属性能够传给jsp页面

 

editArticle.jsp

1 <%@  taglib  uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 2  3 <%@  taglib  uri="http://www.springframework.org/tags/form" prefix="form"%> 4   5  6  7 Edit Article 8  9 10 11 

Edit Article

12 13
14 15 Show All Articles16 17
18 19
20 21
22 23
24 25 26
27
28
29
30 31
Article Name:
32 33
34 35
36 37
Article Desc:
38 39
40 41
42 43
44 45
46 47 48 49

点击保存后被ArticleController捕获,并做处理,在Dao层进行update数据!做完处理后显示在article.html页面

ArticleController.java

1 package net.roseindia.controller; 2  3 import java.util.HashMap; 4 import java.util.Map; 5  6 import net.roseindia.model.Article; 7 import net.roseindia.service.ArticleService; 8 import org.springframework.beans.factory.annotation.Autowired; 9 import org.springframework.stereotype.Controller;10 import org.springframework.validation.BindingResult;11 import org.springframework.web.bind.annotation.ModelAttribute;12 import org.springframework.web.bind.annotation.PathVariable;13 import org.springframework.web.bind.annotation.RequestMapping;14 import org.springframework.web.bind.annotation.RequestMethod;15 import org.springframework.web.bind.annotation.RequestParam;16 import org.springframework.web.servlet.ModelAndView;17 18 @Controller19 @RequestMapping("/articles")20 public class ArticleController {21 22   @Autowired23   private ArticleService articleService;24 25   @RequestMapping(value = "/save", method = RequestMethod.POST)26   public ModelAndView saveArticle(@ModelAttribute(" article") Article  article,27       BindingResult result) {28       System.out.println("save's articleName"+article.getArticleName());29      articleService.addArticle(article);30     return new ModelAndView("redirect:/articles.html");31   }32  33   @RequestMapping(value = "/editSave", method = RequestMethod.POST)34   public ModelAndView editSaveArticle(@RequestParam("ID")  final Integer ids,35           @ModelAttribute(" article") Article  article,36       BindingResult result) {37       System.out.println("editSaveController--id"+ids);38       System.out.println("editSave's articleName"+article.getArticleName());39      articleService.updateArticle(article,ids);40     return new ModelAndView("redirect:/articles.html");41   }42  43 /**@RequestMapping(value="/delete",method = RequestMethod.POST)44   public String deleteService(@RequestParam("ID")  final Integer ids) {45      System.out.println("hello"+"ID="+ids);46      articleService.deleteService(ids);47      return "redirect:/articles.html";          48      }49 **/50 51 52 53   @RequestMapping(method = RequestMethod.GET)54   public ModelAndView listArticles() {55     Map
model = new HashMap
();56 model.put("articles", articleService.listArticles());57 58 return new ModelAndView("articlesList", model);59 }60 61 @RequestMapping(value = "/add", method = RequestMethod.GET)62 public ModelAndView addArticle(@ModelAttribute("article") Article article,63 BindingResult result) {64 return new ModelAndView("addArticle");65 }66 67 68 69 }

ArticleDaoImpl.java

在Dao层进行update数据!做完处理后显示在article.html页面

1 package net.roseindia.dao; 2  3 import java.util.Date; 4 import java.util.List; 5  6 import net.roseindia.model.Article; 7  8 import org.hibernate.Session; 9 import org.hibernate.SessionFactory;10 import org.springframework.beans.factory.annotation.Autowired;11 import org.springframework.stereotype.Repository;12 13 @Repository("articleDao")14 public class ArticleDaoImpl implements ArticleDao {15 16   @Autowired17   private SessionFactory sessionFactory;18 19   // To Save the article detail20   public void saveArticle(Article article) {21     article.setAddedDate(new Date());22     sessionFactory.getCurrentSession().saveOrUpdate(article);23   }24   public void deleteArticle(Integer articleId){25       System.out.println("Dao-->"+articleId);26       Session session=sessionFactory.openSession();27       Article article = (Article) session.get( Article.class,articleId);28       session.beginTransaction();29       if (null != article) {30           session.delete(article);31       }32       session.getTransaction().commit();33       session.close();34 35   }36   public void updateArticle(Article article,Integer ids){37       System.out.println("upadateDao--->"+ids);38       System.out.println("upadateDao--->"+article.getArticleName());39       Session session=sessionFactory.openSession();40       session.beginTransaction();41       Article newArticle=(Article)session.get(Article.class, ids);42       newArticle.setArticleName(article.getArticleName());43       newArticle.setArticleDesc(article.getArticleDesc());44       session.update(newArticle);45       session.getTransaction().commit();46       session.clear();47       48   }49   // To get list of all articles50   @SuppressWarnings("unchecked")51   public List
listArticles() { 52 return (List
) sessionFactory.getCurrentSession().createCriteria(Article.class).list();53 }54 }

 

转载于:https://www.cnblogs.com/kyxyes/p/3448883.html

你可能感兴趣的文章
通过配置CPU参数 worker_cpu_affinity 提升nginx性能
查看>>
解决的方法:warning: Clock skew detected. Your build may be incomplete.
查看>>
技术型创业者easy遇到的三大问题
查看>>
无法打开SQL Server的连接
查看>>
java泛型中的对象
查看>>
进程和线程区别理解
查看>>
php创建token
查看>>
Android 系统API实现数据库的增删改查和SQLite3工具的使用
查看>>
95、Android下在onCreate中获取控件的宽度和高度(通过回调)
查看>>
UML在需求分析阶段的应用
查看>>
JavaScript:JavaScript事件的处理
查看>>
WEB安全测试的类型
查看>>
ES6笔记(7)-- Promise异步编程
查看>>
早睡早起
查看>>
C#软件监控外部程序运行状态
查看>>
几款开源的图形化Redis客户端管理软件推荐
查看>>
数据库设计中常见表结构的设计技巧
查看>>
CVPR论文《100+ Times Faster Weighted Median Filter (WMF)》的实现和解析(附源代码)。...
查看>>
MATLAB模糊逻辑(2)
查看>>
linux 内核模块管理
查看>>