登录失败弹出信息如何完成
SelfController
package com.imooc.sm.controller;
import com.imooc.sm.entity.Staff;
import com.imooc.sm.service.SelfService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@Controller("selfController")
public class SelfController {
@Autowired
private SelfService selfService;
/*用于识别是超级管理员、管理员、员工*/
private boolean flag;
private boolean superFlag;
// /toLogin.do
public void toLogin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
/*退出登录*/
flag = false;
superFlag = false;
request.getRequestDispatcher("login.jsp").forward(request, response);
}
// /login.do
public void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String account = request.getParameter("account");
String password = request.getParameter("password");
Staff staff = selfService.login(account, password);
if (staff == null) {
response.getWriter().print("<script>alert('您的输入有误')</script>");
response.sendRedirect("toLogin.do");
} else {
if ("管理员".equals(staff.getStatus())) {
flag = true;
}
if ("超级管理员".equals(staff.getStatus())) {
flag = true;
superFlag = true;
}
HttpSession session = request.getSession();
session.setAttribute("USER", staff);
session.setAttribute("flag", flag);
session.setAttribute("superFlag", superFlag);
response.sendRedirect("main.do");
// request.getRequestDispatcher(request.getContextPath()+"/main.do").forward(request,response);
}
}
// /logout.do 退出
public void logout(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
session.setAttribute("USER", null);
session.setAttribute("flag", null);
session.setAttribute("superFlag", null);
response.sendRedirect("toLogin.do");
}
// /main.do
public void main(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.getRequestDispatcher("index.jsp").forward(request, response);
}
// /self/info.do
public void info(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.getRequestDispatcher("../info.jsp").forward(request, response);
}
// /self/toChangePassword.do
public void toChangePassword(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.getRequestDispatcher("../change_password.jsp").forward(request, response);
}
// /self/changePassword.do
public void changePassword(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String password = request.getParameter("password");
String password1 = request.getParameter("password1");
HttpSession session = request.getSession();
Staff staff = (Staff) session.getAttribute("USER");
if (!staff.getPassword().equals(password)) {
response.sendRedirect("toChangePassword.do"); //相当于重新输入不过是清空信息了
} else {
selfService.changePassword(staff.getId(), password1);
//response.sendRedirect("../logout.do");
response.getWriter().print("<script type=\"text/javascript\">parent.location.href=\"../logout.do\"</script>");
}
}
}
login.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>登录</title>
<%
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() + "/";
%>
<link rel="stylesheet" type="text/css" href="<%=basePath%>css/reset.css"/>
<link rel="stylesheet" type="text/css" href="<%=basePath%>css/common.css"/>
<link rel="stylesheet" type="text/css" href="<%=basePath%>css/thems.css"/>
<script type="text/javascript" src="<%=basePath%>js/jquery-1.8.3.min.js"></script>
<!--框架高度设置-->
<script type="text/javascript">
$(function () {
//自适应屏幕宽度
window.onresize = function () {
location = location
};
var w_height = $(window).height();
$('.bg_img').css('height', w_height + 'px');
var bg_wz = 1920 - $(window).width();
$('.bg_img img').css('margin-left', '-' + bg_wz / 2 + 'px')
$('.language .lang').click(function () {
$(this).siblings('.lang_ctn').toggle();
});
})
</script>
<!--框架高度设置-->
</head>
<body>
<!--登录-->
<div class="login">
<div class="bg_img"><img src="<%=basePath%>images/login_bg.jpg"/></div>
<div class="logo">
<a href=""><img src="<%=basePath%>images/logo.png" alt=""/></a>
</div>
<div class="login_m">
<form action="login.do" method="post" name="login_form">
<ul>
<li class="wz">用户名</li>
<li><input name="account" type="text"></li>
<li class="wz">密码</li>
<li><input name="password" type="password"></li>
<li class="l_btn">
<a href="javascript:login_form.submit();">登录</a>
</li>
</ul>
</form>
</div>
</div>
<!--登录-->
</body>
</html>
SelfServiceImpl.java
package com.imooc.sm.service.impl;
import com.imooc.sm.dao.SelfDao;
import com.imooc.sm.dao.StaffDao;
import com.imooc.sm.entity.Staff;
import com.imooc.sm.service.SelfService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
@Service("selfService")
public class SelfServiceImpl implements SelfService {
@Autowired
@Qualifier("selfDao")
private SelfDao selfDao;
@Autowired()
@Qualifier("staffDao")
private StaffDao staffDao;
public Staff login(String sAccount, String sPassword) {
Staff staff = selfDao.selectByAccount(sAccount);
if (staff == null) {
return null;
} //如果差不到该账号则返回空,结束
if (staff.getPassword().equals(sPassword)) {
return staff;
}
return null;//如果出现其他问题也返回空
}
public void changePassword(Integer sId, String sPassword) {
Staff staff = staffDao.selectById(sId);
staff.setPassword(sPassword);
staffDao.update(staff);
}
}
SelfDao.xml
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.4//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.imooc.sm.dao.SelfDao">
<select id="selectByAccount" parameterType="String" resultMap="com.imooc.sm.dao.StaffDao.resultMap">
select * from staff where account=#{account}
</select>
</mapper>
正在回答 回答被采纳积分+1
- 参与学习 人
- 提交作业 205 份
- 解答问题 4317 个
Java中非常实用的SSM整合开发内容,从Spring开始,到MyBaits的进阶内容,再到SpringMVC的应用,最后是SSM整合开发案例,逐步深入,助你成长为一名Java工程师!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星