博客
关于我
强烈建议你试试无所不能的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

你可能感兴趣的文章
VS2010远程调试C#程序
查看>>
[MicroPython]TurniBit开发板DIY自动窗帘模拟系统
查看>>
由String类的Split方法所遇到的两个问题
查看>>
Python3.4 12306 2015年3月验证码识别
查看>>
从Handler.post(Runnable r)再一次梳理Android的消息机制(以及handler的内存泄露)
查看>>
windows查看端口占用
查看>>
strongswan ikev2 server on ubuntu 14.04
查看>>
Yii用ajax实现无刷新检索更新CListView数据
查看>>
JDBC的事务
查看>>
Io流的概述
查看>>
App 卸载记录
查看>>
JavaScript变量和作用域
查看>>
JS 对象机制深剖——new 运算符
查看>>
10大托管国家和5大危险电子邮件主题
查看>>
Go嵌入类型及内部提升样例
查看>>
《软件工艺师:专业、务实、自豪》一3.7.4 软件工艺社团
查看>>
jQuery获取数组对象的值
查看>>
Android+struts2+json方式模拟手机登录功能
查看>>
#大学#汇编指令查询
查看>>
mono for android software自动更新
查看>>