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

Posted 九阳神功张无忌

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了EasyUi项目《网上书城》之权限登陆,注册,左侧树形菜单相关的知识,希望对你有一定的参考价值。

前言:给大家讲解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<User> {

	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写好相应路径

<action type="com.hpw.web.UserAction" path="/user">
	<forward path="/bg/mainTemp.jsp" redirect="false" name="main"/>
	<forward path="/login.jsp" redirect="true" name="toLogin"/>
	<forward path="/register.jsp" redirect="false" name="toRegister"/>
</action>

2、权限

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

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

2.2、实现权限登录的思路

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

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

2.3、代码

PermissionDao:

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 {
		//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<TreeVo<Permission>> tree(Permission permission, PageBean pageBean) throws Exception {
		List<Permission> list = this.list(permission, pageBean);
		List<TreeVo<Permission>> listVo = new ArrayList<TreeVo<Permission>>();
		for (Permission p : list) {
			TreeVo<Permission> vo = new TreeVo<>();
			vo.setId(p.getId() + "");
			vo.setText(p.getName());
			vo.setParentId(p.getPid() + "");
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("self", p);
			vo.setAttributes(map);
			listVo.add(vo);
		}
		return BuildTree.buildList(listVo, "0");
	}
	
	public List<TreeVo<Permission>> treePuls(String ids) throws Exception {
		List<Permission> list = this.listPlus(ids);
		List<TreeVo<Permission>> listVo = new ArrayList<TreeVo<Permission>>();
		for (Permission p : list) {
			TreeVo<Permission> vo = new TreeVo<>();
			vo.setId(p.getId() + "");
			vo.setText(p.getName());
			vo.setParentId(p.getPid() + "");
			Map<String, Object> map = new HashMap<String, Object>();
			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 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<RolePermission> 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<Permission> listPlus = permissionDao.listPlus(sb.substring(1));
			List<TreeVo<Permission>> treePuls = permissionDao.treePuls(sb.substring(1));
			//List<TreeVo<Permission>> 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<RolePermission> {

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

效果展示:

买家身份登陆

商家身份登陆

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

欢迎大佬指点 

以上是关于EasyUi项目《网上书城》之权限登陆,注册,左侧树形菜单的主要内容,如果未能解决你的问题,请参考以下文章

spring项目篇13 --- 网上商城之登陆注册

easyui左侧导航菜单右侧载入百度地图项目框架

2-crm项目-kingadmin

SSH网上商城项目实战04EasyUI菜单的实现

EasyUI--根据用户权限显示左侧菜单数据库表设计

EasyUI项目之新书上架热门书籍