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

前言

暂为最后一篇,节点的添加有点难,因为需要自己自行添加。

实现

addhover 函数请仔细看看,具体代码如下:

<%@ 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 目录树的添加、删除和修改(请不要删除根节点)

     

    CityImpl 添加函数:

    public void addCity(Integer pId, String city) {
    		Connection conn = null;
    		PreparedStatement ps = null;
    		try{   
    				conn = DbUtil.getConnection();
    				String sql = "insert into essaytype(parent_id,province) values(?,?)";
    				ps = conn.prepareStatement(sql);
    				ps.setInt(1, pId);
    				ps.setString(2, city);
    				ps.execute();	
    			}catch(Exception e){
    				e.printStackTrace();
    			}finally{
    				DbUtil.closeAll(conn, ps);
    			}
    	
    	}

    AddCityServlet代码:

    package util;
    
    import java.io.IOException;
    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 net.sf.json.JSONObject;
    
    /**
     * Servlet implementation class AddCityServlet
     */
    @WebServlet("/AddCityServlet")
    public class AddCityServlet extends HttpServlet {
    	private static final long serialVersionUID = 1L;
        private CityDAO c = new CityImpl();
        /**
         * @see HttpServlet#HttpServlet()
         */
        public AddCityServlet() {
            super();
            // TODO Auto-generated constructor stub
        }
    
    	/**
    	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
    	 */
    	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    		Integer parentId = Integer.parseInt(request.getParameter("parentId"));
    		String  city = request.getParameter("city");
    		String msg ="";
    		c.addCity(parentId, city);
    		msg = "添加成功!";                     //我这里默认添加成功了,你可以设置异常,然后分情况
    		JSONObject jO = new JSONObject();
    		jO.put("success", true);
    		jO.put("msg", msg);
    		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)