easyui项目——网上书城之登陆注册具有权限的树形菜单

项目介绍:

一、登陆注册

所有的工具类以及jar包请参考之前文章

1.前端代码

1.1登陆界面

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>网上书城登录</title>
    <link href="https://cdn.bootcss.com/twitter-bootstrap/4.4.1/css/bootstrap.css" rel="stylesheet">
    <link href="${pageContext.request.contextPath}/static/css/fg.css" rel="stylesheet">
    <script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.js"></script>
    <script src="https://cdn.bootcss.com/twitter-bootstrap/4.4.1/js/bootstrap.js"></script>

</head>
<body class="text-center">
<form class="form-signin" action="${pageContext.request.contextPath}/user.action?methodName=login" method="post">
    <h1 class="h3 mb-3 font-weight-normal">用户登录</h1>
    <label for="name" class="sr-only">账号</label>
    <input type="text" id="name" name="name" class="form-control" placeholder="请输入账号" required autofocus>
    <label for="pwd" class="sr-only">密码</label>
    <input type="password" id="pwd" name="pwd" class="form-control" placeholder="请输入密码" required>
    <div class="checkbox mb-3">
        <label>
            <input type="checkbox" value="remember-me"> Remember me
        </label>
    </div>
    <button class="btn btn-lg btn-primary btn-block" type="submit" id="login">登录</button>
    <p class="mt-5 mb-3 text-muted">&copy; 2019-2021</p>
</form>

<script>
    $(function () {

        <%--$("#login").click(function () {--%>
            <%--$.ajax({--%>
                <%--url:'${pageContext.request.contextPath}/user.action?methodName=login',--%>
                <%--data:"name="+$("#name").val()+"&pwd="+$("#pwd").val(),--%>
                <%--success:function (data) {--%>

                <%--}--%>
            <%--});--%>
        <%--});--%>
    })
</script>
</body>
</html>

1.2注册界面

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>网上书城注册</title>
    <link href="https://cdn.bootcss.com/twitter-bootstrap/4.4.1/css/bootstrap.css" rel="stylesheet">
    <link href="${pageContext.request.contextPath}/static/css/fg.css" rel="stylesheet">
    <script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.js"></script>
    <script src="https://cdn.bootcss.com/twitter-bootstrap/4.4.1/js/bootstrap.js"></script>

</head>
<body class="text-center">
<form class="form-signin" action="${pageContext.request.contextPath}/user.action?methodName=register" method="post">
    <h1 class="h3 mb-3 font-weight-normal">用户注册</h1>
    <label for="name" class="sr-only">账号</label>
    <input type="text" id="name" name="name" class="form-control" placeholder="请输入账号" required autofocus>
    <label for="pwd" class="sr-only">密码</label>
    <input type="password" id="pwd" name="pwd" class="form-control" placeholder="请输入密码" required>
    <div class="checkbox mb-3">
        <label>
            <input type="checkbox" value="remember-me"> Remember me
        </label>
    </div>
    <button class="btn btn-lg btn-primary btn-block" type="submit" id="">注册</button>
    <p class="mt-5 mb-3 text-muted">&copy; 2017-2020</p>
</form>
</body>
</html>

2.实体类

用户表介绍:

bigint-->long 

2.1User

package com.zking.entiy;

public class User {
private long id;
private String name;
private String pwd;
private int type;
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 getPwd() {
	return pwd;
}
public void setPwd(String pwd) {
	this.pwd = pwd;
}
public int getType() {
	return type;
}
public void setType(int type) {
	this.type = type;
}
@Override
public String toString() {
	return "User [id=" + id + ", name=" + name + ", pwd=" + pwd + ", type=" + type + "]";
}
public User(long id, String name, String pwd, int type) {
	super();
	this.id = id;
	this.name = name;
	this.pwd = pwd;
	this.type = type;
}
public User() {
	// TODO Auto-generated constructor stub
}

}

3.后端代码

3.1登陆注册方法

package com.zking.dao;



import com.zking.entiy.User;
import com.zking.util.BaseDao;

public class UserDao extends BaseDao<User>{
	/**
	 * 登陆
	 * @param user
	 * @return
	 * @throws Exception
	 */
public User login(User user) throws Exception {
	String sql="select * from t_easyui_user where name ='"+user.getName()+"' and pwd = '"+user.getPwd()+"'";
	return super.executeQuery(sql, User.class, null).get(0);
}

/**
 * 注册
 * @param user
 * @throws Exception
 */
public void add(User user) throws Exception {
	String sql="insert into t_easyui_user(name,pwd) values(?,?)";
	super.executeUpdate(sql, user, new String[] {"name","pwd"});
}
}

3.2子控制器

package com.zking.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zking.dao.UserDao;
import com.zking.entiy.User;
import com.zking.framework.ActionSupport;
import com.zking.framework.ModelDriver;

public class UserAction extends ActionSupport implements ModelDriver<User>{
private User user=new User();
private UserDao userDao=new UserDao();
	@Override
	public User getModel() {
		// TODO Auto-generated method stub
		return user;
	}

		public String login(HttpServletRequest req, HttpServletResponse resp) {
			try {
				User u = userDao.login(user);
				if(u == null) {
					return "toLogin";
				}
				req.getSession().setAttribute("cuser", u);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return "toLogin";
			}
			//只要数据库中有这个用户,就跳转到主界面
			return "main";
		}
		public String register(HttpServletRequest req, HttpServletResponse resp) {
			try {
				userDao.add(user);
				req.setAttribute("msg", "用户名密码错误");
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return "toRegister";
			}
			return "toLogin";
		}
}

4.配置文件

4.1mvc2.xml

<?xml version="1.0" encoding="UTF-8"?>
<config>
	<action path="/user" type="com.zking.web.UserAction">
		<forward name="main" path="/bg/mainTemp.jsp" redirect="false"></forward>
		<forward name="toLogin" path="/login.jsp" redirect="true"></forward>
		<forward name="toRegister" path="/register.jsp" redirect="false"></forward>
	</action>

4.2中央控制器的配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>pro_mvc_book</display-name>
 <servlet>
  	<servlet-name>mvc</servlet-name>
  	<servlet-class>com.zking.framework.DispatchServlet</servlet-class>
  	<init-param>
  		<param-name>configurationLocation</param-name>
  		<param-value>/mvc2.xml</param-value>
  	</init-param>
  </servlet>
  <servlet-mapping>
  	<servlet-name>mvc</servlet-name>
  	<url-pattern>*.action</url-pattern>
  </servlet-mapping>
</web-app>

5.效果展示

 

 二、具有权限的树形展示

卖家和买家拥有不同的权限,不同的用户登录进来菜单不同,假设用户登录,只能显示用户菜单

方法:保存用户登录信息,根据type属性查询用户权限表,在用户权限表获取菜单id,根据菜单id获取菜单

表数据展示

1.实体类

category类别表

package com.zking.entiy;

public class Category {
private long id;
private String name;
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;
}
@Override
public String toString() {
	return "Categroy [id=" + id + ", name=" + name + "]";
}
public Category(long id, String name) {
	super();
	this.id = id;
	this.name = name;
}
public Category() {
	// TODO Auto-generated constructor stub
}
}

 permission菜单

package com.zking.entiy;

public class Permission {
private long id;
private String name;
private String description;
private String url;
private long pid;
private int ismenu;
private long displayno;
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 getDescription() {
	return description;
}
public void setDescription(String description) {
	this.description = description;
}
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 int getIsmenu() {
	return ismenu;
}
public void setIsmenu(int ismenu) {
	this.ismenu = ismenu;
}
public long getDisplayno() {
	return displayno;
}
public void setDisplayno(long displayno) {
	this.displayno = displayno;
}
@Override
public String toString() {
	return "Permission [id=" + id + ", name=" + name + ", description=" + description + ", url=" + url + ", pid=" + pid
			+ ", ismenu=" + ismenu + ", displayno=" + displayno + "]";
}
public Permission(long id, String name, String description, String url, long pid, int ismenu, long displayno) {
	super();
	this.id = id;
	this.name = name;
	this.description = description;
	this.url = url;
	this.pid = pid;
	this.ismenu = ismenu;
	this.displayno = displayno;
}

public Permission() {
	// TODO Auto-generated constructor stub
}
}

 role权限菜单

package com.zking.entiy;

public class RolePermission {
private long rid;
private long pid;
public long getRid() {
	return rid;
}
public void setRid(long rid) {
	this.rid = rid;
}
public long getPid() {
	return pid;
}
public void setPid(long pid) {
	this.pid = pid;
}
@Override
public String toString() {
	return "RolePermission [rid=" + rid + ", pid=" + pid + "]";
}
public RolePermission(long rid, long pid) {
	super();
	this.rid = rid;
	this.pid = pid;
}
public RolePermission() {
	// TODO Auto-generated constructor stub
}
}

2.后端代码

2.1dao方法

package com.zking.dao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.zking.entiy.Permission;
import com.zking.util.BaseDao;
import com.zking.util.BuildTree;
import com.zking.util.PageBean;
import com.zking.util.TreeVo;

public class PermissionDao extends BaseDao<Permission>{

	/**
	 * 
	 * @param permission
	 * @param pageBean
	 * @return
	 * @throws Exception
	 */
	public List<Permission> list(Permission permission, PageBean pageBean) throws Exception {
		String sql="select * from t_easyui_permission where 1=1";
		return super.executeQuery(sql, Permission.class, pageBean);
	}
	
	public List<Permission> listPlus(String ids) throws Exception {
		
		String sql="select * from t_easyui_permission where id in ("+ids+")";
		return super.executeQuery(sql, Permission.class, null);
	}
	
	
	public List<TreeVo<Permission>> tree(Permission permission,PageBean pageBean) throws Exception{
		//拿到平级数据list方法
			List<Permission>list=this.list(permission, null);
			//建立一个装list数据的数组
			List<TreeVo<Permission>> listvos=new ArrayList<TreeVo<Permission>>();
			//遍历list数据
			for (Permission p : list) {
				TreeVo<Permission>vo=new TreeVo<>();
				vo.setId(p.getId()+"");
				vo.setText(p.getName());
				vo.setParentId(p.getPid()+"");
			Map<String, Object> attributes=new HashMap<String, Object>();
			attributes.put("self", p);//selef随便取一个名字
			
			vo.setAttributes(attributes);
			listvos.add(vo);
			}
			return  BuildTree.buildList(listvos,"0");
		}
	public List<TreeVo<Permission>> treePuls(String ids) throws Exception{
		//拿到平级数据list方法
			List<Permission>list=this.listPlus(ids);
			//建立一个装list数据的数组
			List<TreeVo<Permission>> listvos=new ArrayList<TreeVo<Permission>>();
			//遍历list数据
			for (Permission p : list) {
				TreeVo<Permission>vo=new TreeVo<>();
				vo.setId(p.getId()+"");
				vo.setText(p.getName());
				vo.setParentId(p.getPid()+"");
			Map<String, Object> attributes=new HashMap<String, Object>();
			attributes.put("self", p);//selef随便取一个名字
			
			vo.setAttributes(attributes);
			listvos.add(vo);
			}
			return  BuildTree.buildList(listvos,"0");
		}
	
}
package com.zking.dao;

import java.util.List;

import com.zking.entiy.RolePermission;
import com.zking.util.BaseDao;

public class RolePermissionDao extends BaseDao<RolePermission>{

	/**
	 * 通过user表的type字段 查询,查询出商家或者买家卖家对应的菜单id
	 * @param type
	 * @return
	 * @throws Exception
	 */
	public List<RolePermission> finRolePermission(int type) throws Exception {
		String sql="select * from t_easyui_role_permission where rid="+type;
		return super.executeQuery(sql, RolePermission.class, null);
	}
}

2.2.子控制器

package com.zking.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zking.dao.PermissionDao;
import com.zking.dao.RolePermissionDao;
import com.zking.dao.UserDao;
import com.zking.entiy.Permission;
import com.zking.entiy.RolePermission;
import com.zking.entiy.User;
import com.zking.framework.ActionSupport;
import com.zking.framework.ModelDriver;
import com.zking.util.ResponseUtil;
import com.zking.util.TreeVo;

public class PermissionAction extends ActionSupport implements ModelDriver<Permission> {
	private Permission permission = new Permission();
	private PermissionDao permissionDao = new PermissionDao();
	private RolePermissionDao rolePermissionDao=new RolePermissionDao();
	@Override
	public Permission getModel() {
		// TODO Auto-generated method stub
		return permission;
	}

	public String tree(HttpServletRequest req, HttpServletResponse resp) {

		try {
			User cuser =(User) req.getSession().getAttribute("cuser");
			if(cuser==null) {
				return "toLogin";
			}
			int type=cuser.getType();
			List<RolePermission> rolePermissions = rolePermissionDao.finRolePermission(type);
			StringBuffer sb=new StringBuffer();
			for (RolePermission rp : rolePermissions) {
				sb.append(",").append(rp.getPid());
			}
			  List<TreeVo<Permission>> treePuls = permissionDao.treePuls(sb.substring(1));
			
			//List<TreeVo<Permission>>tree = permissionDao.tree(null, null);
			ResponseUtil.writeJson(resp, treePuls);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			try {
				ResponseUtil.writeJson(resp, "0");
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
		return null;
	}
}

3.配置xml文件

<action path="/permission" type="com.zking.web.PermissionAction">
		<forward name="toLogin" path="/login.jsp" redirect="true"></forward>
	</action>

4.前端代码

4.1.jsp界面

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>后管主界面</title>
    <link rel="stylesheet" type="text/css"
          href="${pageContext.request.contextPath}/static/js/easyui/themes/default/easyui.css">
    <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/static/js/easyui/themes/icon.css">
    <script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.js"></script>
    <script type="text/javascript"
            src="${pageContext.request.contextPath}/static/js/easyui/jquery.easyui.min.js"></script>
    <script src="${pageContext.request.contextPath}/static/js/main.js"></script>
</head>
<body class="easyui-layout">
<input type="text" name="ctx" id="ctx" value="${pageContext.request.contextPath}">
<div data-options="region:'north',border:false" style="height:60px;background:#B3DFDA;padding:10px">网上书城</div>
<div data-options="region:'west',split:true,title:'West'" style="width:150px;padding:10px;">
    <ul id="bookMenus"></ul>
</div>
<div data-options="region:'south',border:false" style="height:50px;background:#A9FACD;padding:10px;">底部版权</div>
<div data-options="region:'center',title:'Center'">
    <div id="bookTabs" class="easyui-tabs" style="width:100%;height:100%;">
        <div title="首页" style="padding:20px;display:none;">
            欢迎来到网上书城
        </div>
    </div>
</div>
</body>
</html>

4.2.配置js文件(main.js)

$(function() {
	$("#bookMenus").tree({
		url:$("#ctx").val()+"/permission.action?methodName=tree",
		onClick:function(node){
			var exists= $("#bookTabs").tabs('exists',node.text);
			if(exists){
				$("#bookTabs").tabs('select',node.text);
		}else{
			$('#bookTabs').tabs('add',{    
			    title:node.text,    
			    content:'<iframe width="100%" height="100%" src="'+$("#ctx").val()+node.attributes.self.url+'"></iframe>',    
			    closable:true,    
			    
			});  

		}
		}
	});
})

效果展示(商家):

 (买家):

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值