Back-end system six: [Delete] function;

Delete function:

table of Contents

1. Write the delete() method in the XmlDataSource tool class:

 2. Write the delete() method in Dao and call the delete method in the XmlDataSource tool class

3. Write the delete() method in the Service and call the delete method in Dao (there is no additional logic code in the Service)

4. Write the front-end content of list.jsp

5. Write ManagementController


1. Write the delete() method in the XmlDataSource tool class:

package com.imooc.mgallery.utils;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;

import com.imooc.mgallery.entity.Painting;

/**
 * 数据源类,将xml文件解析为Java对象
 * 
 * @author dell
 *
 */
public class XmlDataSource {
	// 为了保证在同一时间,内存中只有一份xml的Document对象,即为了保证加载的xml对象全局唯一
	// 通过static静态关键字保证数据的全局唯一性;(自然也可以通过单例模式)
	private static List<Painting> data = new ArrayList<Painting>();  // 在XmlDataSource类加载的时候,就会创建data对象,这个对象隶属于XmlDataSource类,可以保证是全局唯一;
	private static String dataFile;  // xml文件的地址;
	static {
		// 程序运行编译后,src目录下得java文件会被编译为class文件,这些class文件会被放在classes目录下,而同样处于src目录下的painting.xml文件也会放在classes目录下;
		// XmlDataSource.class.getResoure()得到classes目录的根路径,然后在classes目录的根路径下找到painting.xml,然后getPath()获得painting.xml文件的完整的物理磁盘的地址;	
		dataFile = XmlDataSource.class.getResource("/painting.xml").getPath();
		//System.out.println(dataFile);
		// 如painting.xml文件的地址是:c:\new style\painting.xml;可以发现,new和style之间有一个空格,这个空格是个特殊字符;
		// datFile得到painting.xml文件地址的时候,会进行base64转换,实际dataFile的值会是:c:\new%20style\painting.xml,即空格被转化成了%20;
		// 但是如果在后续中,利用JavaIO对painting.xml文件按照“c:\new%20style\painting.xml”读取时,会提示路径找不到,因为%20不会被JavaIO解析;需要手动的将%20转换为空格;
		// URLDecoder的作用就是:将base64转回普通的字符串;
		reload();
	}
	
	/**
	 * 读取xml文件内容到内存中,把这个功能,提炼成了一个方法
	 */
	private static void reload() {
		data.clear();// 先清空
		URLDecoder decoder = new URLDecoder();
		try {
			dataFile = decoder.decode(dataFile, "UTF-8");  // 这个需要捕获“不支持的编码格式”异常
			//System.out.println(dataFile);
			SAXReader reader = new SAXReader();
			Document document = reader.read(dataFile);  // 需要捕获“DocumentException”异常
			List<Node> nodes = document.selectNodes("/root/painting");
			for(Node node:nodes) {
				Element element = (Element)node;
				// 提取数据,如何将数据转换成Java对象?通过什么载体来保存油画的数据?所以,需要开发对应的JavaBean承载油画数据;
				String id = element.attributeValue("id");
				String pname = element.elementText("pname");
				//
				Painting painting = new Painting();
				painting.setId(Integer.parseInt(id));
				painting.setPname(pname);
				// 剩余几个采用紧凑的写法
				painting.setCategory(Integer.parseInt(element.elementText("category")));
				painting.setPrice(Integer.parseInt(element.elementText("price")));
				painting.setPreview(element.elementText("preview"));
				painting.setDescription(element.elementText("description"));
				// 将对象存储到data集合中;
				data.add(painting);  // 这样以后,当XmlDataSource这个类被加载以后,data集合中就保存了完整的油画信息;
				
			}
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * 为了保证外部可以访问data集合,需要给data增加一个出口;;;
	 * 这儿添加一个方法,getRawData():即获取最原始的信息;
	 * @return
	 */
	public static List<Painting> getRawData(){
		return data;
	}
	

	/**
	 * 向xml中添加
	 * @param painting :从前端封装好的Painting对象,一个Painting对象就是一个油画数据
	 * @throws DocumentException 
	 */
	public static void append(Painting painting) {
		//1.读取XML文档,得到Document对象 
		SAXReader reader = new SAXReader();
		Writer writer = null;
		try {
			Document document = reader.read(dataFile); // xml文件地址,上面已经定义好了,直接拿来用就可以了
			//2.创建新的painting
			Element root = document.getRootElement();  // 获取根节点
			Element p = root.addElement("painting"); // 创建一个新的节点
			// 下面,就根据原始xml的结构,来以此设置它的属性和子节点了
			//3.创建painting节点的各个子节点
			p.addAttribute("id", String.valueOf(data.size()+1));  // 直接在原有节点数的基础上加一就可以了;
			Element pname = p.addElement("pname");
			pname.setText(painting.getPname());
			p.addElement("category").setText(painting.getCategory().toString());
			p.addElement("price").setText(painting.getPrice().toString());
			p.addElement("preview").setText(painting.getPreview());
			p.addElement("description").setText(painting.getDescription());
			// 自此,就创建了一个新的painting节点,属性节点已经设置好了;;即内存中的Document对象就形成了一个全新的油画数据;
			// 接下来,需要把内存中的油画数据,写入到xml文件中
			//4.写入XML,完成追加操作
			writer = new OutputStreamWriter(new FileOutputStream(dataFile),"UTF-8");
			document.write(writer);
			//System.out.println(dataFile);   // 测试用,测试后删除
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch(Exception e) {
			e.printStackTrace();
		}finally {
			if(writer != null) {        // 这儿将writer的关闭操作,写在了finally中;
				try {
					writer.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			reload();// 将重新加载的方法,写在了finally中,这或许也是为了保证reaload()方法一定会被执行的 一个保证措施吧
		}
	}
	
	/**
	 * 更新操作
	 * @param painting
	 */
	public static void update(Painting painting) {
		SAXReader reader = new SAXReader();
		Writer writer = null;
		try {
			Document document = reader.read(dataFile);
			// 利用XPath表达式去查询对应id的节点
			List<Node> nodes = document.selectNodes("/root/painting[@id="+painting.getId()+"]");
			// 如果根据传入的id没有搜索到对应点的painting
			if(nodes.size() == 0) {
				throw new RuntimeException("id="+painting.getId()+"编号油画不存在。");
			}
			Element p = (Element)nodes.get(0); // 将nodes集合中唯一的Element节点提取出来
			// p.selectSingleNode("pname")获取,某一个(大的)Painting节点的,值为“pname”的子节点;;返回值类型当然是一个Element,然后直接调用setTest()重新赋值就可以了;
			p.selectSingleNode("pname").setText(painting.getPname());
			p.selectSingleNode("category").setText(painting.getCategory().toString());
			p.selectSingleNode("price").setText(painting.getPrice().toString());
			p.selectSingleNode("preview").setText(painting.getPreview());
			p.selectSingleNode("description").setText(painting.getDescription());
			// 上面设置以后以后,在内存中就会产生一个包含了新数据的painting节点;
			writer = new OutputStreamWriter(new FileOutputStream(dataFile),"UTF-8");
			document.write(writer);//利用Document文档对象的write()方法,对xml文件进行回写,将数据更新在xml中;
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {  // 不支持的编码异常
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FileNotFoundException e) {  // 文件未发现异常
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			if(writer != null) {
				try {
					writer.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			reload();  //由于xml文件发生了改变,需要调用reload方法,是的xml文件在内存中的那一份copy保持最新
		}
		 
	}
	
/**
 * 工具方法:删除某条数据
 * @param id:要删除数据的id
 */
	public static void delete(Integer id) {
		SAXReader reader = new SAXReader();
		Writer writer = null;
		try {
			Document document = reader.read(dataFile);
			List<Node> nodes = document.selectNodes("/root/painting[@id="+id+"]");
			if(nodes.size() == 0) {
				throw new RuntimeException("待删除id的油画数据不存在。");
			}
			Element p = (Element)nodes.get(0);
			p.getParent().remove(p);
			writer = new OutputStreamWriter(new FileOutputStream(dataFile),"UTF-8");
			document.write(writer);
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			if(writer != null) {
				try {
					writer.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			reload(); 
		}
		
	}
}

Some descriptions of the XmlDataSource tool class:

(1) [Element p = (Element)nodes.get(0); and p.getParent().remove(p);] Pay attention to the method of using Dom4j to delete a node;

(2) Finally, don't forget to reload() to update the copy of xml in memory in time;


 2. Write the delete() method in Dao and call the delete method in the XmlDataSource tool class

	/**
	 * 删除方法,调用工具类XmlDataSource的delete方法,删除油画数据
	 * @param id
	 */
	public void delete(Integer id) {
		XmlDataSource.delete(id);
	}

3. Write the delete() method in the Service and call the delete method in Dao (there is no additional logic code in the Service)

	/**
	 * 删除有有油画数据
	 * @param id
	 */
	public void delete(Integer id) {
		paintingDao.delete(id);
	}

4. Write the front-end content of list.jsp

List.jsp content:

<%@page contentType="text/html;charset=utf-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>


<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>油画列表</title>
<script src="js/jquery-3.4.1.min.js" type="text/javascript"></script>
<script src="js/sweetalert2.js" type="text/javascript"></script>
<link rel="stylesheet" type="text/css" href="css\list.css">
<script type="text/javascript">
    // 在<head></head>中,增加一个自定义函数;
	function showPreview(previewObj){  //形参数previewObj就是【预览】超链接的对象;previewObj是一个HTML原生的的Dom对象
		// 利用jQuery的${previewObj}:可以将previewObj这个HTML原生的Dom对象进行扩展,让其变成一个JQuery对象;
		// 只有previewObj变成jQuery对象之后,才能利用jQUery的attr()方法,对里面的自定义属性进行提取;
		var preview = $(previewObj).attr("data-preview");
		var pname = $(previewObj).attr("data-pname");
		Swal.fire({     // Swal是SweetAlert组件的核心对象;通过调用.fire()方法,在当前页面弹出一个指定样式的对话框
			title:pname,  // 对话框的标题
			// 对话框主体部分,显示的html文本是什么;这儿使用<img>让其显示图片;
			html:"<img src='"+preview+"' style='width:361px;height:240px'>", 
			showCloseButton:true,  // 是否在对话框右上方显示叉号;
			showConfirmButton:false  // 是否显示确认按钮,这儿设置的是不出现;
		})	
	}
    
    function del(delObj){
    	var id = $(delObj).attr("data-id");
    	var pname = $(delObj).attr("data-pname");
    	var preview = $(delObj).attr("data-preview");
    	Swal.fire({
    		title:"确定要删除["+pname+"]这幅油画吗?",
    		html:"<img src='"+preview+"' style='width:361px;height:240px'>", 
    		showCancelButton:true,
    		confirmButtonText:"是",
    		cancelButtonText:"否"
    	}).then(function(result){
			if(result.value==true){
				$.ajax({
					url:"/management?method=delete&id="+id,
					type:"get",
					dataType:"json",
					success:function(json){
						if(json.result=="ok"){
							window.location.reload();
						}else{
							Swal.fire({
								title:json.result
							})
						}
					}
				})
			}
		}		
    	)
    }
</script>
</head>
<body>
	<div class="container">
		<fieldset>
			<legend>油画列表</legend>
			<div style="height: 40px">
				<a href="/management?method=show_create" class="btn-button">新增</a>
			</div>
			<!-- 油画列表 -->
			<table cellspacing="0px">
				<thead>
					<tr style="width: 150px;">
						<th style="width: 100px">分类</th>
						<th style="width: 150px;">名称</th>
						<th style="width: 100px;">价格</th>
						<th style="width: 400px">描述</th>
						<th style="width: 100px">操作</th>
					</tr>
				</thead>
				<c:forEach items="${pageModel.pageData}" var="painting">
					<tr>
						<c:choose>
							<c:when test="${painting.category == 1}">
								<td>现实主义</td>
							</c:when>
							<c:when test="${painting.category == 2}">
								<td>抽象主义</td>
							</c:when>
							<c:otherwise>
								<td>未分类别</td>
							</c:otherwise>						
						</c:choose>
						<td>${painting.pname}</td>
						<td><fmt:formatNumber pattern="¥0.00" value="${painting.price}"></fmt:formatNumber></td>
						<td>${painting.description }</td>
						<td>
							<a class="oplink" data-preview="${painting.preview}" data-pname="${painting.pname}" href="javascript:void(0)" onclick="showPreview(this)">预览</a>
							<a class="oplink" href="/management?method=show_update&id=${painting.id}">修改</a>
							<a class="oplink" href="javascript:void(0)" data-id="${painting.id}" data-pname="${painting.pname}" data-preview="${painting.preview}" onclick="del(this)">删除</a>
						</td>
					</tr>
				</c:forEach>
			</table>
			<!-- 分页组件 -->
			<ul class="page">
				<li><a href="/management?method=list&p=1">首页</a></li>
				<li><a href="/management?method=list&p=${pageModel.hasPreviousPage?pageModel.page-1:1 }">上页</a></li>
				<c:forEach begin="1" end="${pageModel.totalPages}" var="pno" step="1">
					<li ${pno==pageModel.page?"class='active'":""}><span>
						<a href="/management?method=list&p=${pno}">
							${pno}
						</a>
					</span></li>
			<!-- 	<li class='active'><a href="#">1</a></li>
				<li ><a href="#">2</a></li> -->
				</c:forEach>
				<li><a href="/management?method=list&p=${pageModel.hasNextPage?pageModel.page+1:pageModel.totalPages}">下页</a></li>
				<li><a href="/management?method=list&p=${pageModel.totalPages}">尾页</a></li>
			</ul>
		</fieldset>
	</div>

</body>
</html>

A few notes about list.jsp:

(1) 

(2) Swal.fire({}): The core object of the SweetAlert plug-in, where it needs to be used;;;; Then pay attention to its then() method;


5. Write ManagementController

ManagementController class: mainly delete () method content

package com.imooc.mgallery.controller;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.imooc.mgallery.entity.Painting;
import com.imooc.mgallery.service.PaintingService;
import com.imooc.mgallery.utils.PageModel;

/**
 * 后台管理功能Controller;
 * 后台系统,所需要的增,删,改,查的操作,都在这一个Controller类中完成;
 * Servlet implementation class ManagementController
 */
@WebServlet("/management")
public class ManagementController extends HttpServlet {
	private static final long serialVersionUID = 1L;
	// 创建PaintingService对象;;
	// 即无论是前台系统的PaintingController,还是后台系统的ManagementController都调用PaintingService中提供的方法;
	private PaintingService paintingService = new PaintingService();
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ManagementController() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 设置请求体中的字符集编码方式;;;
		// get请求没有请求体,所以,这个语句是为doPost()方法中执行doGet(request,response)后,跳转过来的post请求来设置的;
		// 即这条代码是为doPost()来进行服务的;
		request.setCharacterEncoding("UTF-8");
		// 设置响应的字符集编码方式
		response.setContentType("text/html;charset=utf-8");
		
		String method = request.getParameter("method");
		if(method.equals("list")) {  // 当前台传入的method参数值为“list”的时候,代表是分页请求,调用定义的list方法;
			this.list(request,response);  // 然后,将分页处理的代码写在list方法中就可以了;
		}else if(method.equals("delete")) {  // 当method参数值为“delete”时,表示是删除请求,调用定义的delete方法;
			this.delete(request, response);
		}else if(method.equals("show_create")) {
			// method参数为“show_create”,表示是新增;调用新增的方法,跳转到create.jsp
			this.showCreatePage(request, response);
		}else if(method.equals("create")) {
			this.create(request, response);
		}else if(method.equals("show_update")) {
			// method参数为“show_update”,表示是修改;调用修改的方法,跳转到update.jsp
			this.showUpdatePage(request,response);
		}else if(method.equals("update")) {
			this.update(request, response);
		}
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response); // doPost调用了doGet()方法,所以,把逻辑代码都写在doGet方法中就可以了;
	}
	
	/**
	 * 
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws ServletException 
	 */
	private void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String p = request.getParameter("p");
		String r = request.getParameter("r");
		if(p==null) {
			p = "1";
		}
		if(r==null) {
			r = "6";
		}
		PageModel pageModel = paintingService.pagination(Integer.parseInt(p), Integer.parseInt(r));
		request.setAttribute("pageModel", pageModel);
		request.getRequestDispatcher("/WEB-INF/jsp/list.jsp").forward(request, response);
	}
	
	/**
	 * 显示【新增】页面;这个方法,是一个纯粹的入口;跳转到create.jsp前端页面
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void showCreatePage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.getRequestDispatcher("/WEB-INF/jsp/create.jsp").forward(request, response);
	}
	
	/**
	 * 新增油画数据;; 处理来自于create.jsp提交过来的表单数据
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 * @throws FileUploadException 
	 */
	private void create(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		// 使用FileUpload需要按照以下步骤
		// 1.初始化FileUpload组件
		FileItemFactory factory = new DiskFileItemFactory();
		/**
		 * FileItemFactory :将前端表单的数据转化为一个一个的FileItem对象;即用于数据的转换;
		 * ServletFileUpload :为FileUpload组件提供凝Java web层面上的HTTP请求解析的功能;即提供对web的支持;
		 */
		ServletFileUpload sf = new ServletFileUpload(factory);
		// 2.遍历所有的FileItem
		try {
			// 对原有的request请求进行解析;这个方法执行后,就会将当前请求中每一个提交来的表单数据转化为一个一个的FileItem对象;
			// 并且方法的返回值是一个List
			// 这个会抛出“FileUploadException”,这儿建议对异常进行捕捉而不是抛出;
			// 这个“FileUploadException”异常抛出的时机是:当对form表单进行解析时候,发现当前表单的编码格式并不是【enctype="multipart/form-data"】时候,就会抛出“FileUploadException”异常
			List<FileItem> formData = sf.parseRequest(request);
			Painting painting = new Painting();
			for(FileItem fi:formData) {
				// FileItem对象的isFormField()方法可以判断:这个FileItem对象是一个普通输入项,还是一个文件上传框;
				// 如果FileItem对象是一个普通输入项,该FileItem对象调用isFormField()方法,返回true;
				// 如果FileItem对象是一个文件上传框,该FileItem对象调用isFormField()方法,返回false;
				if(fi.isFormField()) {
					// 这个输出,只是开发阶段的测试用,后续会有其他处理方式;;;
					switch (fi.getFieldName()) {
					case "pname":
						// 当请求中是一个form,而且这个form的编码方式是“multipart/form-data”时候,在doGet()方法中的【request.setCharacterEncoding("UTF-8");】会失效;;;所以这儿在获取请求中(表单的)参数值的时候,需要设置下编码方式
						painting.setPname(fi.getString("UTF-8"));
						break;
					case "category":
						painting.setCategory(Integer.parseInt(fi.getString("UTF-8")));
						break;
					case "price":
						painting.setPrice(Integer.parseInt(fi.getString("UTF-8")));
						break;
					case "description":
						painting.setDescription(fi.getString("UTF-8"));
						break;
					default:
						break;
					}
					//System.out.println("普通输入项"+fi.getFieldName()+":"+fi.getString("UTF-8"));
				}else {
					// 这个输出,只是开发阶段的测试用,后续会有其他处理方式;;;
					System.out.println("文件上传框"+fi.getFieldName());
					// 3.将前端上传的文件,保存到服务器某个目录中
					// getRealPath()方法:获取Tomcat在实际运行环境中,某个对应的目录在(部署了该Tomcat)服务器上的物理地址;
					String path = request.getServletContext().getRealPath("/upload");
					System.out.println(path);
					//String fileName = "test.jpg";
					// UUID:根据计算机(实际中就是Tomcat部署的服务器)的本地特性,根据当前时间,计算机网卡的mac地址,或
					// 者其他的,诸如此类独特的特性,生成一个全世界唯一的字符串;
					// UUID类是Java内置的,可以直接使用;调用randomUUID()就可以得到随机字符串;
					// 以这个随机字符串作为文件名,根本不用担心重名的问题;
					String fileName = UUID.randomUUID().toString();
					// 得到文件扩展名:getName():获取文件的名称;然后substring()获取文件的扩展名
					String suffix = fi.getName().substring(fi.getName().lastIndexOf("."));
					fi.write(new File(path,fileName+suffix));
					painting.setPreview("/upload/"+fileName + suffix);// 设置油画地址
				}
			}
			paintingService.create(painting);
			// 由此,后台部分,油画的新增操作已经完成了;;;;然后可以再跳转到油画列表页面,对数据进行展示
			// 使用响应重定向,调回到油画列表页面;
			// 上面【完成新增数据】和【显示列表页】,这两者并没有明显的直接关系;;;【显示列表页】仅仅是让浏览器跳转到一个全新的功能上,对于此类场景就可以使用响应重定向;
			// 如果此时,【新增完数据 】之后,不是显示列表页,而是弹出另外一个页面,进行新增数据以后的后续操作,这个操作和前面的新增数据是紧密联系的,此时就需要使用请求转发,将当前的请求转给下面的功能,继续进行操作;
			response.sendRedirect("/management?method=list");
		} catch (FileUploadException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch(Exception e) {
			e.printStackTrace();
		}
		
	}
	
	/**
	 * 显示【修改】页面;这个方法,是一个纯粹的入口;跳转到update.jsp前端页面
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws ServletException 
	 */
	public void showUpdatePage(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		String id = request.getParameter("id");// 首先获取前台传递过来的id号
		Painting painting = paintingService.findById(Integer.parseInt(id));  //调动Model层的方法,获取根据id查询的painting对象
		request.setAttribute("painting", painting);// 将查询得到的painting对象,设置成request对象的参数;方便前端获取
		request.getRequestDispatcher("/WEB-INF/jsp/update.jsp").forward(request, response);
	}

	/**
	 * 更新油画数据
	 * @param request
	 * @param response
	 */
	private void update(HttpServletRequest request, HttpServletResponse response) {		
		FileItemFactory factory = new DiskFileItemFactory();
		ServletFileUpload sf = new ServletFileUpload(factory);		
		int isPriviewModified = 0; // 是否上传新文件的标识符
		Painting painting = new Painting();
		try {
			List<FileItem> formData = sf.parseRequest(request);
			for(FileItem fi:formData) {
				if(fi.isFormField()) {
					switch (fi.getFieldName()) {
					case "id":
						painting.setId(Integer.parseInt(fi.getString("UTF-8")));
						break;
					case "pname":
						// 当请求中是一个form,而且这个form的编码方式是“multipart/form-data”时候,在doGet()方法中的【request.setCharacterEncoding("UTF-8");】会失效;;;所以这儿在获取请求中(表单的)参数值的时候,需要设置下编码方式
						painting.setPname(fi.getString("UTF-8"));
						break;
					case "category":
						painting.setCategory(Integer.parseInt(fi.getString("UTF-8")));
						break;
					case "price":
						painting.setPrice(Integer.parseInt(fi.getString("UTF-8")));
						break;
					case "isPreviewModified":
						isPriviewModified = Integer.parseInt(fi.getString("UTF-8"));
						break;
					case "description":
						painting.setDescription(fi.getString("UTF-8"));
						break;
					default:
						break;
					}
				}else if(isPriviewModified == 1) {// 只有当其上传了新的图片文件,formData中才会有文件项,也才会执行到这个else语句
					String path = request.getServletContext().getRealPath("/upload");
					String fileName = UUID.randomUUID().toString();
					String suffix = fi.getName().substring(fi.getName().lastIndexOf("."));
					fi.write(new File(path,fileName+suffix));
					painting.setPreview("/upload/"+fileName + suffix);// 设置油画地址
				}
			}
			paintingService.update(painting, isPriviewModified);
			response.sendRedirect("/management?method=list");
		} catch (FileUploadException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
	}
	
	/**
	 * 删除油画数据
	 * @param request
	 * @param response
	 * @throws IOException 
	 */
	private void delete(HttpServletRequest request, HttpServletResponse response) throws IOException {

		try {
			int id = Integer.parseInt(request.getParameter("id"));
			paintingService.delete(id);
			String result = "{\"result\":\"ok\"}";
			JSONObject json = JSON.parseObject(result);
			response.getWriter().println(json);
			//成功后就返回,成功的JSON字符串
		}catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			String result = "{\"result\":"+e.getMessage()+"}";
			JSONObject json = JSON.parseObject(result);
			response.getWriter().println(json);
		}
		
	}
}

Some descriptions of ManagementController:

(1) A Controller (currently the Servlet class), after receiving a request from the front end, after processing is completed, either request forwarding, response redirection, or direct response.getWriter().println() printing;;;; In other words, there must be an explanation for the response, and there must be no silence or no response;;;; Otherwise, the request will stagnate here and cause problems! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! !

Guess you like

Origin blog.csdn.net/csucsgoat/article/details/115272052