尚筹网:登录功能优化

使用常量

不足

将类似 在这里插入图片描述之类的常量全部写到一起,集成管理

优化

1、常量声明

所在工程:atcrowdfunding-admin-3-common
全类名:com.atguigu.crowd.funding.util.CrowdFundingConstant
内容:

public class CrowdFundingConstant {
	
	public static final String ATTR_NAME_MESSAGE = "MESSAGE";
	public static final String ATTR_NAME_LOGIN_ADMIN = "LOGIN-ADMIN";
	
	public static final String MESSAGE_LOGIN_FAILED = "登录账号或密码不正确!请核对后再登录!";
	public static final String MESSAGE_CODE_INVALID = "明文不是有效字符串,请核对后再操作!";

}

2、使用:用上面的声明取代之前代码中的常量

配置异常映射机制

不足

如果我们不输入密码选择提交的话,服务器会得到一个空字符串,但是因为不能对空字符串加密,程序会抛出异常
在这里插入图片描述

优化

1、错误
作用:将捕获到的异常类型和具体页面对应起来。在捕获到对应类型的异常后,跳转到对应页面。

  • 配置:XML方式(只能返回页面,很难兼容Ajax请求)
  • 配置:Annotation方式(兼容两种请求)

所在工程:atcrowdfunding-admin-2-component
包:com.atguigu.crowd.funding.exeption

package com.atguigu.crowd.funding.exeption;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;

@ControllerAdvice
public class CrowdFundingExceptionResolever {

	@ExceptionHandler(value = Exception.class)
	public ModelAndView catchException(Exception ex) {
		ModelAndView  modelAndView= new ModelAndView();
		
		modelAndView.addObject("exception", ex);
		modelAndView.setViewName("system-error");
		return modelAndView;
	}
}

在 /atcrowdfunding-admin-1-webui/src/main/resources/spring-web-mvc.xml中加入扫描

<context:component-scan base-package="com.atguigu.crowd.funding.handler, com.atguigu.crowd.funding.exeption"/>

2、错误信息页面

/atcrowdfunding-admin-1-webui/src/main/webapp/WEB-INF/system-error.jsp内容:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="UTF-8">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="keys" content="">
<meta name="author" content="">
<title>❤汇聚点滴的力量,成就非凡的伟业❤</title>
<base href="http://${pageContext.request.serverName}:${pageContext.request.serverPort}${pageContext.request.contextPath}/"/>
<link rel="stylesheet" href="bootstrap/css/bootstrap.min.css">
<link rel="stylesheet" href="css/font-awesome.min.css">
<link rel="stylesheet" href="css/login.css">
<style>
</style>
</head>
<body>
	<nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
		<div class="container">
			<div class="navbar-header">
				<div>
					<a class="navbar-brand" href="index.html" style="font-size: 32px;">尚筹网-创意产品众筹平台</a>
				</div>
			</div>
		</div>
	</nav>

	<script type="text/javascript" src="jquery/jquery-2.1.1.min.js"></script>
	<script type="text/javascript">
		$(function(){
			$("#bakcBtn").click(function(){
				window.history.back();
			});
		});
	</script>
	<h1>${requestScope.exception.message }</h1>
	<button id="bakcBtn" class="btn btn-lg btn-success btn-block">后退</button>
</body>
</html>

退出登录状态

1、增加连接:在/atcrowdfunding-admin-1-webui/src/main/webapp/WEB-INF/admin-main.jsp中

在这里插入图片描述
2、编写代码:/atcrowdfunding-admin-2-component/src/main/java/com/atguigu/crowd/funding/handler/AdminHandler.java中

@RequestMapping("/admin/logout")
public String logout(HttpSession session) {
	
	session.invalidate();
	
	return "redirect:/index.html";
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值