how2j.cn

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

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

步骤 1 : 先运行,看到效果,再学习   
步骤 2 : 模仿和排错   
步骤 3 : 效果   
步骤 4 : 编辑页面提交数据   
步骤 5 : CategoryService   
步骤 6 : CategoryServiceImpl   
步骤 7 : CategoryAction   

步骤 1 :

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

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

模仿和排错

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

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

效果

如图所示,"测试分类A" 的名称被修改为了 "测试分类B"
效果
步骤 4 :

编辑页面提交数据

编辑页面提交数据到admin_category_update

<form method="post" id="editForm" action="admin_category_update"
enctype="multipart/form-data">

1. method="post" 用于提交中文
2. enctype="multipart/form-data" 用于提交二进制文件
编辑页面提交数据
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" import="java.util.*"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <%@include file="../include/admin/adminHeader.jsp"%> <%@include file="../include/admin/adminNavigator.jsp"%> <title>编辑分类</title> <div class="workingArea"> <ol class="breadcrumb"> <li><a href="admin_category_list">所有分类</a></li> <li class="active">编辑分类</li> </ol> <div class="panel panel-warning editDiv"> <div class="panel-heading">编辑分类</div> <div class="panel-body"> <form method="post" id="editForm" action="admin_category_update" enctype="multipart/form-data"> <table class="editTable"> <tr> <td>分类名称</td> <td><input id="name" name="category.name" value="${category.name}" type="text" class="form-control"></td> </tr> <tr> <td>分类圖片</td> <td> <input id="categoryPic" type="file" name="img" /> </td> </tr> <tr class="submitTR"> <td colspan="2" align="center"> <input type="hidden" name="category.id" value="${category.id}"> <button type="submit" class="btn btn-success">提 交</button> </td> </tr> </table> </form> </div> </div> </div>
步骤 5 :

CategoryService

新增update方法
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); }
步骤 6 :

CategoryServiceImpl

实现update方法
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); } }
步骤 7 :

CategoryAction

CategoryAction.update() 方法和CategoryAction.add() 方法的处理很类似,有所不同之处在于增加操作一定会提交图片,而修改不一定提交图片。

在update()方法中做了如下操作:
1. 更新category对象
2. 当发现上传的文件的时候,进行文件更新操作
2.1 根据category对象的id, 计算其对应的图片文件的名称: id.jpg
2.2 然后把 Struts 接受到的浏览器上传的临时文件,复制到 id.jpg。
2.3 借助ImageUtil把这个文件的格式转换为真正的JPG格式。
2.4 通过listCategoryPage,客户端跳转到admin_category_list 路径
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公众号,关注后实时获知布最新的教程和优惠活动,谢谢。


关于 实践项目-天猫整站SSH-修改 的提问

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

上传截图