1、今日完成任务
(1)完成项目业务员登录功能的代码书写及测试工作
2、核心源码:
@RequestMapping(value="/login1")
@ResponseBody
public String login(String username,String password,String inputaccount,HttpSession session){
try {
String key = (String) session.getAttribute("key");
if(key.equals(inputaccount)){
Clerk c = cs.queryOne(username,password);
session.setAttribute("user", c);
if(c!=null){
return "success";
}else{
return "loginError";
}
}else{
return "accountError";
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
@RequestMapping("/getName")
@ResponseBody
public Clerk getName(HttpSession session){
Clerk c = (Clerk) session.getAttribute("user");
if(c!=null){
return c;
}else{
return null;
}
}
@RequestMapping("/logout")
public String logout(HttpSession session){
session.removeAttribute("user");
return "login";
}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,innitial-scale=1">
<title>车辆派遣管理系统登录页面</title>
<script src="./js/jquery-1.8.3.js" type="text/javascript"></script>
<link rel="stylesheet" type="text/css" href="css/bootstrap.css">
<link rel="stylesheet" type="text/css" href="css/login.css">
<script type="text/javascript" src="js/jquery-1.8.3.js"></script>
<script>
$(function(){
$("#login").click(function(){
$.post("./login1",$('#loginform').serialize(),function(data){
if(data=="loginError"){
alert("用户名或密码有误!");
}else if(data=="accountError"){
alert("验证码有误!");
}else{
window.location.href = "index.html";
}
});
});
})
</script>
</head>
<body>
<div class="loginbox">
<div class="loginnav">
<nav class="navbar navbar-default">
<div class="container">
<div class="navbar-header">
<a class="navbar-brand" href="#"><img src="image/logo.png" height="50px" width="50px"></a>
<span class="logintitle">车辆派遣管理系统</span>
</div>
</div>
</nav>
</div>
<section class="mainlogin">
<div class="container">
<div class="col-md-4 col-md-offset-4 logincontent">
<h4>员工登录</h4>
<form class="form-horizontal" id="loginform" name="loginform"
method="post" action="/login">
<div class="form-group" id="idInputLine">
<label for="inputPassword3" class="col-sm-3 control-label">账号</label>
<div class="col-sm-8">
<input id="loginform:idInput" type="text" name="username" class="form-control" placeholder="请输入手机号/邮箱/用户名">
</div>
</div>
<div class="form-group" id="pwdInputLine">
<label id="loginform:pwdInput" class="col-sm-3 control-label">密码</label>
<div class="col-sm-8">
<input for="pwdInput" type="password" name="password" class="form-control" placeholder="请输入您的密码">
</div>
</div>
<div class="form-group">
<label for="inputvalidate" class="col-sm-3 control-label">验证码</label>
<div class="col-sm-4">
<input type="text" class="form-control" name="inputaccount" id="inputaccount" placeholder="请输入验证码">
</div>
<div class="col-sm-4">
<img id="loginform:vCode" src="validatecode.jsp" onclick="javascript:document.getElementById('loginform:vCode'). src='validatecode.jsp?'+Math.random();" />
</div>
</div>
<div class="col-md-offset-3 col-md-8">
<a href="javascript:void(0);" id="login" name="loginform:j_id19"
class="btn btn-danger" target="_blank">立即登录</a>
</div>
</form>
</div>
</div>
</section>
<footer class="clearfix">
<div class="container">
<p class="text-center">京公网安备 11000002000088号 | 京ICP证070359号 | 互联网信息服务资格证编号(京)-经营性-2014-0008 | 新出发京零 字第大120007号</p>
<p class="text-center">互联网出版许可证编号新出网证(京)字150号 | 出版物经营许可证 | 网络文化经营许可证京网文[2014]2148-348号 | 违法和不良信息举报电话:4006561155</p>
<p class="text-center">Copyright © 2020 - 2030 王东东@ 版权所有 | 消费者维权热线:4006067733经营证照 | (京)网械平台备字(2018)第00003号 | 营业执照</p>
</div>
</footer>
</div>
</body>
</html>
3、遇到的问题:
(1)登录成功后需要在HTML页面显示用户名
4、解决的方法:
(1)登录成功后跳转到首页发送ajax请求返回用户名
@RequestMapping("/getName")
@ResponseBody
public Clerk getName(HttpSession session){
Clerk c = (Clerk) session.getAttribute("user");
if(c!=null){
return c;
}else{
return null;
}
}
var name = "";
$.post("./getName",function(data){
if(data!=null){
name = data.name;
$("#name").html(name);
}else{
window.location.href = "login.html";
}
},"json");
5、项目燃尽图更新: