使用常量
不足
将类似 之类的常量全部写到一起,集成管理
优化
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";
}