网上书城项目的注册、登录功能及按照权限登录功能(项目进度二)

转载请标明出处:https://blog.csdn.net/men_ma/article/details/106847165.
本文出自 不怕报错 就怕不报错的小猿猿 的博客

前言

博主前面的博客中有关于网上书城这个项目的博客,今天我们接着上一篇的网上书城前端页面编写框架那篇博客继续,链接:网上书城项目的需求分析、数据库表设计及前端界面的编写.
现在我们来写登录注册功能,这是一个网站的开始,必不可少的部分,今天的登录功能要判断权限问题,为方便管理整个网上书城商城,权限则分为老板和门户(顾客),老板能够操作整个项目的功能,则用户只能操作自己的订单管理,所以这需要注意的一个梗,首先我们先来分析一下注册及登录的流程,博主以思维导图的方式讲解

1.实现目标效果

1.1 注册功能

1.2 登录功能(按权限区分)

在这里插入图片描述
去数据库表验证是否注册成功:
在这里插入图片描述

1.2.1 老板登录看到的菜单效果

在这里插入图片描述

1.2.2 门户(顾客)登录看到的菜单效果

在这里插入图片描述

2.实现登录的编码逻辑思路(还未分权限)

在这里插入图片描述

3.实现注册的编码逻辑思路

在这里插入图片描述

4.实现权限登录的逻辑思路

在这里插入图片描述

5.普通的登录注册代码实现

5.1 User实体类

package com.xiaoqing.entity;

public class User {
		private long id;//id,标识列
		private String name;//用户名:唯一键,登陆时使用
		private String pwd;//密码
		private int type;//用户类型:1 管理员 2 普通用户     注:管理员是直接添加到数据库的,注册时只能注册普通用户 
		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(long id, String name, String pwd, int type) {
			super();
			this.id = id;
			this.name = name;
			this.pwd = pwd;
			this.type = type;
		}
		public User() {
			
		}
		@Override
		public String toString() {
			return "User [id=" + id + ", name=" + name + ", pwd=" + pwd + ", type=" + type + "]";
		}
		
}

5.2 UserDao

package com.xiaoqing.dao;

import java.sql.SQLException;
import java.util.List;

import javax.jws.soap.SOAPBinding.Use;

import com.xiaoqing.entity.Book;
import com.xiaoqing.entity.User;
import com.xiaoqing.util.BaseDao;
import com.xiaoqing.util.PageBean;
import com.xiaoqing.util.StringUtils;

public class UserDao extends BaseDao<User>{

	/**
	 * 注册:增加方法
	 * @return
	 * @throws SQLException 
	 * @throws IllegalAccessException 
	 * @throws IllegalArgumentException 
	 * @throws SecurityException 
	 * @throws NoSuchFieldException 
	 */
	public int addUser(User user) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, SQLException {
		String sql="insert into t_easyui_user(name,pwd) values(?,?)";
		return super.executeUpdate(sql, user, new String[] {"name","pwd"});
	}
	/**
	 * 登录:查询方法
	 * @param user
	 * @param pageBean
	 * @return
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 * @throws SQLException
	 */
	public List<User> login(User user,PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException{
		String sql="select * from t_easyui_user where true";
		if(StringUtils.isNotBlank(user.getName())) {
			sql+=" and name='"+user.getName()+"'";
		}
		if(StringUtils.isNotBlank(user.getPwd())) {
			sql+=" and pwd='"+user.getPwd()+"'";
		}
		return  super.executeQuery(sql, User.class,null);
	}
}

5.3 UserAction处理业务逻辑类

package com.xiaoqing.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;

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

import com.xiaoqing.dao.UserDao;
import com.xiaoqing.entity.Book;
import com.xiaoqing.entity.User;
import com.xiaoqing.frameWork.ActionSupport;
import com.xiaoqing.frameWork.ModelDriver;
import com.xiaoqing.util.PageBean;

public class UserAction extends ActionSupport implements ModelDriver<User>{
	
	private UserDao ud=new UserDao();
	private User user=new User();
	
	@Override
	public User getModel() {
		// TODO Auto-generated method stub
		return user;
	}
	
	public String login(HttpServletRequest req,HttpServletResponse resp) {
			try {
				 User CustomerUser = this.ud.login(user,null).get(0);
				if(CustomerUser==null) {
					PrintWriter out = resp.getWriter();
					out.print(11);
					return "login";
				}			
				req.getSession().setAttribute("CustomerUser", CustomerUser);
			} catch (InstantiationException e) {
				e.printStackTrace();
				return "login";
			} catch (IllegalAccessException e) {
				e.printStackTrace();
				return "login";
			} catch (SQLException e) {
				e.printStackTrace();
				return "login";
			} catch (IOException e) {
				e.printStackTrace();
			} 
		return "index";
	}
	
	public String register(HttpServletRequest req,HttpServletResponse resp) {
			try {
				this.ud.addUser(user);
			} catch (NoSuchFieldException e) {
				e.printStackTrace();
				return "register";
			} catch (SecurityException e) {
				e.printStackTrace();
				return "register";
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
				return "register";
			} catch (IllegalAccessException e) {
				e.printStackTrace();
				return "register";
			} catch (SQLException e) {
				e.printStackTrace();
				return "register";
			}
		return "login";
	}
}

5.4 mvc.xml:配置结果码跳转相应的界面

<?xml version="1.0" encoding="UTF-8"?>
<config>
	<!--permission:菜单及子菜单(左边类似树叶概念)  -->
	 <action path="/permission" type="com.xiaoqing.web.PermissionAction">
		<!-- <forward name="index" path="/index.jsp" redirect="false" /> -->
	</action>   
	<!--menu:菜单  -->
	<action path="/menu" type="com.xiaoqing.web.MenuAction">
		<!-- <forward name="menu" path="/index.jsp" redirect="false" /> -->
	</action>
	<!--book:书籍  -->
	<action path="/book" type="com.xiaoqing.web.BookAction">
		<!-- <forward name="menu" path="/index.jsp" redirect="false" /> -->
	</action>
	<!--user:用户  -->
	<action path="/user" type="com.xiaoqing.web.UserAction">
	<!--false:如果需要提示密码错误这些就用false -->
		<forward name="login" path="/login.jsp" redirect="false" />
		<forward name="index" path="/index.jsp" redirect="false" />
		<forward name="mainPag" path="/mainPag.jsp" redirect="false" />
		<forward name="register" path="/register.jsp" redirect="false" />
	</action>
	
	<action path="/version" type="com.xiaoqing.web.User_Version2Action">
		<!-- <forward name="menu" path="/index.jsp" redirect="false" /> -->
	</action>
	
	<!--category:书籍分类(下拉列表)  -->
	<action path="/category" type="com.xiaoqing.web.CategoryAction">
		<!-- <forward name="menu" path="/index.jsp" redirect="false" /> -->
	</action>
	
</config>

5.5 login.jsp登录界面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<!--全局样式  -->
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath }/static/js/jquery-easyui-1.5.1/themes/default/easyui.css">   
<!--定义图标  -->
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath }/static/js/jquery-easyui-1.5.1/themes/icon.css">   

<!--导入js  -->
<script type="text/javascript" src="${pageContext.request.contextPath }/static/js/jquery-easyui-1.5.1/jquery.min.js"></script>   
<!--组件库源码的js文件  -->
<script type="text/javascript" src="${pageContext.request.contextPath }/static/js/jquery-easyui-1.5.1/jquery.easyui.min.js"></script>  
<script type="text/javascript" src="${pageContext.request.contextPath }/static/js/user.js"></script>  
<!-- <link rel="stylesheet" type="text/css" href="css/bootstrap.css" /> -->
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css" />
<link rel="stylesheet" type="text/css" href="css/fg.css" />
<!--导入js  -->
<script type="text/javascript" src="${pageContext.request.contextPath }/static/js/jquery-easyui-1.5.1/jquery.min.js"></script>   
<title>网上书城登录</title>
<style>
			.form-signin{
				width: 400px;
				/* 相对定位 :relative    绝对定位:absolute*/
				position: relative;
				top: 100px;
				left: 50%;
				margin-left: -200px;
			}
</style>
</head>
<!--隐藏域:项目名  -->
<%-- <input type="hidden" id="ctx" value="${pageContext.request.contextPath }">  --%>
	<body>
		<form class="form-signin" action="${pageContext.request.contextPath }/user.action?methodName=login" method="post">
			<div class="text-center mb-4">
				<h1 class="h3 mb-3 font-weight-normal">用户登录</h1>
			</div>
			<div class="form-label-group">
				<input type="text" id="name" name="name" class="form-control" placeholder="请输入用户名" required autofocus>
			</div>
			<div class="form-label-group">
				<input type="password" id="pwd" name="pwd" class="form-control" placeholder="请输入密码" required>
			</div>
			<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">登录</button>
			<p class="mt-5 mb-3 text-muted text-center">&copy; 2017-2020</p>
		</form>
		<!-- <script src="css/bootstrap.js" type="text/javascript" charset="utf-8"></script> -->
	</body>
</html>

5.6 register注册界面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<!-- <link rel="stylesheet" type="text/css" href="css/bootstrap.css" /> -->
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css" />
<link rel="stylesheet" type="text/css" href="css/fg.css" />
<!--导入js  -->
<script type="text/javascript" src="${pageContext.request.contextPath }/static/js/jquery-easyui-1.5.1/jquery.min.js"></script>   
		<style>
			.form-signin{
				width: 400px;
				/* 相对定位 :relative    绝对定位:absolute*/
				position: relative;
				top: 100px;
				left: 50%;
				margin-left: -200px;
			}
    </style>
</head>
<body>
		<form class="form-signin" action="${pageContext.request.contextPath }/user.action?methodName=register" method="post">
			<div class="text-center mb-4">
				<h1 class="h3 mb-3 font-weight-normal">用户注册</h1>
			</div>

			<div class="form-label-group">
				<input type="text" id="name" name="name" class="form-control" placeholder="请输入用户名" required autofocus>
			</div>

			<div class="form-label-group">
				<input type="password" id="pwd" name="pwd" class="form-control" placeholder="请输入密码" required>
			</div>

			<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">注册</button>
			<p class="mt-5 mb-3 text-muted text-center">&copy; 2017-2020</p>
		</form>
	</body>
</html>

6.按权限登录看到不同的菜单栏

6.1 权限中间表建立Role_Permission实体类

package com.xiaoqing.entity;

public class Role_Permission {
	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 Role_Permission(long rid, long pid) {
		super();
		this.rid = rid;
		this.pid = pid;
	}
	public Role_Permission() {
		super();
	}
	@Override
	public String toString() {
		return "Role_Permission [rid=" + rid + ", pid=" + pid + "]";
	}
	
	
}

6.2 写Role_PermissionDao类:根据rid(角色编号)进行查询

package com.xiaoqing.dao;

import java.sql.SQLException;
import java.util.List;

import com.xiaoqing.entity.Role_Permission;
import com.xiaoqing.util.BaseDao;
import com.xiaoqing.util.PageBean;

public class Role_PermissionDao extends BaseDao<Role_Permission>{

	public List<Role_Permission> list(Role_Permission role_Permission,PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException{
		String sql="select * from t_easyui_role_permission where true";
		long rid = role_Permission.getRid();
		if(rid!=0) {
			sql+=" and rid="+rid;
		}
		return super.executeQuery(sql, Role_Permission.class, pageBean);
	}
	
}

6.3 修改PermissionDao(根据pids(各菜单项的编号根据他查询不同的角色看到不同的菜单栏)进行查询):在原方法的基础上重载一个方法,参数不同

package com.xiaoqing.dao;

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

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.xiaoqing.entity.Permission;
import com.xiaoqing.util.BaseDao;
import com.xiaoqing.util.BuildTree;
import com.xiaoqing.util.PageBean;
import com.xiaoqing.vo.TreeVo;

public class PermissionDao extends BaseDao<Permission> {

	/**
	 * 直接从数据库获取到的数据
	 * @param permission
	 * @param pageBean
	 * @return
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 * @throws SQLException
	 */
	public List<Permission> list(Permission permission,PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException{
		String sql="select * from t_easyui_Permission ";
		return super.executeQuery(sql, Permission.class, pageBean);
	}
	
	//重载list方法
	public List<Permission> listPermission(String pids,PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException{
		String sql="select * from t_easyui_Permission where id in ("+pids+")";
		return super.executeQuery(sql, Permission.class, pageBean);
	}

	
//	方法二:代码少
//	得到数据节点,能够将数据库中的数据,体现父子结构
	public List<TreeVo<Permission>> topNode(Permission permission,PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException{
		List<Permission> list = this.list(permission, pageBean);
		List<TreeVo<Permission>> nodes=new ArrayList<TreeVo<Permission>>();
		TreeVo treeVo=null;
		for (Permission p : list) {
			treeVo=new TreeVo<>();
			treeVo.setId(p.getId()+"");
			treeVo.setText(p.getName());
			treeVo.setParentId(p.getPid()+"");
			Map<String, Object> attributes=new HashMap<String, Object>();
			attributes.put("self", p);
			treeVo.setAttributes(attributes);
			nodes.add(treeVo);
		}
//		0 :可以指定哪个节点为父节点,选择自己想要的菜单
		return  BuildTree.buildList(nodes,"0");
	}
	
	
	public List<TreeVo<Permission>> topNodePermission(String pids,PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException{
		List<Permission> list = this.listPermission(pids, pageBean);
		List<TreeVo<Permission>> nodes=new ArrayList<TreeVo<Permission>>();
		TreeVo treeVo=null;
		for (Permission p : list) {
			treeVo=new TreeVo<>();
			treeVo.setId(p.getId()+"");
			treeVo.setText(p.getName());
			treeVo.setParentId(p.getPid()+"");
			Map<String, Object> attributes=new HashMap<String, Object>();
			attributes.put("self", p);
			treeVo.setAttributes(attributes);
			nodes.add(treeVo);
		}
//		0 :可以指定哪个节点为父节点,选择自己想要的菜单
		return  BuildTree.buildList(nodes,"0");
	}
}

6.4 修改PermissionAction处理业务逻辑类(管理树形菜单)

package com.xiaoqing.web;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

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

import com.xiaoqing.dao.PermissionDao;
import com.xiaoqing.dao.Role_PermissionDao;
import com.xiaoqing.entity.Permission;
import com.xiaoqing.entity.Role_Permission;
import com.xiaoqing.entity.User;
import com.xiaoqing.frameWork.ActionSupport;
import com.xiaoqing.frameWork.DispatcherServlet;
import com.xiaoqing.frameWork.ModelDriver;
import com.xiaoqing.util.ResponseUtil;
import com.xiaoqing.vo.TreeVo;

public class PermissionAction extends ActionSupport implements ModelDriver<Permission>{

	private PermissionDao pd=new PermissionDao();
	private Permission permission=new Permission();
	private Role_PermissionDao rpd=new Role_PermissionDao();
	
	@Override
	public Permission getModel() {
		return permission;
	}

	public String menuTree(HttpServletRequest req,HttpServletResponse resp) {
		try {
//			TreeVo<Permission> topNode = this.pd.topNode(null, null);
//			放到一个容器里面   json格式数据
//			List<TreeVo<Permission>> list=new ArrayList<TreeVo<Permission>>();
//			list.add(topNode);
			
//			不管老板还是消费者都是看到所有的菜单
//			老板看老板的
//			消费者看消费者
//			获取当前用户
			User CustomerUser = (User)req.getSession().getAttribute("CustomerUser");
			Role_Permission rp=new Role_Permission();
			rp.setRid(CustomerUser.getType());
			List<Role_Permission> role_Permissions = this.rpd.list(rp, null);
//			,0,1,2,3,4,5......
			StringBuilder pids=new StringBuilder();
			for (Role_Permission r : role_Permissions) {
				pids.append(",").append(r.getPid());
			}
			List<TreeVo<Permission>> topNodePermission = this.pd.topNodePermission(pids.substring(1), null);
//			ResponseUtil.writeJson(resp, this.pd.topNode(null, null));
			ResponseUtil.writeJson(resp,topNodePermission);
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
//		结果码的配置就是为了,在mvc.xml中寻找到底是重定向还是转发
		return null;
	}
	
}

总结

博主下篇博客会发布后台的未上架、已上架、已下架功能,敬请期待!!!
有些功能已完成,一篇博客不能写太多,所以博主分批次写,不然很难消化
Thanks♪(・ω・)ノ

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值