SpringMVC+mybatis+ztree实现树的展示

实现页面中显示叔结构,并显示,已保存的节点

如图:SpringMVC+mybatis+ztree实现树的展示_第1张图片

选中的数据是从数据库读取出来的

menu表

SpringMVC+mybatis+ztree实现树的展示_第2张图片

账户与菜单关系表 

 

前台界面:

首先需要导入ztree的js及样式

 JSP页面代码

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>



	
    Tree
    <%@include file="../../include/list.jsp" %>

    

	

    bean

    public class Menu {
    
    	private Long id;
    	
    	private String name;
    	
    	private String url;
    	
    	private Long pId;
    	
    	private boolean open;
    	
    	private boolean checked;
    
    	public Long getId() {
    		return id;
    	}
    
    	public void setId(Long id) {
    		this.id = id;
    	}
    
    	public String getName() {
    		return name;
    	}
    
    	public void setName(String name) {
    		this.name = name;
    	}
    
    	public String getUrl() {
    		return url;
    	}
    
    	public void setUrl(String url) {
    		this.url = url;
    	}
    
    	public Long getpId() {
    		return pId;
    	}
    
    	public void setpId(Long pId) {
    		this.pId = pId;
    	}
    
    	public boolean isOpen() {
    		return open;
    	}
    
    	public void setOpen(boolean open) {
    		this.open = open;
    	}
    
    	public boolean isChecked() {
    		return checked;
    	}
    
    	public void setChecked(boolean checked) {
    		this.checked = checked;
    	}
    	
    }

    Controller代码

    @Controller
    @RequestMapping("/menu")
    public class MenuController {
    	@Autowired
    	private MenuService menuService;
    	
    	/**
    	 * 保存数据
    	 * @param request
    	 * @return
    	 */
    	@RequestMapping(value="saveNode", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    	@ResponseBody
    	public ResponseData saveNode(HttpServletRequest request){
    		ResponseData responseData=new ResponseData();
    		Map map = MapUtil.requestParam2MapString(request);
    		try{
    			menuService.saveNode(map);
    			responseData.setStatus(0);
    			responseData.setMsg("保存数据成功");
    	    } catch (Exception e) {
    			e.printStackTrace();
    			responseData.setStatus(1);
    			responseData.setMsg("保存数据失败");
    		}
    		return  responseData;
    	}
    	
    	@RequestMapping(value="getTreeList", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    	@ResponseBody
    	public ResponseData getTreeList(HttpServletRequest request){
    		ResponseData responseData=new ResponseData();
    		
    		try{
    			List menu = menuService.getMenuByIds();
    			responseData.setStatus(0);
    			responseData.setMsg("获取数据成功");
    			responseData.setData(menu);
    	    } catch (Exception e) {
    			e.printStackTrace();
    			responseData.setStatus(1);
    			responseData.setMsg("获取数据失败");
    		}
    		return  responseData;
    	}
    	
    }

    Service代码

    @Service
    public class MenuService extends BaseService {
    	@Autowired
    	private MenuDao menuDao;
        
    	public List getMenu(){
    		return menuDao.getMenu();
    	}
    	
    	/**
    	 * 插入节点
    	 * @param map
    	 */
    	public void saveNode(Map map){
    		menuDao.deleteNode(map.get("user_id").toString());
    		menuDao.insertNode(map);
    	}
    	
    	/**
    	 * 获取节点
    	 * @return
    	 */
    	public List getMenuByIds(){
    		List list = menuDao.getMenu();
    		Set p_list = new HashSet<>();
    		Map map = menuDao.getMenuIdByUserId("1");
    		String ids [] = String.valueOf(map.get("role_id")).split(",");
    		List idsList = Arrays.asList(ids);
    		// 父节点展开、子节点选中
    		for(Menu m : list){
    			if(m.getpId() == 0){
    				m.setOpen(true); //打开
    			}else{
    				if(idsList.contains(m.getId()+"")){
    					p_list.add(m.getpId());
    					m.setChecked(true); // 选中
    				}
    			}
    		}
    		// 父节点选中
    		for(Menu m : list){
    			if(p_list.contains(m.getId())){
    				m.setChecked(true); // 选中
    			}
    		}
    		
    		return list;
    	}
    	
    }

    DAO代码

    public interface MenuDao extends BaseDao {
    	
    	public List getMenu();
    	
    	public void insertNode(Map map);
    	
    	public Map getMenuIdByUserId(String id);
    	
    	public List getMenuByIds(String [] ids);
    	
    	public void deleteNode(String id);
    }

    XML代码

    
    	
    	
    	
    	
    	
    	
    	
    
    
    		insert into  newmedia_base.role_menu         
    		
    			  user_id, 
    			  role_id, 
    		
    		
    			   #{user_id,jdbcType=INTEGER },
    			   #{role_id,jdbcType=INTEGER },
    		
    	
    	
    	
        	delete from newmedia_base.role_menu where user_id = #{id}
        

     

    你可能感兴趣的:(ztree)