<span>package com.hsj.servlet; import java.io.IOException; import java.io.PrintWriter; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import net.sf.json.JSONArray; import com.hsj.dao.MenuDao; import com.hsj.dao.bean.MenuDaoBean; import com.hsj.domain.Menu; import com.hsj.domain.TreeDTO; public class MenuServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8"); PrintWriter out = response.getWriter(); String method = request.getParameter("method"); if (method != null && !"".equals(method) && "getMenu".equals(method)) { getMenu(request, response); } else if (method != null && !"".equals(method) && "changeMenu".equals(method)) { changeMenu(request, response); } else if (method != null && !"".equals(method) && "addMenu".equals(method)) { addMenu(request, response); } else if (method != null && !"".equals(method) && "updateMenu".equals(method)) { updateMenu(request, response); } else if (method != null && !"".equals(method) && "deleteMenu".equals(method)) { deleteMenu(request, response); } out.flush(); out.close(); } /** * 删除当前菜单及其当前菜单的所有子菜单 * @param request * @param response */ private void deleteMenu(HttpServletRequest request, HttpServletResponse response) { String id=request.getParameter("id"); MenuDao menuDao=new MenuDaoBean(); System.out.println(id); menuDao.deleteChildren(Integer.valueOf(id)); } /** * 修改菜单 * @param request * @param response */ private void updateMenu(HttpServletRequest request, HttpServletResponse response) { String id=request.getParameter("id"); String name=request.getParameter("name"); String url=request.getParameter("url"); MenuDao menuDao=new MenuDaoBean(); Menu menu=menuDao.findMenuById(Integer.valueOf(id)); menu.setName(name); menu.setUrl(url); menuDao.update(menu); } /** * 添加菜单 * @param request * @param response */ private void addMenu(HttpServletRequest request,HttpServletResponse response) { String parentId=request.getParameter("parentId"); String name=request.getParameter("name"); String url=request.getParameter("url"); Menu menu=new Menu(); menu.setName(name); menu.setUrl(url); menu.setParent_id(Integer.valueOf(parentId)); MenuDao menuDao=new MenuDaoBean(); menuDao.save(menu); } /** * 菜单菜单的父菜单 * @param request * @param response */ private void changeMenu(HttpServletRequest request, HttpServletResponse response) { String targetId= request.getParameter("targetId"); String sourceId= request.getParameter("sourceId"); String point= request.getParameter("point"); System.out.println("point="+point); MenuDao menuDao=new MenuDaoBean(); Menu target= menuDao.findMenuById(Integer.valueOf(targetId)); Menu source= menuDao.findMenuById(Integer.valueOf(sourceId)); if("append".equals(point)) { //源菜单作为目标菜单的子菜单 source.setParent_id(target.getId()); }else{ //源菜单和目标菜单使用相同的父菜单的id值 source.setParent_id(target.getParent_id()); } menuDao.update(source); } /** * 根据父id得到它所有的子菜单 * @param request * @param response */ private void getMenu(HttpServletRequest request,HttpServletResponse response) { System.out.println("getMenu-------"); //获取当前展的节点的id try { String parentId=request.getParameter("id"); MenuDao menuDao=new MenuDaoBean(); List<TreeDTO> treeDTOS=menuDao.getChildrenByParentId(parentId); System.out.println(treeDTOS.toString()); response.setContentType("text/html;charset=utf-8"); String json=JSONArray.fromObject(treeDTOS).toString(); response.getWriter().write(json); System.out.println("json="+json); } catch (Exception e) { e.printStackTrace(); } } } </span>
映射文件信息:
Web.xml