how2j.cn

相关下载
文件名 文件大小
请先登录 19m
使用站长秘制下载工具

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

步骤 1 : 重申Action层存在的问题   
步骤 2 : 重构后的效果   
步骤 3 : 重构思路   
步骤 4 : 上传专用 Action4Upload   
步骤 5 : 分页专用 Action4Pagination   
步骤 6 : 对象和集合 Action4Pojo   
步骤 7 : 注入服务专用 Action4Service   
步骤 8 : Action4Service新加一个方法t2p()   
步骤 9 : 定义返回页面的 Action4Result   
步骤 10 : 完整版的Action4Result   
步骤 11 : CategoryAction   
步骤 12 : 可运行项目   

步骤 1 :

重申Action层存在的问题

这里放上了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; } }
步骤 2 :

重构后的效果

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"; } }
步骤 3 :

重构思路

增值内容,请先登录
步骤 4 :

上传专用 Action4Upload

增值内容,请先登录
步骤 5 :

分页专用 Action4Pagination

增值内容,请先登录
步骤 6 :

对象和集合 Action4Pojo

增值内容,请先登录
步骤 7 :

注入服务专用 Action4Service

增值内容,请先登录
步骤 8 :

Action4Service新加一个方法t2p()

增值内容,请先登录
步骤 9 :

定义返回页面的 Action4Result

增值内容,请先登录
步骤 10 :

完整版的Action4Result

增值内容,请先登录
步骤 11 :

CategoryAction

增值内容,请先登录
步骤 12 :

可运行项目

增值内容,请先登录


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


关于 实践项目-天猫整站SSH-Action重构 的提问

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

上传截图