执行登录的时候,产生如下错误
我查看网络显示如下
控制台显示如下所示:
项目所有代码如下:
UserMapper
package com.hotel.dao;
import com.hotel.entity.User;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
public interface UserMapper {
@Select("select * from admin where username = #{username} and password = #{password}")
User getUser(@Param("username") String username, @Param("password") String password);
}
User
package com.hotel.entity;
import lombok.Data;
@Data
public class User {
int id;
String username;
String nickname;
String password;
}
MainFilter
package com.hotel.filter;
import com.hotel.entity.User;
import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpFilter;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import java.io.IOException;
@WebServlet("/*")
public class MainFilter extends HttpFilter {
@Override
protected void doFilter(HttpServletRequest req, HttpServletResponse res, FilterChain chain) throws IOException, ServletException {
String url = req.getRequestURL().toString();
if(!url.contains("/static/") && !url.endsWith("login")){
HttpSession session = req.getSession();
User user = (User) session.getAttribute("user");
if(user == null){
res.sendRedirect("login");
return;
}
}
chain.doFilter(req,res);
}
}
UserServiceImpl
package com.hotel.service.impl;
import com.hotel.dao.UserMapper;
import com.hotel.entity.User;
import com.hotel.service.UserService;
import com.hotel.utils.MybatisUtil;
import jakarta.servlet.http.HttpSession;
import org.apache.ibatis.session.SqlSession;
public class UserServiceImpl implements UserService {
@Override
public boolean auth(String username, String password, HttpSession session){
try (SqlSession sqlSession = MybatisUtil.getSession()){
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
User user = mapper.getUser(username, password);
if(user == null) return false;
session.setAttribute("user",user);
return true;
}
}
}
UserService
package com.hotel.service;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
public interface UserService {
boolean auth(String username, String password, HttpSession session);
}
LoginServlet
package com.hotel.serviet;
import com.hotel.service.UserService;
import com.hotel.service.impl.UserServiceImpl;
import com.hotel.utils.ThymeleafUtil;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.thymeleaf.context.Context;
import java.io.IOException;
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
UserService service;
@Override
public void init() throws ServletException{
service = new UserServiceImpl() ;
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
Context context = new Context();
if(req.getSession().getAttribute("login-failure") != null){
context.setVariable("failure",true);
req.getSession().removeAttribute("login-failure");
}
ThymeleafUtil.process("login.html", context,resp.getWriter());
}
@Override
protected void doPost(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException{
String username =req.getParameter("username");
String password =req.getParameter("password");
String remember =req.getParameter("remember-me");
if(service.auth(username,password,req.getSession())){
resp.getWriter().write("Login Success!");
}else {
req.getSession().setAttribute("login-failure",new Object());
this.doGet(req,resp);
}
}
}
MybaitsUtil
package com.hotel.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.IOException;
public class MybatisUtil {
private static SqlSessionFactory factory;
static {
try{
factory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsReader("mybatis-config.xml"));
}catch (IOException e){
e.printStackTrace();
}
}
public static SqlSession getSession(){
return factory.openSession(true);
}
}
ThymeleafUtil
package com.hotel.utils;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.IContext;
import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver;
import java.io.Writer;
public class ThymeleafUtil {
private static final TemplateEngine engine;
static {
engine = new TemplateEngine();
ClassLoaderTemplateResolver r = new ClassLoaderTemplateResolver();
r.setCharacterEncoding("UTF-8");
engine.setTemplateResolver(r);
}
public static void process(String template, IContext context, Writer writer){
engine.process(template,context,writer);
}
}
login.html
<!DOCTYPE html>
<!--[if IE 8]> <html lang="en" class="ie8 no-js"> <![endif]-->
<!--[if IE 9]> <html lang="en" class="ie9 no-js"> <![endif]-->
<!--[if !IE]><!-->
<html lang="zxx" xmlns: th ="https://www.thymeleaf.org">
<!--<![endif]-->
<!-- Begin Head -->
<head>
<title>酒店管理系统 - 登录</title>
<meta charset="utf-8">
<meta content="width=device-width, initial-scale=1.0" name="viewport">
<meta name="description" content="">
<meta name="keywords" content="">
<meta name="author" content="">
<meta name="MobileOptimized" content="320">
<!--Start Style -->
<link rel="stylesheet" type="text/css" href="static/css/fonts.css">
<link rel="stylesheet" type="text/css" href="static/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="static/css/auth.css">
</head>
<body>
<div class="ad-auth-wrapper">
<div class="ad-auth-box">
<div class="row align-items-center">
<div class="col-xl-6 col-lg-6 col-md-12 col-sm-12 col-12">
<div class="ad-auth-img">
<img src="static/picture/auth-img1.png" alt="">
</div>
</div>
<div class="col-xl-6 col-lg-6 col-md-12 col-sm-12 col-12">
<div class="ad-auth-content">
<form method="post" action="login">
<h2><span class="primary">你好,</span>欢迎!</h2>
<p th:unless="${failure}">请输入用户名和密码进行登录</p>
<p th:if="${failure}" style="color: red">您的用户名或密码输入错误</p>
<div class="ad-auth-form">
<div class="ad-auth-feilds mb-30">
<input type="text" placeholder="用户名称" class="ad-input" name="username">
<div class="ad-auth-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewbox="0 0 483.3 483.3"><path d="M424.3,57.75H59.1c-32.6,0-59.1,26.5-59.1,59.1v249.6c0,32.6,26.5,59.1,59.1,59.1h365.1c32.6,0,59.1-26.5,59.1-59.1 v-249.5C483.4,84.35,456.9,57.75,424.3,57.75z M456.4,366.45c0,17.7-14.4,32.1-32.1,32.1H59.1c-17.7,0-32.1-14.4-32.1-32.1v-249.5 c0-17.7,14.4-32.1,32.1-32.1h365.1c17.7,0,32.1,14.4,32.1,32.1v249.5H456.4z" data-original="#000000" class="active-path" data-old_color="#000000" fill="#9abeed"></path><path d="M304.8,238.55l118.2-106c5.5-5,6-13.5,1-19.1c-5-5.5-13.5-6-19.1-1l-163,146.3l-31.8-28.4c-0.1-0.1-0.2-0.2-0.2-0.3 c-0.7-0.7-1.4-1.3-2.2-1.9L78.3,112.35c-5.6-5-14.1-4.5-19.1,1.1c-5,5.6-4.5,14.1,1.1,19.1l119.6,106.9L60.8,350.95 c-5.4,5.1-5.7,13.6-0.6,19.1c2.7,2.8,6.3,4.3,9.9,4.3c3.3,0,6.6-1.2,9.2-3.6l120.9-113.1l32.8,29.3c2.6,2.3,5.8,3.4,9,3.4 c3.2,0,6.5-1.2,9-3.5l33.7-30.2l120.2,114.2c2.6,2.5,6,3.7,9.3,3.7c3.6,0,7.1-1.4,9.8-4.2c5.1-5.4,4.9-14-0.5-19.1L304.8,238.55z" data-original="#000000" class="active-path" data-old_color="#000000" fill="#9abeed"></path></svg>
</div>
</div>
<div class="ad-auth-feilds">
<input type="password" placeholder="密码" class="ad-input" name="password">
<div class="ad-auth-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewbox="0 0 482.8 482.8"><path d="M395.95,210.4h-7.1v-62.9c0-81.3-66.1-147.5-147.5-147.5c-81.3,0-147.5,66.1-147.5,147.5c0,7.5,6,13.5,13.5,13.5 s13.5-6,13.5-13.5c0-66.4,54-120.5,120.5-120.5c66.4,0,120.5,54,120.5,120.5v62.9h-275c-14.4,0-26.1,11.7-26.1,26.1v168.1 c0,43.1,35.1,78.2,78.2,78.2h204.9c43.1,0,78.2-35.1,78.2-78.2V236.5C422.05,222.1,410.35,210.4,395.95,210.4z M395.05,404.6 c0,28.2-22.9,51.2-51.2,51.2h-204.8c-28.2,0-51.2-22.9-51.2-51.2V237.4h307.2L395.05,404.6L395.05,404.6z" data-original="#000000" class="active-path" data-old_color="#000000" fill="#9abeed"></path><path d="M241.45,399.1c27.9,0,50.5-22.7,50.5-50.5c0-27.9-22.7-50.5-50.5-50.5c-27.9,0-50.5,22.7-50.5,50.5 S213.55,399.1,241.45,399.1z M241.45,325c13,0,23.5,10.6,23.5,23.5s-10.5,23.6-23.5,23.6s-23.5-10.6-23.5-23.5 S228.45,325,241.45,325z" data-original="#000000" class="active-path" data-old_color="#000000" fill="#9abeed"></path></svg>
</div>
</div>
</div>
<div class="ad-other-feilds">
<div class="ad-checkbox">
<label>
<input type="checkbox" name="remember-me" class="ad-checkbox">
<span>记住我</span>
</label>
</div>
<a class="forgot-pws-btn" href="#">忘记密码?</a>
</div>
<div class="ad-auth-btn">
<button class="ad-btn ad-login-member">登录</button>>
</div>
<p class="ad-register-text">没有用户吗? <a href="#">注册用户</a></p>
</form>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
mybatis-configs.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/hotel_manage"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper class="com.hotel.dao.UserMapper"/>
</mappers>
</configuration>
请问谁知道问题所在,可以告诉我吗?