步骤 1 : 先运行,看到效果,再学习 步骤 2 : 模仿和排错 步骤 3 : 效果 步骤 4 : 用于删除的超链 步骤 5 : 删除前的确认操作 步骤 6 : CategoryService 步骤 7 : CategoryServiceImpl 步骤 8 : CategoryAction
老规矩,先下载右上角的可运行项目,配置运行起来,确认可用之后,再学习做了哪些步骤以达到这样的效果。
在确保可运行项目能够正确无误地运行之后,再严格照着教程的步骤,对代码模仿一遍。
模仿过程难免代码有出入,导致无法得到期望的运行结果,此时此刻通过比较正确答案 ( 可运行项目 ) 和自己的代码,来定位问题所在。 采用这种方式,学习有效果,排错有效率,可以较为明显地提升学习速度,跨过学习路上的各个槛。 推荐使用diffmerge软件,进行文件夹比较。把你自己做的项目文件夹,和我的可运行项目文件夹进行比较。 这个软件很牛逼的,可以知道文件夹里哪两个文件不对,并且很明显地标记出来 这里提供了绿色安装和使用教程:diffmerge 下载和使用教程
如图所示,本来存在的测试分类10被删除了
用于删除的超链,指向地址admin_category_delete,并且会传递当前分类对象的id过去。
<a deleteLink="true" href="admin_category_delete?category.id=${c.id}"><span class=" glyphicon glyphicon-trash"></span></a>
在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; } }); })
为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); }
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);
}
}
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公众号,关注后实时获知布最新的教程和优惠活动,谢谢。
![]() |