how2j.cn

相关下载
文件名 文件大小
tmall_ssh.rar 19m
使用站长秘制下载工具

1分53秒
本视频采用html5方式播放,如无法正常播放,请将浏览器升级至最新版本,推荐火狐,chrome,360浏览器 如果装有迅雷,播放视频呈现直接下载状态,请调整 迅雷系统设置-基本设置-启动-监视全部浏览器 (去掉这个选项)

步骤 1 : 先运行,看到效果,再学习   
步骤 2 : 模仿和排错   
步骤 3 : 效果   
步骤 4 : 用于删除的超链   
步骤 5 : 删除前的确认操作   
步骤 6 : CategoryService   
步骤 7 : CategoryServiceImpl   
步骤 8 : CategoryAction   

步骤 1 :

先运行,看到效果,再学习

老规矩,先下载右上角的可运行项目,配置运行起来,确认可用之后,再学习做了哪些步骤以达到这样的效果。
步骤 2 :

模仿和排错

在确保可运行项目能够正确无误地运行之后,再严格照着教程的步骤,对代码模仿一遍。
模仿过程难免代码有出入,导致无法得到期望的运行结果,此时此刻通过比较正确答案 ( 可运行项目 ) 和自己的代码,来定位问题所在。
采用这种方式,学习有效果,排错有效率,可以较为明显地提升学习速度,跨过学习路上的各个槛。

推荐使用diffmerge软件,进行文件夹比较。把你自己做的项目文件夹,和我的可运行项目文件夹进行比较。
这个软件很牛逼的,可以知道文件夹里哪两个文件不对,并且很明显地标记出来
这里提供了绿色安装和使用教程:diffmerge 下载和使用教程
步骤 3 :

效果

如图所示,本来存在的测试分类10被删除了
效果
步骤 4 :

用于删除的超链

用于删除的超链,指向地址admin_category_delete,并且会传递当前分类对象的id过去。

<a deleteLink="true" href="admin_category_delete?category.id=${c.id}"><span class=" glyphicon glyphicon-trash"></span></a>
用于删除的超链
步骤 5 :

删除前的确认操作

adminHeader.jsp中对所有的删除连接都进行了监听:
$(function(){ $("a").click(function(){ var deleteLink = $(this).attr("deleteLink"); console.log(deleteLink); if("true"==deleteLink){ var confirmDelete = confirm("确认要删除"); if(confirmDelete) return true; return false; } }); })
$(function(){
	$("a").click(function(){
		var deleteLink = $(this).attr("deleteLink");
		console.log(deleteLink);
		if("true"==deleteLink){
			var confirmDelete = confirm("确认要删除");
			if(confirmDelete)
				return true;
			return false;
			
		}
	});
})
步骤 6 :

CategoryService

为CategoryService接口新增加delete(Category category)方法
package com.how2java.tmall.service; import java.util.List; import com.how2java.tmall.pojo.Category; import com.how2java.tmall.util.Page; public interface CategoryService{ public List list(); public void save(Category category); public int total(); public List<Category> listByPage(Page page); public void delete(Category category); }
package com.how2java.tmall.service;

import java.util.List;

import com.how2java.tmall.pojo.Category;
import com.how2java.tmall.util.Page;

public interface CategoryService{
	
	public List list();

	public void save(Category category);
	
	public int total();

	public List<Category> listByPage(Page page);

	public void delete(Category category);
}
步骤 7 :

CategoryServiceImpl

CategoryServiceImpl 实现 delete() 方法
package com.how2java.tmall.service.impl; import java.util.List; import org.hibernate.criterion.DetachedCriteria; import org.hibernate.criterion.Order; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.how2java.tmall.dao.impl.DAOImpl; import com.how2java.tmall.pojo.Category; import com.how2java.tmall.service.CategoryService; import com.how2java.tmall.util.Page; @Service public class CategoryServiceImpl implements CategoryService { @Autowired DAOImpl dao; @Override public List list() { DetachedCriteria dc = DetachedCriteria.forClass(Category.class); dc.addOrder(Order.desc("id")); return dao.findByCriteria(dc); } @Override public int total() { String hql = "select count(*) from Category "; List<Long> l= dao.find(hql); if(l.isEmpty()) return 0; Long result= l.get(0); return result.intValue(); } @Override public List<Category> listByPage(Page page) { DetachedCriteria dc = DetachedCriteria.forClass(Category.class); dc.addOrder(Order.desc("id")); return dao.findByCriteria(dc,page.getStart(),page.getCount()); } @Override public void save(Category category) { dao.save(category); } @Override public void delete(Category category) { dao.delete(category); } }
步骤 8 :

CategoryAction

CategoryAction新增方法用于映射对于路径admin_category_delete的访问
1. 通过categoryService删除Category对象
2. 客户端跳转到listCategoryPage
package com.how2java.tmall.action; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.util.List; import javax.imageio.ImageIO; import org.apache.commons.io.FileUtils; import org.apache.struts2.ServletActionContext; import org.apache.struts2.convention.annotation.Action; import org.apache.struts2.convention.annotation.Namespace; import org.apache.struts2.convention.annotation.ParentPackage; import org.apache.struts2.convention.annotation.Result; import org.apache.struts2.convention.annotation.Results; import org.springframework.beans.factory.annotation.Autowired; import com.how2java.tmall.pojo.Category; import com.how2java.tmall.service.CategoryService; import com.how2java.tmall.util.ImageUtil; import com.how2java.tmall.util.Page; @Namespace("/") @ParentPackage("basicstruts") @Results( { /*分类管理*/ @Result(name="listCategory", location="/admin/listCategory.jsp"), @Result(name="listCategoryPage", type = "redirect", location="/admin_category_list"), }) public class CategoryAction { @Autowired CategoryService categoryService; List<Category> categorys; Category category; File img; Page page; @Action("admin_category_list") public String list() { if(page==null) page = new Page(); int total = categoryService.total(); page.setTotal(total); categorys = categoryService.listByPage(page); return "listCategory"; } @Action("admin_category_add") public String add() { categoryService.save(category); File imageFolder= new File(ServletActionContext.getServletContext().getRealPath("img/category")); File file = new File(imageFolder,category.getId()+".jpg"); try { FileUtils.copyFile(img, file); BufferedImage img = ImageUtil.change2jpg(file); ImageIO.write(img, "jpg", file); } catch (IOException e) { e.printStackTrace(); } return "listCategoryPage"; } @Action("admin_category_delete") public String delete() { categoryService.delete(category); return "listCategoryPage"; } public List<Category> getCategorys() { return categorys; } public void setCategorys(List<Category> categorys) { this.categorys = categorys; } public Page getPage() { return page; } public void setPage(Page page) { this.page = page; } public Category getCategory() { return category; } public void setCategory(Category category) { this.category = category; } public File getImg() { return img; } public void setImg(File img) { this.img = img; } }


HOW2J公众号,关注后实时获知布最新的教程和优惠活动,谢谢。


关于 实践项目-天猫整站SSH-删除 的提问

尽量提供截图代码异常信息,有助于分析和解决问题。 也可进本站QQ群交流: 620943819
提问尽量提供完整的代码,环境描述,越是有利于问题的重现,您的问题越能更快得到解答。
对教程中代码有疑问,请提供是哪个步骤,哪一行有疑问,这样便于快速定位问题,提高问题得到解答的速度
在已经存在的几千个提问里,有相当大的比例,是因为使用了和站长不同版本的开发环境导致的,比如 jdk, eclpise, idea, mysql,tomcat 等等软件的版本不一致。
请使用和站长一样的版本,可以节约自己大量的学习时间。 站长把教学中用的软件版本整理了,都统一放在了这里, 方便大家下载: http://how2j.cn/k/helloworld/helloworld-version/1718.html

上传截图