ztree 增删修改---修改节点

前言

上一篇我们已经学习过了删除节点的实现,对于修改节点就简单多了,因为上一篇中已经实现了大部分的代码,这里只需添加一些代码就可以了。

实现

为了改动较少,所以我们不去掉上篇中已经实现的删除的功能,就让修改和删除共存!

代码如下:

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










Insert title here




jsp/servelt 目录树的添加、删除和修改(请不要删除根节点)

     

    CityDAO代码:

    package util;
    
    import java.util.List;
    
    public interface CityDAO {
    	public List findTypeChildrenById(Integer id);
    	public void deleteCity(Integer id);
    	public void editCity(Integer id, String city);  //修改
    }

    CityImpl代码(仅给出修改功能代码):

    public void editCity(Integer id, String city) {
    		Connection conn = null;
    		PreparedStatement ps = null;
    		try{   
    				conn = DbUtil.getConnection();
    				String sql = "update essaytype set province = ? where id = ?";
    				ps = conn.prepareStatement(sql);
    				ps.setString(1, city);
    				ps.setInt(2, id);
    				ps.execute();	
    			}catch(Exception e){
    				e.printStackTrace();
    			}finally{
    				DbUtil.closeAll(conn, ps);
    			}
    	}


    修改处理函数editCityServlet:

    package util;
    
    import java.io.IOException;
    import java.util.List;
    
    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 com.sun.org.apache.xalan.internal.xsltc.compiler.Template;
    
    import net.sf.json.JSONObject;
    
    /**
     * Servlet implementation class EditCityServlet
     */
    @WebServlet("/EditCityServlet")
    public class EditCityServlet extends HttpServlet {
    	private static final long serialVersionUID = 1L;
        private CityDAO c = new CityImpl();
        /**
         * @see HttpServlet#HttpServlet()
         */
        public EditCityServlet() {
            super();
            // TODO Auto-generated constructor stub
        }
    
    	/**
    	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
    	 */
    	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    		request.setCharacterEncoding("utf-8");
    		Integer id      =  Integer.parseInt(request.getParameter("id"));
    		String  city = request.getParameter("name");
    		Integer pId = Integer.parseInt(request.getParameter("pId"));
    		List cities = c.findTypeChildrenById(pId);
    		String msg ="";
    		boolean flag = true;   //成功或者失败的标志
    		for(City  temp:cities){          //遍历孩子节点  看看名称是否重复
    			if(temp.getCity().equals(city)){
    				flag= false;
    				break;
    			}
    		}
    		if(flag){
    			c.editCity(id, city);
    			msg = "修改成功!";
    		}
    		else {
    			msg = "修改失败,同一级下该名称已存在,请尝试修改为其它名称!";
    		}
    		JSONObject jO = new JSONObject();
    		jO.put("msg", msg);
    		jO.put("success", flag);
    		response.setContentType("application/json;charset=utf-8");
    		response.getWriter().println(jO.toString());
    	}
    
    	/**
    	 * @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);
    	}
    
    
    

    效果图:

    ztree 增删修改---修改节点_第1张图片

    代码地址

    https://github.com/SpecialYy/ztree-demo-practice

    你可能感兴趣的:(Java,Web)