• SpringMVC修改功能


    articleList.jsp

    <%@  taglib  uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
    
     
    <html>
    
    <head>
    
    <title>All Articles</title>
    
    </head>
    
    <body>
    
    <h1>List Articles</h1>
    
    <a href="articles/add.html">Add Article</a>
     
    <c:if test="${!empty articles}">
     
    <table>
     
    <tr>
     
    <th>Article ID</th>
     
    <th>Article Name</th>
     
    <th>Article Desc</th>
     
    <th>Added Date</th>
     
    </tr>
    
    <c:forEach items="${articles}" var="article">
     
    <tr>
     
    <td><c:out value="${article.articleId}"/></td>
     
    <td><c:out value="${article.articleName}"/></td>
     
    <td><c:out value="${article.articleDesc}"/></td>
     
    <td><c:out value="${article.addedDate}"/></td>
     
    <!-- <td><a href="#" onclick="getData('articles.do?actionMethod=delete&queryId=${article.articleId}','','workspace');">delete</a></td>  -->
     
    <!-- <td><a href="/articles/delete/${article.articleId}">delete</a></td>  -->
    <td><a href="articles/delete.do?ID=${article.articleId}">delete</a></td> 
    <td><a href="articles/edit.do?ID=${article.articleId}">edit</a></td> 
    </tr>
     
    </c:forEach>
     
    </table>
    
    </c:if>
     
    </body>
    </html>

    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 @Controller
    18 @RequestMapping("/articles")
    19 public class DeleteController {
    20     @Autowired
    21       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 <html>
     6 
     7 <head><title>Edit Article</title></head>
     8 
     9 <body>
    10 
    11 <h1>Edit Article</h1>
    12  
    13 <c:url var="viewArticlesUrl" value="/articles.html" />
    14 
    15 <a href="${viewArticlesUrl}">Show All Articles</a>
    16  
    17 <br />
    18 
    19 <br />
    20 
    21 <c:url var="editSaveArticleUrl" value="/articles/editSave.html?ID=${articleId}" />
    22 
    23 <form:form modelAttribute="article" method="POST" action="${editSaveArticleUrl}">
    24 
    25 
    26 <c:out value="Article ID:"></c:out>
    27  <c:out value="${articleId}"/>
    28  <br />
    29  <br />
    30  
    31 <form:label path="articleName">Article Name:</form:label>
    32  
    33 <form:input path="articleName" />
    34  
    35 <br />
    36  
    37 <form:label path="articleDesc">Article Desc:</form:label>
    38  
    39 <form:textarea path="articleDesc" />
    40  
    41 <br />
    42  
    43 <input type="submit" value="Save Article"  />
    44 
    45 </form:form>
    46  
    47 </body>
    48 
    49 </html>

    点击保存后被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 @Controller
    19 @RequestMapping("/articles")
    20 public class ArticleController {
    21 
    22   @Autowired
    23   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<String, Object> model = new HashMap<String, Object>();
    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   @Autowired
    17   private SessionFactory sessionFactory;
    18 
    19   // To Save the article detail
    20   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 articles
    50   @SuppressWarnings("unchecked")
    51   public List<Article> listArticles() {    
    52     return (List<Article>) sessionFactory.getCurrentSession().createCriteria(Article.class).list();
    53   }
    54 } 

    自信与努力 用心坚持
  • 相关阅读:
    Opencv在mac系统的安装与试用
    VINS 估计器之检查视差
    C语言——第零次作业
    C语言博客05指针
    循环结构
    C语言博客作业数组
    函数3
    C博客作业01分支、顺序结构
    group by的查询
    layui多张图片上传最多9张(新增和修改时的显示问题)
  • 原文地址:https://www.cnblogs.com/kyxyes/p/3448883.html
Copyright © 2020-2023  润新知