步骤 1 : 分类管理完成 步骤 2 : Service层的问题 步骤 3 : Action的问题 步骤 4 : 解决办法
截止到现在, 分类管理的功能就已经做好了。 虽然分类管理做好了,但是代码层面的问题开始逐渐浮现出来了。
问题主要表现在Service层,和Action层。
在开发分类管理的过程中,Service层用到了一个接口和一个实现类,分别是CategoryService和CategoryServiceImpl。
这个层有什么问题呢? 首先看接口:CategoryService。 其声明的方法基本上就是CURD和分页。 可以预见的是,在后续做产品管理,用户管理,订单管理等等,也会有这么一个非常近似的CURD的接口,换句话说,这里是有做抽象和代码重构(Refactory)的机会和价值的。 然后看实现类:CategoryServiceImpl。 CategoryServiceImpl本身其实就是个架子,真正起作用的是为其注入的DAO对象,所以这个地方也是可以引入委派模式,使得代码调用更加顺畅。 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);
public Category get(Class clazz, int id);
public void update(Category category);
}
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);
}
@Override
public Category get(Class clazz, int id) {
return (Category) dao.get(clazz, id);
}
@Override
public void update(Category category) {
dao.update(category);
}
}
这里放上了CategoryAction的代码,这样的CategoryAction代码完成功能是没有问题的,但是问题恰恰在于,这样一个本来是用于充当控制层(Controller)的类,需要集中应付太多的需求:
1. 返回页面的定义 2. 单个对象的getter setter 3. 集合对象的getter setter 4. 分页对象的getter setter 5. 上传文件对象的getter setter 6. Service层对象的注入 7. 作为控制层进行的访问路径映射 把所有的这些代码,都放在一个类里面,这个类就会显得繁杂,不易阅读,不易维护。 所以这个地方也是很有代码重构价值的。 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"),
@Result(name="editCategory", location="/admin/editCategory.jsp"),
})
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";
}
@Action("admin_category_edit")
public String edit() {
int id = category.getId();
category = categoryService.get(Category.class,id);
return "editCategory";
}
@Action("admin_category_update")
public String update() {
categoryService.update(category);
if(null!=img){
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";
}
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公众号,关注后实时获知布最新的教程和优惠活动,谢谢。
![]() |