web day24 小项目练习图书商城, 用户,模块(注册,激活,登陆,退出),分类/图书模块

时间:2022-11-19 00:14:42

图书商城

 

模块

 

用户模块

分类模块

图书模块

购物车模块

订单模块

 

功能分析

 

前台

  用户模块:注册/激活/登陆/退出

  分类模块:查看所有分类

  图书模块:查询所有图书/按分类查询图书/查询图书详细(id)

  购物车模块:添加/清空/删除购物车条目 /我的购物车(按用户查询)

  订单模块:生成订单/我的订单(按用户查询)/按id查询订单/确认收货/

              /付款功能(只跳转到银行页面)/付款回调功能

 

后台

  管理员:登陆

  分类管理:增/删/改/查

  图书管理(我的):增(上传图片)/删/改/查

  订单模块:查询所有订单/按状态查询/发货

 

 

表(数据库)

 web day24 小项目练习图书商城, 用户,模块(注册,激活,登陆,退出),分类/图书模块


 

用户模块

 

1注册

web day24 小项目练习图书商城, 用户,模块(注册,激活,登陆,退出),分类/图书模块

 

2激活

web day24 小项目练习图书商城, 用户,模块(注册,激活,登陆,退出),分类/图书模块

 

3登陆

web day24 小项目练习图书商城, 用户,模块(注册,激活,登陆,退出),分类/图书模块

 

4退出

把session销毁/

 

代码

web层

/**
 * User表述层
 */
public class UserServlet extends BaseServlet {
	private UserService userService = new UserService();
	
	/**
	 * 退出功能
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String quit(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.getSession().invalidate();
		return "r:/index.jsp";
	}
	
	public String login(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		/*
		 * 1. 封装表单数据到form中
		 * 2. 输入校验(不写了)
		 * 3. 调用service完成激活
		 *   > 保存错误信息、form到request,转发到login.jsp
		 * 4. 保存用户信息到session中,然后重定向到index.jsp
		 */
		User form = CommonUtils.toBean(request.getParameterMap(), User.class);
		try {
			User user = userService.login(form);
			request.getSession().setAttribute("session_user", user);
			return "r:/index.jsp";
		} catch (UserException e) {
			request.setAttribute("msg", e.getMessage());
			request.setAttribute("form", form);
			return "f:/jsps/user/login.jsp";
		}
	}
	
	/**
	 * 激活功能
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String active(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		/*
		 * 1. 获取参数激活码
		 * 2. 调用service方法完成激活
		 *   > 保存异常信息到request域,转发到msg.jsp
		 * 3. 保存成功信息到request域,转发到msg.jsp
		 */
		String code = request.getParameter("code");
		try {
			userService.active(code);
			request.setAttribute("msg", "恭喜,您激活成功了!请马上登录!");
		} catch (UserException e) {
			request.setAttribute("msg", e.getMessage());
		}
		return "f:/jsps/msg.jsp";
	}
	
	/**
	 * 注册功能
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String regist(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		/*
		 * 1. 封装表单数据到form对象中
		 * 2. 补全:uid、code
		 * 3. 输入校验
		 *   > 保存错误信息、form到request域,转发到regist.jsp
		 * 4. 调用service方法完成注册
		 *   > 保存错误信息、form到request域,转发到regist.jsp
		 * 5. 发邮件
		 * 6. 保存成功信息转发到msg.jsp
		 */
		// 封装表单数据
		User form = CommonUtils.toBean(request.getParameterMap(), User.class);
		// 补全
		form.setUid(CommonUtils.uuid());
		form.setCode(CommonUtils.uuid() + CommonUtils.uuid());
		/*
		 * 输入校验
		 * 1. 创建一个Map,用来封装错误信息,其中key为表单字段名称,值为错误信息
		 */
		Map<String,String> errors = new HashMap<String,String>();
		/*
		 * 2. 获取form中的username、password、email进行校验
		 */
		String username = form.getUsername();
		if(username == null || username.trim().isEmpty()) {
			errors.put("username", "用户名不能为空!");
		} else if(username.length() < 3 || username.length() > 10) {
			errors.put("username", "用户名长度必须在3~10之间!");
		}
		
		String password = form.getPassword();
		if(password == null || password.trim().isEmpty()) {
			errors.put("password", "密码不能为空!");
		} else if(password.length() < 3 || password.length() > 10) {
			errors.put("password", "密码长度必须在3~10之间!");
		}
		
		String email = form.getEmail();
		if(email == null || email.trim().isEmpty()) {
			errors.put("email", "Email不能为空!");
		} else if(!email.matches("\\w+@\\w+\\.\\w+")) {
			errors.put("email", "Email格式错误!");
		}
		/*
		 * 3. 判断是否存在错误信息
		 */
		if(errors.size() > 0) {
			// 1. 保存错误信息
			// 2. 保存表单数据
			// 3. 转发到regist.jsp
			request.setAttribute("errors", errors);
			request.setAttribute("form", form);
			return "f:/jsps/user/regist.jsp";
		}
		
		/*
		 * 调用service的regist()方法
		 */
		try {
			userService.regist(form);
		} catch (UserException e) {
			/*
			 * 1. 保存异常信息
			 * 2. 保存form
			 * 3. 转发到regist.jsp
			 */
			request.setAttribute("msg", e.getMessage());
			request.setAttribute("form", form);
			return "f:/jsps/user/regist.jsp";
		}
		
		/*
		 * 发邮件
		 * 准备配置文件!
		 */
		// 获取配置文件内容
		Properties props = new Properties();
		props.load(this.getClass().getClassLoader()
				.getResourceAsStream("email_template.properties"));
		String host = props.getProperty("host");//获取服务器主机
		String uname = props.getProperty("uname");//获取用户名
		String pwd = props.getProperty("pwd");//获取密码
		String from = props.getProperty("from");//获取发件人
		String to = form.getEmail();//获取收件人
		String subject = props.getProperty("subject");//获取主题
		String content = props.getProperty("content");//获取邮件内容
		content = MessageFormat.format(content, form.getCode());//替换{0}
		
		Session session = MailUtils.createSession(host, uname, pwd);//得到session
		Mail mail = new Mail(from, to, subject, content);//创建邮件对象
		try {
			MailUtils.send(session, mail);//发邮件!
		} catch (MessagingException e) {
		}
		
		
		/*
		 * 1. 保存成功信息
		 * 2. 转发到msg.jsp
		 */
		request.setAttribute("msg", "恭喜,注册成功!请马上到邮箱激活");
		return "f:/jsps/msg.jsp";
	}
}

激活service

	/**
	 * 激活功能
	 * @throws UserException 
	 */
	public void active(String code) throws UserException {
		/*
		 * 1. 使用code查询数据库,得到user
		 */
		User user = userDao.findByCode(code);
		/*
		 * 2. 如果user不存在,说明激活码错误
		 */
		if(user == null) throw new UserException("激活码无效!");
		/*
		 * 3. 校验用户的状态是否为未激活状态,如果已激活,说明是二次激活,抛出异常
		 */
		if(user.isState()) throw new UserException("您已经激活过了,不要再激活了,除非你想死!");
		
		/*
		 * 4. 修改用户的状态
		 */
		userDao.updateState(user.getUid(), true);
	}



分类模块

 

查询所有分类:main.jsp→CategoryService#findAll()→left.jsp

 

图书模块

 

查询所有图书

web day24 小项目练习图书商城, 用户,模块(注册,激活,登陆,退出),分类/图书模块

 

按分类查询

web day24 小项目练习图书商城, 用户,模块(注册,激活,登陆,退出),分类/图书模块

 

查询详细信息(加载)

web day24 小项目练习图书商城, 用户,模块(注册,激活,登陆,退出),分类/图书模块