登陆点击后只会显示用户名不存在,不填或填写正确都不会有对应显示
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" href="/resources/layui/css/layui.css">
<style>
body{
background-color:#f2f2f2 ;
}
.oa-container{
position:absolute;
width: 400px;
height: 350px;
top: 50%;
left: 50%;
padding: 20px;
margin-left: -200px;
margin-top: -175px;
}
#username,#password{
text-align: center;
font-size:24px;
}
</style>
</head>
<body>
<div class="oa-container">
<h1 style="text-align: center;margin-bottom:20px">OA系统</h1>
<form class="layui-from">
<div class="layui-form-item">
<input type="text" id="username" lay-verify="required" name="username" placeholder="请输入用户名" autocomplete="off" class="layui-input">
</div>
<div class="layui-form-item">
<input type="password" id="password" lay-verify="required" name="password" placeholder="请输入密码" autocomplete="off" class="layui-input">
</div>
<div class="layui-form-item">
<button class="layui-btn layui-btn-fluid" lay-submit lay-filter="login">登录</button>
</div>
</form>
</div>
<script src="/resources/layui/layui.all.js"></script>
<script>
layui.form.on("submit(login)",function(formdata){
console.log(formdata);
layui.$.ajax({
url:"/check_login",
data: formdata.field,
type:"post",
dataType:"json",
success:function(json){
console.log(json);
if(json.code=="0"){
layui.layer.msg("登陆成功!");
}else{
layui.layer.msg(json.message);
}
}
})
return false;
})
</script>
</body>
</html>
mybatisutil
package com.wjb.oa.utils;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.Reader;
import java.util.function.Function;
public class MybatisUtils {
private static SqlSessionFactory sqlSessionFactory=null;
static {
Reader reader=null;
try{
reader= Resources.getResourceAsReader("mybatis-config.xml");
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
}catch(Exception e){
throw new ExceptionInInitializerError(e);
}
}
public static Object excuteQuery(Function<SqlSession, Object> func){
SqlSession sqlSession=sqlSessionFactory.openSession();
try{
Object obj=func.apply(sqlSession);
return obj;
}
finally {
sqlSession.close();
}
}
public static Object excuteUpdate(Function<SqlSession,Object> func){
SqlSession sqlSession=sqlSessionFactory.openSession(false);
try{
Object obj=func.apply(sqlSession);
sqlSession.commit();
return obj;
}catch (RuntimeException e){
sqlSession.rollback();
throw e;
}finally {
sqlSession.close();
}
}
}
dao
package com.wjb.oa.dao;
import com.wjb.oa.entity.User;
import com.wjb.oa.utils.MybatisUtils;
public class UserDao {
public User selectByUsername(String username){
User user=(User) MybatisUtils.excuteQuery(sqlSession -> sqlSession.selectOne("usermapper.selectByUsername", username));
return user;
}
}
service
package com.wjb.oa.service;
import com.wjb.oa.dao.UserDao;
import com.wjb.oa.entity.User;
import com.wjb.oa.service.exception.BussinessException;
public class UserService {
private UserDao userDao = new UserDao();
public User checkLogin(String username,String password){
User user=userDao.selectByUsername(username);
if(user==null){
//抛出用户不存在异常
throw new BussinessException("L001", "用户名不存在");
}
if(!password.equals(user.getPassword())){
throw new BussinessException("L002", "密码错误");
}
return user;
}
}
controller
package com.wjb.oa.controller;
import com.alibaba.fastjson.JSON;
import com.wjb.oa.entity.User;
import com.wjb.oa.service.UserService;
import com.wjb.oa.service.exception.BussinessException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
@WebServlet(name = "LoginServlet" ,urlPatterns = "/check_login")
public class LoginServlet extends HttpServlet {
Logger logger = LoggerFactory.getLogger(LoginServlet.class);
private UserService userService=new UserService();
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
//接受用户输入
String username=request.getParameter("username");
String password = request.getParameter("password");
Map<String, Object> result = new HashMap<>();
try {
//调用业务逻辑
User user = userService.checkLogin(username, password);
result.put("code","0");
result.put("message", "success");
}catch (BussinessException e){
logger.error(e.getMessage(),e);
result.put("code",e.getCode());
result.put("message", e.getMessage());
}catch (Exception e){
logger.error(e.getMessage(),e);
result.put("code",e.getClass().getSimpleName());
result.put("message", e.getMessage());
}
//返回对应结果
String json=JSON.toJSONString(result);
response.getWriter().println(json);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
exception
package com.wjb.oa.service.exception;
/**
* 业务逻辑异常
*/
public class BussinessException extends RuntimeException{
private String code;//一场编码,异常的标识
private String message;//异常具体的文本消息
public BussinessException(String code,String msg){
super(code+":"+msg);
this.code=code;
this.message=msg;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
@Override
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
正在回答
同学你好,这种错误可以通过调试,看一下问题出现在哪里。如果使用了别人的框架,可以检查一下是否拼写有错,导致程序出错。
祝学习愉快~
- 参与学习 人
- 提交作业 9400 份
- 解答问题 16556 个
综合就业常年第一,编程排行常年霸榜,无需脱产即可学习,北上广深月薪过万 无论你是未就业的学生还是想转行的在职人员,不需要基础,只要你有梦想,想高薪
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星