资讯专栏INFORMATION COLUMN

EasyUi项目《网上书城》之权限登陆,注册,左侧树形菜单

Aldous / 3440人阅读

摘要:思路如下在实体类创建类在层中写好相应的方法登录注册方法然后在子控制器中写好对应的方法。

前言:给大家讲解EasyUi项目《网上书城》

*权限:不同用户登录时,树形菜单会展示出不同的效果

码字不易,点个关注

转载请说明!

开发工具:eclipse,MySQL


 思维导图:


目录

1、目标

2、思路,代码以及效果展示

1、登录、注册

2、权限

2.1、user表中type中1为商家,2为买家

可以根据用户的type值来登录不同的界面

2.2、实现权限登录的思路

2.3、代码


1、目标

1、登录、注册

2、权限树形展示(不同用户登录时,树形菜单会展示出不同的效果)

2、思路,代码以及效果展示

1、登录、注册


登录、注册个人觉得比较简单。思路如下:

1、在实体类entity创建user类

2、在dao层中写好相应的方法(登录login、注册register方法)

3、然后在子控制器中写好对应的方法。

4、最后到配置文件中xml写好相应路径

user实体类

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 + "]";	}			}

dao层

    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);	}		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"});			}

子控制器层

public class UserAction extends ActionSupport implements ModelDriver {	private User user = new User();	private UserDao userDao = new UserDao();	public User getModel() {		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) {			e.printStackTrace();			return "toLogin";		}		//只要数据库中有这个用户就跳转到主界面		return "main";	}	public String register(HttpServletRequest req, HttpServletResponse resp) {		try {			userDao.add(user);			req.setAttribute("mag", "用户名密码错误");		} catch (Exception e) {			e.printStackTrace();			return "toRegister";		}		//如果注册成功,跳转到登录界面		return "toLogin";	}}

配置文件hpw.xml写好相应路径

			

2、权限

2.1、user表中type中1为商家,2为买家

可以根据用户的type值来登录不同的界面

2.2、实现权限登录的思路

1、将两个表关联起来,其中Permission中的id与pid行成主外键关系,RolePermission中的rid与      pid行成父子关系。

2、从两个表中得到type对应的菜单号将其显示

2.3、代码

PermissionDao:

public List 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 listPlus(String ids) throws Exception {		//ids="1,2,3,4,5,6,7,8,9,14";		String sql = "select * from t_easyui_Permission where id in ("+ids+")";		return super.executeQuery(sql, Permission.class, null);	}	public List> tree(Permission permission, PageBean pageBean) throws Exception {		List list = this.list(permission, pageBean);		List> listVo = new ArrayList>();		for (Permission p : list) {			TreeVo vo = new TreeVo<>();			vo.setId(p.getId() + "");			vo.setText(p.getName());			vo.setParentId(p.getPid() + "");			Map map = new HashMap();			map.put("self", p);			vo.setAttributes(map);			listVo.add(vo);		}		return BuildTree.buildList(listVo, "0");	}		public List> treePuls(String ids) throws Exception {		List list = this.listPlus(ids);		List> listVo = new ArrayList>();		for (Permission p : list) {			TreeVo vo = new TreeVo<>();			vo.setId(p.getId() + "");			vo.setText(p.getName());			vo.setParentId(p.getPid() + "");			Map map = new HashMap();			map.put("self", p);			vo.setAttributes(map);			listVo.add(vo);		}		return BuildTree.buildList(listVo, "0");	}

PermissionAction:

public class PermissionAction extends ActionSupport implements ModelDriver {	Permission permission = new Permission();	PermissionDao permissionDao = new PermissionDao();	UserDao userDao = new UserDao();	RolePermissionDao rolePermissionDao = new RolePermissionDao();		public Permission getModel() {		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 findRolePermissions = rolePermissionDao.findRolePermission(type);			StringBuffer sb = new StringBuffer();			for (RolePermission rp : findRolePermissions) {				sb.append(",").append(rp.getPid());			}			//ids="1,2,3,4,5,6,7,8,9,14";			//List listPlus = permissionDao.listPlus(sb.substring(1));			List> treePuls = permissionDao.treePuls(sb.substring(1));			//List> tree = permissionDao.tree(null, null);			ResponseUtil.writeJson(resp, treePuls);		} catch (Exception e) {			e.printStackTrace();			try {				ResponseUtil.writeJson(resp, "0");			}  catch (IOException e1) {				e1.printStackTrace();			}		}		return null;	}	}

 RolePermissionDao:

package com.zking.dao;import java.util.List;import com.hpw.entity.RolePermission;import com.hpw.util.BaseDao;public class RolePermissionDao extends BaseDao {	/**	 * 通过user表中的type字段进行查询,查询出商家/买家对应的菜单ID	 * @param type	 * @return	 * @throws Exception	 */	public List findRolePermission(int type) throws Exception {		String sql = "select * from t_easyui_role_Permission where rid = "+type;		return super.executeQuery(sql, RolePermission.class, null);	}	}

效果展示:

买家身份登陆

商家身份登陆

到这里就结束了,其中重点是权限登录,当中的逻辑比较复杂

欢迎大佬指点 

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/121063.html

相关文章

  • EasyUI项目门户书籍、类别查询、图片上传

    摘要:前言继续上一篇讲解项目网上书城之门户书籍类别查询图片上传码字不易,点个关注转载请说明开发工具,目录一目标一目标二具体思路以及代码,效果展示二具体思路以及代码,效果展示一显示菜单栏一显示菜单栏二点击左侧菜单栏,出现对应的书 前言:继续上一篇讲解EasyUi项目《网上书城》之门户书籍、类别查询、...

    OpenDigg 评论0 收藏0
  • 从0到1搭建element后台框架权限

    摘要:项目中按钮权限注册全局自定义指令来完成的。如果对自定义指令不熟的话可以查阅官方文档。相关文章链接从到搭建后台框架打包优化从到搭建后台框架优化篇 前言 首先还是谢谢各位童鞋的大大的赞赞,你们的支持是我前进的动力!上周写了一篇从0到1搭建element后台框架,很多童鞋留言提到权限问题,这一周就给大家补上。GitHub 一、jwt授权认证 现在大多数项目都是采用jwt授权认证,也就是我们所...

    NervosNetwork 评论0 收藏0
  • EasyUI项目购物车功能

    摘要:前言继续讲解项目网上书城之加入购物车,清空购物车功能码字不易,点个关注转载请说明开发工具,目录目标目标代码展示代码展示加入购物车加入购物车清空购物车清空购物车思维导图实现购物车的三种方式目标加入购物车,清空购物车代码展 前言:继续讲解EasyUi项目《网上书城》之加入购物车,清空购物车功能 ...

    PrototypeZ 评论0 收藏0
  • iview admin动态路由、权限控制个人见解

    摘要:目前是没有给我处理好权限这块的逻辑。我们的项目正常是路由是在本地配置好的一个路由文件,所以,要想实现动态的路由,我们就必须让实现动态生成。具体页面上的按钮权限的分配在前端页面是怎么控制的,完全可以去里借鉴。 iview admin目前是没有给我处理好权限这块的逻辑。所以,权限这块还是得我们自己去撸。(脸上笑嘻嘻、心里mmp!) 思路做权限,说到底就是为了让不同权限的用户, 可以访问不...

    LeanCloud 评论0 收藏0
  • 20180308_vue-router前端权限控制问题

    vue-router前端权限控制问题前提纲要:1.用户A和用户B有不同的权限。 页面分左侧菜单部分和右侧内容部分,右侧内容可能有不同路径的不同内容 最简单例子为点击左侧用户管理 右侧显示用户列表 点击某条内容详情 右侧显示某一用户的详细内容 2.用户A可以访问路径权限如下: a/list a/detail/:id a/list/:id 用户B可以访问路径权限如下: ...

    阿罗 评论0 收藏0

发表评论

0条评论

Aldous

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<