how2j.cn

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

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

步骤 1 : 先运行,看到效果,再学习   
步骤 2 : 模仿和排错   
步骤 3 : 效果   
步骤 4 : 编辑超链   
步骤 5 : CategoryMapper.xml   
步骤 6 : CategoryMapper   
步骤 7 : CategoryService   
步骤 8 : CategoryServiceImpl   
步骤 9 : CategoryController   
步骤 10 : editCategory.jsp   

步骤 1 :

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

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

模仿和排错

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

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

效果

如图所示,点击编辑按钮之后,出现分类编辑页面
效果
步骤 4 :

编辑超链

用于编辑的超链,指向地址admin_category_edit,并且会传递当前分类对象的id过去。

<a href="admin_category_edit?id=${c.id}"><span class="glyphicon glyphicon-edit"></span></a>
编辑超链
步骤 5 :

CategoryMapper.xml

增加通过id获取Category对象的sql语句
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.how2java.tmall.mapper.CategoryMapper"> <select id="list" resultType="Category"> select * from category order by id desc <if test="start!=null and count!=null"> limit #{start},#{count} </if> </select> <select id="total" resultType="int"> select count(*) from category </select> <insert id="add" keyProperty="id" useGeneratedKeys="true" parameterType="Category" > insert into category ( name ) values (#{name}) </insert> <delete id="delete"> delete from category where id= #{id} </delete> <select id="get" resultType="Category"> select * from category where id= #{id} </select> </mapper>
步骤 6 :

CategoryMapper

增加get方法
package com.how2java.tmall.mapper; import com.how2java.tmall.util.Page; import com.how2java.tmall.pojo.Category; import java.util.List; public interface CategoryMapper { List<Category> list(Page page); int total(); void add(Category category); void delete(int id); Category get(int id); }
package com.how2java.tmall.mapper;

import com.how2java.tmall.util.Page;
import com.how2java.tmall.pojo.Category;

import java.util.List;

public interface CategoryMapper {
     List<Category> list(Page page);

     int total();

     void add(Category category);

     void delete(int id);

     Category get(int id);
}
步骤 7 :

CategoryService

增加get方法
package com.how2java.tmall.service; import com.how2java.tmall.pojo.Category; import com.how2java.tmall.util.Page; import java.util.List; public interface CategoryService{ int total(); List<Category> list(Page page); void add(Category category); void delete(int id); Category get(int id); }
package com.how2java.tmall.service;

        import com.how2java.tmall.pojo.Category;
        import com.how2java.tmall.util.Page;
        import java.util.List;

public interface CategoryService{
    int total();
    List<Category> list(Page page);

    void add(Category category);

    void delete(int id);

    Category get(int id);
}
步骤 8 :

CategoryServiceImpl

增加get方法
package com.how2java.tmall.service.impl; import com.how2java.tmall.util.Page; import com.how2java.tmall.mapper.CategoryMapper; import com.how2java.tmall.pojo.Category; import com.how2java.tmall.service.CategoryService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class CategoryServiceImpl implements CategoryService { @Autowired CategoryMapper categoryMapper; @Override public List<Category> list(Page page) { return categoryMapper.list(page); } @Override public int total() { return categoryMapper.total(); } @Override public void add(Category category) { categoryMapper.add(category); } @Override public void delete(int id) { categoryMapper.delete(id); } @Override public Category get(int id) { return categoryMapper.get(id); } }
步骤 9 :

CategoryController

增加edit方法
1. 映射admin_category_edit路径的访问
2. 参数id用来接受注入
3. 通过categoryService.get获取Category对象
4. 把对象放在“c"上
5. 返回editCategory.jsp
@RequestMapping("admin_category_edit") public String edit(int id,Model model) throws IOException { Category c= categoryService.get(id); model.addAttribute("c", c); return "admin/editCategory"; }
package com.how2java.tmall.controller; import com.how2java.tmall.pojo.Category; import com.how2java.tmall.service.CategoryService; import com.how2java.tmall.util.ImageUtil; import com.how2java.tmall.util.Page; import com.how2java.tmall.util.UploadedImageFile; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; import javax.imageio.ImageIO; import javax.servlet.http.HttpSession; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.util.List; @Controller @RequestMapping("") public class CategoryController { @Autowired CategoryService categoryService; @RequestMapping("admin_category_list") public String list(Model model,Page page){ List<Category> cs= categoryService.list(page); int total = categoryService.total(); page.setTotal(total); model.addAttribute("cs", cs); model.addAttribute("page", page); return "admin/listCategory"; } @RequestMapping("admin_category_add") public String add(Category c, HttpSession session, UploadedImageFile uploadedImageFile) throws IOException { categoryService.add(c); File imageFolder= new File(session.getServletContext().getRealPath("img/category")); File file = new File(imageFolder,c.getId()+".jpg"); if(!file.getParentFile().exists()) file.getParentFile().mkdirs(); uploadedImageFile.getImage().transferTo(file); BufferedImage img = ImageUtil.change2jpg(file); ImageIO.write(img, "jpg", file); return "redirect:/admin_category_list"; } @RequestMapping("admin_category_delete") public String delete(int id,HttpSession session) throws IOException { categoryService.delete(id); File imageFolder= new File(session.getServletContext().getRealPath("img/category")); File file = new File(imageFolder,id+".jpg"); file.delete(); return "redirect:/admin_category_list"; } @RequestMapping("admin_category_edit") public String edit(int id,Model model) throws IOException { Category c= categoryService.get(id); model.addAttribute("c", c); return "admin/editCategory"; } }
步骤 10 :

editCategory.jsp

提供editCategory.jsp
在第40行显示分类名称
在第50行提供id
editCategory.jsp
<%@ 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> <script> $(function(){ $("#editForm").submit(function(){ if(!checkEmpty("name","分类名称")) return false; return true; }); }); </script> <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="name" value="${c.name}" type="text" class="form-control"></td> </tr> <tr> <td>分类圖片</td> <td> <input id="categoryPic" accept="image/*" type="file" name="image" /> </td> </tr> <tr class="submitTR"> <td colspan="2" align="center"> <input type="hidden" name="id" value="${c.id}"> <button type="submit" class="btn btn-success">提 交</button> </td> </tr> </table> </form> </div> </div> </div>


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


关于 实践项目-天猫整站SSM-编辑 的提问

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

上传截图