网上书城登录注册权限管理

前言

本章给大家分享网上书城项目的登录注册和权限管理功能实现

效果图展

登录界面效果
在这里插入图片描述
管理员登录进去的效果:
在这里插入图片描述
普通用户登录进去的效果:
在这里插入图片描述

功能实现步骤

1.先创建与数据库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;
 }
  
 public User() {
    
    
   
  }
 public User(long id, String name, String pwd, int type) {
    
    
  this.id = id;
  this.name = name;
  this.pwd = pwd;
  this.type = type;
 }
 @Override
 public String toString() {
    
    
  return "User [id=" + id + ", name=" + name + ", pwd=" + pwd + ", type=" + type + "]";
 }


}

2.创建dao类,里面一个登录方法及注册

public class UserDao extends BaseDao<User> {
    
    
 
 //登录
 public User login(User user) throws Exception {
    
    
  String name=user.getName();
  String pwd=user.getPwd();
  String sql="select * from t_easyui_user where true";
  if(StringUtils.isNotBlank(name)) {
    
    
   sql+=" and name ='%"+name+"%' ";
  }
  
  if(StringUtils.isNotBlank(pwd)) {
    
    
   sql+="and pwd= '%"+pwd+"%'";
  }
  List<User> list=super.executeQuery(sql, null, User.class);
  if(list.size()==0) {
    
    
   return null;
  }
  return list.get(0);
 }


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

3.action


public class UserAction extends ActionSupport implements ModelDriven<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 current = this.userdao.login(user);
   if(current!=null) {
    
    
    return "login";
   }
   req.getSession().setAttribute("currenUser",current);
  } catch (Exception e) {
    
    
   // TODO Auto-generated catch block
   e.printStackTrace();
   return "login";
  }
   return "mainTemp";
 }
public String add(HttpServletRequest req,HttpServletResponse resp) {
    
    
  try {
    
    
   this.userdao.add(user);
  } catch (Exception e) {
    
    
e.printStackTrace();
   return "register";
  }

return "login";
}
}

4.权限管理实体类

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;
 }
public RolePermission() {
    
    
  
 }

public RolePermission(long rid, long pid) {
    
    
   this.rid = rid;
  this.pid = pid;
 }

@Override
 public String toString() {
    
    
  return "RolePermission [rid=" + rid + ", pid=" + pid + "]";
 }

4.权限管理dao方法:

public class RolePermissionDao extends BaseDao<RolePermission>{
    
    
 /**
  * 获取角色对应的权限id
  * @param rid
  * @return
  * @throws Exception
  */
 public List<RolePermission> getpids(long rid) throws Exception{
    
    
  String sql="select * from t_easyui_role_permission where rid = "+rid;
  return super.executeQuery(sql, null, RolePermission.class);
 }
 }

5.权限action

public class PermissionAction extends ActionSupport implements ModelDriven<Permission> {
    
    
   private Permission permission=new Permission();
   private PermissionDao permissiondao=new PermissionDao();
   
  @Override
 public Permission getModel() {
    
    
  // TODO Auto-generated method stub
  return permission;
 }
 
 public String menuTree(HttpServletRequest req,HttpServletResponse resp) throws Exception {
    
    
  try {
    
    
   ResponseUtil.writeJson(resp, this.permissiondao.topNode(null, null));
  } catch (InstantiationException e) {
    
    
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IllegalAccessException e) {
    
    
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (SQLException e) {
    
    
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  return null;
 }
}

猜你喜欢

转载自blog.csdn.net/qq_45432593/article/details/107134441