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
package net.roseindia.controller;
import net.roseindia.model.Article;
import net.roseindia.service.ArticleService; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView; @Controller
@RequestMapping("/articles")
public class DeleteController {
@Autowired
private ArticleService articleService; @RequestMapping(value="/delete")
public String deleteService(@RequestParam("ID") final Integer ids) {
System.out.println("delete"+"ID="+ids);
articleService.deleteService(ids); return "redirect:/articles.html"; }
@RequestMapping(value="/edit")
public ModelAndView editSerivie(@RequestParam("ID") final Integer ids,
@ModelAttribute("article") Article article,BindingResult result,final Model model){
System.out.println("edit ID"+ids);
model.addAttribute("articleId",ids);
return new ModelAndView("editArticle"); }
/**
@RequestMapping(value = "/add", method = RequestMethod.GET)
public ModelAndView addArticle(@ModelAttribute("article") Article article,
BindingResult result) {
return new ModelAndView("addArticle");
}**/
}
把ids给articleId
articleId作为模型属性能够传给jsp页面
editArticle.jsp
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%> <html> <head><title>Edit Article</title></head> <body> <h1>Edit Article</h1> <c:url var="viewArticlesUrl" value="/articles.html" /> <a href="${viewArticlesUrl}">Show All Articles</a> <br /> <br /> <c:url var="editSaveArticleUrl" value="/articles/editSave.html?ID=${articleId}" /> <form:form modelAttribute="article" method="POST" action="${editSaveArticleUrl}"> <c:out value="Article ID:"></c:out>
<c:out value="${articleId}"/>
<br />
<br /> <form:label path="articleName">Article Name:</form:label> <form:input path="articleName" /> <br /> <form:label path="articleDesc">Article Desc:</form:label> <form:textarea path="articleDesc" /> <br /> <input type="submit" value="Save Article" /> </form:form> </body> </html>
点击保存后被ArticleController捕获,并做处理,在Dao层进行update数据!做完处理后显示在article.html页面
ArticleController.java
package net.roseindia.controller; import java.util.HashMap;
import java.util.Map; import net.roseindia.model.Article;
import net.roseindia.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView; @Controller
@RequestMapping("/articles")
public class ArticleController { @Autowired
private ArticleService articleService; @RequestMapping(value = "/save", method = RequestMethod.POST)
public ModelAndView saveArticle(@ModelAttribute(" article") Article article,
BindingResult result) {
System.out.println("save's articleName"+article.getArticleName());
articleService.addArticle(article);
return new ModelAndView("redirect:/articles.html");
} @RequestMapping(value = "/editSave", method = RequestMethod.POST)
public ModelAndView editSaveArticle(@RequestParam("ID") final Integer ids,
@ModelAttribute(" article") Article article,
BindingResult result) {
System.out.println("editSaveController--id"+ids);
System.out.println("editSave's articleName"+article.getArticleName());
articleService.updateArticle(article,ids);
return new ModelAndView("redirect:/articles.html");
} /**@RequestMapping(value="/delete",method = RequestMethod.POST)
public String deleteService(@RequestParam("ID") final Integer ids) {
System.out.println("hello"+"ID="+ids);
articleService.deleteService(ids);
return "redirect:/articles.html";
}
**/ @RequestMapping(method = RequestMethod.GET)
public ModelAndView listArticles() {
Map<String, Object> model = new HashMap<String, Object>();
model.put("articles", articleService.listArticles()); return new ModelAndView("articlesList", model);
} @RequestMapping(value = "/add", method = RequestMethod.GET)
public ModelAndView addArticle(@ModelAttribute("article") Article article,
BindingResult result) {
return new ModelAndView("addArticle");
} }
ArticleDaoImpl.java
在Dao层进行update数据!做完处理后显示在article.html页面
package net.roseindia.dao; import java.util.Date;
import java.util.List; import net.roseindia.model.Article; import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository; @Repository("articleDao")
public class ArticleDaoImpl implements ArticleDao { @Autowired
private SessionFactory sessionFactory; // To Save the article detail
public void saveArticle(Article article) {
article.setAddedDate(new Date());
sessionFactory.getCurrentSession().saveOrUpdate(article);
}
public void deleteArticle(Integer articleId){
System.out.println("Dao-->"+articleId);
Session session=sessionFactory.openSession();
Article article = (Article) session.get( Article.class,articleId);
session.beginTransaction();
if (null != article) {
session.delete(article);
}
session.getTransaction().commit();
session.close(); }
public void updateArticle(Article article,Integer ids){
System.out.println("upadateDao--->"+ids);
System.out.println("upadateDao--->"+article.getArticleName());
Session session=sessionFactory.openSession();
session.beginTransaction();
Article newArticle=(Article)session.get(Article.class, ids);
newArticle.setArticleName(article.getArticleName());
newArticle.setArticleDesc(article.getArticleDesc());
session.update(newArticle);
session.getTransaction().commit();
session.clear(); }
// To get list of all articles
@SuppressWarnings("unchecked")
public List<Article> listArticles() {
return (List<Article>) sessionFactory.getCurrentSession().createCriteria(Article.class).list();
}
}
SpringMVC修改功能的更多相关文章
- SpringMVC由浅入深day01_9商品修改功能开发
9 商品修改功能开发 9.1 需求 操作流程: 1.进入商品查询列表页面 2.点击修改,进入商品修改页面,页面中显示了要修改的商品(从数据库查询) 要修改的商品从数据库查询,根据商品id(主键)查询商 ...
- php大力力 [052节] php数据库页面修改功能
php大力力 [052节] php数据库页面修改功能
- SpringMVC学习--功能完善
简介 在基本的项目中,无非就是基本的增删改查,前面我们已经实现了一个简单的查询功能,现在来实现增删改功能,来了解实际开发中的运用,以修改功能为例,因为修改功能基本覆盖了增加和删除的运用. 前面我们实现 ...
- ajax 实现修改功能
这段时间在做项目,发现自己忘得好快呀,幸亏有博客园帮我记着呢,整理博客园简直不要太重要了哦 因为做的是一个内部管理系统,只用了一个主页面,所有的都不允许整个网页刷新,所以我们只能用ajax 来做,当 ...
- JAVAEE——BOS物流项目04:学习计划、datagrid、分页查询、批量删除、修改功能
1 学习计划 1.datagrid使用方法(重要) n 将静态HTML渲染为datagrid样式 n 发送ajax请求获取json数据创建datagrid n 使用easyUI提供的API创建data ...
- 系统管理模块_岗位管理_改进_使用ModelDroven方案_套用美工写好的页面效果_添加功能与修改功能使用同一个页面
改进_使用ModelDroven方案 @Controller @Scope("prototype") public class RoleAction extends ActionS ...
- 如何使Label有修改功能
如何使Label有修改功能 之前制作一个项目时需要这样一个功能: 双击Label, 随后Label变为TextBox,用户修改后回车,TextBox变回Label 之前使用WPF做了一个,代码如下: ...
- Spring Boot2.0+中,自定义配置类扩展springMVC的功能
在spring boot1.0+,我们可以使用WebMvcConfigurerAdapter来扩展springMVC的功能,其中自定义的拦截器并不会拦截静态资源(js.css等). @Configur ...
- 轻松搭建CAS 5.x系列(5)-增加密码找回和密码修改功能
概述说明 CAS内置了密码找回和密码修改的功能: 密码找回功能是,系统会吧密码重置的连接通过邮件或短信方式发送给用户,用户点击链接后就可以重置密码,cas还支持预留密码重置的问题,只有回答对了,才可以 ...
随机推荐
- Android课程设计第五天欢迎界面(滑动)和图形选择
注意:课程设计只为完成任务,不做细节描述~ 滑动界面 package com.example.myapplication; import android.content.Intent; import ...
- Aria's Loops
https://www.hackerrank.com/contests/101hack41/challenges/arias-loops 可以看我以前的笔记,http://www.cnblogs.co ...
- mvc报( 检测到有潜在危险的 request.form 值 )错的解决方案
今天在做项目中遇到了报( 检测到有潜在危险的 request.form 值 )错,百度过后解决了该问题,出此问题主要还是因为提交的Form中有HTML字符串,例如你在TextBox中输入了html标签 ...
- 关于 a 标签 jquery的trigger("click"),无法触发问题。
这个问题的原因不是jquery的trigger("click"), 函数的问题, 而是 a标签之间要有其他子标签,要对这个子标签调用trigger("click" ...
- AJPFX总结抽象类和接口的区别
/* * 抽象类和接口的区别 * 1.成员的区别 * ...
- AJPFX的内存管理小结
管理范围:任何继承于 NSObject的对象原理:每一个对象都有引用计数器当使用alloc new 和 copy创建对象时引用计数器被设置为1给对象发送一条retain消息 ,引用计数器加1 ...
- 牛人cad二次开发网站(.net)
http://through-the-interface.typepad.com/through_the_interface/autocad_net/ http://through-the-inter ...
- [BZOJ3527][ZJOI2014]力 FFT+数学
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3527 首先卷积的形式是$h(i)=\sum_{i=0}^jf(i)g(i-j)$,如果我们 ...
- iOS 开发App捕获异常, 反馈给服务器, 提高用户体验
在我们开发的app中, 不可避免的, 有时候用户使用软件会崩溃. 我们就需要捕获异常, 可以在入口类中加入相应的代码, 可以在每次用户打开程序的时候, 检查一下沙盒中是否有崩溃日志, 如果有, 可以 ...
- IOS动画之抖动
-(void)shakeView:(UIView*)viewToShake { CGFloat t =2.0; CGAffineTransform translateRight =CGAffineT ...