步骤 1 : 重申Action层存在的问题 步骤 2 : 重构后的效果 步骤 3 : 重构思路 步骤 4 : 上传专用 Action4Upload 步骤 5 : 分页专用 Action4Pagination 步骤 6 : 对象和集合 Action4Pojo 步骤 7 : 注入服务专用 Action4Service 步骤 8 : Action4Service新加一个方法t2p() 步骤 9 : 定义返回页面的 Action4Result 步骤 10 : 完整版的Action4Result 步骤 11 : CategoryAction 步骤 12 : 可运行项目
这里放上了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;
}
}
CategoryAction 通过继承功能性的父类,将非控制器功能规划到了父类里,
CategoryAction 本身只需要专注于扮演控制器(Controller)本身就行了,不会再出现其他看上去纷繁复杂的其他手段代码 package com.how2java.tmall.action;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import com.how2java.tmall.util.ImageUtil;
import com.how2java.tmall.util.Page;
public class CategoryAction extends Action4Result{
@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() {
t2p(category);
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";
}
}
HOW2J公众号,关注后实时获知布最新的教程和优惠活动,谢谢。
![]() |