基于springmvc+mybatis的登录表单提交后404

前端填写正确的用户名和密码后,提交登录,浏览器显示404,前端提交的地址是/userlogin,后端对接的/user/userlogin也是对的,浏览器f12里显示请求的确成功传给了后端,但是不知道为什么404。用的是tomcat8.5,java版本1.8,phpstudy最新版,mysql5.7.26。

这是所用到的包:

图片说明

这是目录结构:

图片说明

这是web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>springmvc_test</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <!-- 配置spring监听器 -->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <!-- 加载spring配置文件 -->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:config/context-config.xml</param-value>
  </context-param>
  <!-- 配置前端控制器 -->
  <servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <!-- DispatcherServlet在初始化方法里面会读取该初始化参数的值来获得
                 spring配置文件的位置 ,然后启动spring容器。 -->
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:config/springmvc-config.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
  <!-- 配置字符编码 -->
  <filter>
    <filter-name>encodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>UTF-8</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>encodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

这是context-config.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:aop="http://www.springframework.org/schema/aop"
 xmlns:tx="http://www.springframework.org/schema/tx"
 xmlns:context="http://www.springframework.org/schema/context"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
                     http://www.springframework.org/schema/beans/spring-beans.xsd
                     http://www.springframework.org/schema/tx
                     http://www.springframework.org/schema/tx/spring-tx.xsd
                     http://www.springframework.org/schema/aop
                     http://www.springframework.org/schema/aop/spring-aop.xsd
                     http://www.springframework.org/schema/context
                     http://www.springframework.org/schema/context/spring-context.xsd">
    <!-- 扫描service包下的注解 -->
    <context:component-scan base-package="com.test.service"></context:component-scan>
    <!-- 配置数据库 -->
    <!-- 加载配置文件 -->
    <!--  <context:property-placeholder location="classpath:jdbc.properties"/> -->
    <bean id="dateSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driver" value="com.mysql.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://localhost:3306/Eday_Test"></property>
        <property name="username" value="root"></property>
        <property name="password" value="123456"></property>
    </bean>
    <!-- 配置Sqlsessionfactory并将数据源注入 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <!-- 引入数据源 -->
        <property name="dateSource" ref="dateSource"></property>
        <!-- 载入mybatis配置文件 -->
        <property name="configLocation" value="classpath:mybatis-config.xml"></property>
        <!-- 载入配置mapper映射的xml -->
        <property name="mapperLocations" value="classpath:com/test/mapper/*.xml"></property>
    </bean>
    <!-- 配置扫描mapper接口 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackge" value="com.test.mapper"></property>
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
    </bean>
    <!-- 配置事务管理器 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dateSource" ref="dateSource"></property>
    </bean>
    <tx:annotation-driven transaction-manager="transactionManager"/>
</beans>

这是mybatis-config.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>
    <typeAliases>
    <!-- 配置别名 -->
        <typeAlias alias="User" type="com.test.pojo.User"/>
    </typeAliases>
</configuration>

这是springmvc-config.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <!-- 扫描controller包下的注解 -->
    <context:component-scan base-package="com.test.controller"></context:component-scan>
    <!-- 开启注解 -->
    <mvc:annotation-driven></mvc:annotation-driven>
    <!-- 静态资源访问 -->
    <mvc:default-servlet-handler/>
    <!-- 视图解析器 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <!-- 配置试图解析的默认路径,即配置页面的根路径 -->
        <property name="prefix" value="/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

</beans>

这是UserController.java:

package com.test.controller;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.test.pojo.User;
import com.test.service.UserService;

//声明控制器
@Controller
//设置bean的scope属性为多例(prototype)
@Scope("prototype")
//设置请求映射,当客户端请求/user时,转到该控制器处理
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;
    @RequestMapping(value="/userlogin")


    public ModelAndView login(String user_Name,String user_pwd,ModelAndView mv,HttpSession session){
        //调用userService中的login方法处理user实体类对象
        User user = userService.login(user_Name,user_pwd);
        //登录的逻辑判断,判断条件是返回结果不为空
        if(user!=null){
            //登陆成功,将user对象设置到HttpSession作用范围域中,相当于服务端的cookie,有效时间默认30分钟
            //在程序运行期间,在任意页面都可以提取它的值。
            session.setAttribute("user",user);
            //转发到main请求
            //登录成功,跳转页面
            mv.setViewName("login/login-success");
        }else{
            //登录失败,向前端传递失败信息
            mv.addObject("message","用户名或密码错误,请重新输入!");
            //登录失败,跳转到登录页面
            mv.setViewName("login");
        }
        return mv;
    }
    //跳转到用户注册界面
    @RequestMapping(value="/userregister"/*,method=RequestMethod.POST*/)
    public String register(User user){
        String user_Name = user.getUser_Name();
        //如果数据库中没有该用户,可以注册,否则跳转页面
        if(userService.findByUserName(user_Name)==null){
            //添加用户
            userService.register(user);
            //注册成功,跳转到主页面
            return "index";
        }else{
            //注册失败,跳转到错误页面
            return "error";
        }
    }
}

这是UserMapper.java:

package com.test.mapper;

import org.apache.ibatis.annotations.Param;

import com.test.pojo.User;

public interface UserMapper {
    //根据用户名和密码查找,mybatis中有多个参数时,需要使用@Param注解
    User findByUserNameAndPassword(@Param("user_Name")String user_Name,@Param("user_Pwd")String user_Pwd);
    //增加用户
    void addUser(User user);
    //根据用户名查询
    User findByUserName(String user_Name);
}

这是UserMapper.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.tes.mapper.UserMapper">
    <!-- 根据用户名和密码查询 -->
    <select id="findByUserNameAndPasssword" resultType="User">
        select * from user where user_Name=#{user_Name} and user_Pwd=#{user_Pwd}
    </select>
    <!-- 增加用户 -->
    <insert id="addUser" parameterType="User">
        insert into user
        (user_Name,user_Pwd,user_Email,user_NickName,user_Birth,user_Phone,user_InvitationCode)
        values(#{user_Name},#{user_Pwd},#{user_Email},#{user_NickName},#{user_Birth},#{user_Phone},#{user_InvitationCode})
    </insert>
    <!-- 根据用户名查询 -->
    <select id="findByUserName" resultType="User">
        select * from user where user_Name=#{user_Name}
    </select>

</mapper>

这是User.java:

package com.test.pojo;

import java.sql.Timestamp;

public class User {
    private int user_Id;
    private String user_Name;
    private int user_Pwd;
    private String user_Email;
    private String user_NickName;
    private Timestamp user_Time;
    private String user_Birth;
    private int user_Fans;
    private int user_Follow;
    private int user_Score;
    private String user_HeadImgAddr;
    private int user_Phone;
    private String user_InvitationCode;
    public int getUser_Id() {
        return user_Id;
    }
    public void setUser_Id(int user_Id) {
        this.user_Id = user_Id;
    }
    public String getUser_Name() {
        return user_Name;
    }
    public void setUser_Name(String user_Name) {
        this.user_Name = user_Name;
    }
    public int getUser_Pwd() {
        return user_Pwd;
    }
    public void setUser_Pwd(int user_Pwd) {
        this.user_Pwd = user_Pwd;
    }
    public String getUser_Email() {
        return user_Email;
    }
    public void setUser_Email(String user_Email) {
        this.user_Email = user_Email;
    }
    public String getUser_NickName() {
        return user_NickName;
    }
    public void setUser_NickName(String user_NickName) {
        this.user_NickName = user_NickName;
    }
    public Timestamp getUser_Time() {
        return user_Time;
    }
    public void setUser_Time(Timestamp user_Time) {
        this.user_Time = user_Time;
    }
    public String getUser_Birth() {
        return user_Birth;
    }
    public void setUser_Birth(String user_Birth) {
        this.user_Birth = user_Birth;
    }
    public int getUser_Fans() {
        return user_Fans;
    }
    public void setUser_Fans(int user_Fans) {
        this.user_Fans = user_Fans;
    }
    public int getUser_Follow() {
        return user_Follow;
    }
    public void setUser_Follow(int user_Follow) {
        this.user_Follow = user_Follow;
    }
    public int getUser_Score() {
        return user_Score;
    }
    public void setUser_Score(int user_Score) {
        this.user_Score = user_Score;
    }
    public String getUser_HeadImgAddr() {
        return user_HeadImgAddr;
    }
    public void setUser_HeadImgAddr(String user_HeadImgAddr) {
        this.user_HeadImgAddr = user_HeadImgAddr;
    }
    public int getUser_Phone() {
        return user_Phone;
    }
    public void setUser_Phone(int user_Phone) {
        this.user_Phone = user_Phone;
    }
    public String getUser_InvitationCode() {
        return user_InvitationCode;
    }
    public void setUser_InvitationCode(String user_InvitationCode) {
        this.user_InvitationCode = user_InvitationCode;
    }
    @Override
    public String toString() {
        return "User [user_Id=" + user_Id + ", user_Name=" + user_Name + ", user_Pwd=" + user_Pwd + ", user_Email="
                + user_Email + ", user_NickName=" + user_NickName + ", user_Time=" + user_Time + ", user_Birth="
                + user_Birth + ", user_Fans=" + user_Fans + ", user_Follow=" + user_Follow + ", user_Score="
                + user_Score + ", user_HeadImgAddr=" + user_HeadImgAddr + ", user_Phone=" + user_Phone
                + ", user_InvitationCode=" + user_InvitationCode + "]";
    }
}

这是UserService.java:

package com.test.service;

import com.test.pojo.User;

public interface UserService {
    //通过用户名及密码核查用户登录
    User login(String user_Name,String user_Pwd);
    //增加用户
    void register(User user);
    //根据用户名查询
    User findByUserName(String user);
}

这是UserServiceImpl.java:

package com.test.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.test.mapper.UserMapper;
import com.test.pojo.User;
import com.test.service.UserService;

@Service
@Transactional
public class UserServiceImpl implements UserService {
    //注入UserMapper接口
    @Autowired
    private UserMapper userMapper;
    //登录,根据用户名和密码进行查询
    @Override
    public User login(String user_Name,String user_Pwd){
        return userMapper.findByUserNameAndPassword(user_Name,user_Pwd);
    }
    //注册,增加用户
    @Override
    public void register(User user){
        userMapper.addUser(user);
    }
    //根据用户名查询
    @Override
    public User findByUserName(String user_Name){
        return userMapper.findByUserName(user_Name);
    }
}

##这是login.jsp

        <%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" type="text/css" href="../css/login.css" />
<title>登录</title>
</head>
<body>
    <!--导航栏-->
    <nav class="nav">
    <ul>
        <li class="logo"><a href="#">Eday</a></li>
        <li class="shouye"><a href="../index.jsp" class="neirong">首页</a></li>
        <li class="fgx">|</li>
        <li><a href="#" class="neirong">文章列表</a></li>
        <li class="fgx">|</li>
        <li><a href="#" class="neirong">留言板</a></li>
        <li class="fgx">|</li>
        <li><a href="#" class="neirong">更新日志</a></li>
    </ul>
    </nav>
    <!--登录板块-->
    <header class="header">

    <p class="logintitle">
        <b>登录</b>
    </p>
    <form action="${pageContext.request.contextPath }/user/userlogin" method="post" class=login-form>
        <p class= login-username-p>
        <label for="username" class="login-username-text">用户名:</label>
        <input type=text name="user_Name" class="login-username-input">
        <br><a href="register.jsp" class=login-register>注册</a>
        </p>
        <p class="login-psw-p">
        <label for="psw" class="login-psw-text">密码:</label>
        <input type=password name="user_Pwd" class="login-psw-input">
        <br><a href="#" class=login-forget>忘记密码</a>
        </p>
        <div class=login-button-div>
        <label for=button></label>
        <button type=sublim class=login-button>登录</button>
        </div>
    </form>
    </header>
</body>
</html>

这是login-success.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>登录成功,请等待跳转...</title>
<script type="text/javascript"> 
onload=function(){ 
setInterval(go, 1000); 
}; 
var x=3; //利用了全局变量来执行 
function go(){ 
x--; 
if(x>0){ 
document.getElementById("sp").innerHTML=x; //每次设置的x的值都不一样了。 
}else{ 
location.href='../index.jsp'; 
} 
} 
</script>
</head>
<body>
<!--导航栏-->
    <nav class="nav">
    <ul>
        <li class="logo"><a href="#">Eday</a></li>
        <li class="shouye"><a href="../index.jsp" class="neirong">首页</a></li>
        <li class="fgx">|</li>
        <li><a href="#" class="neirong">文章列表</a></li>
        <li class="fgx">|</li>
        <li><a href="#" class="neirong">留言板</a></li>
        <li class="fgx">|</li>
        <li><a href="#" class="neirong">更新日志</a></li>
    </ul>
    </nav>
    <header>
        <div>
            <p>登录成功!页面将在3秒后自动跳转,请稍等...</p>
        </div>
    </header>
</body>
</html>

这是数据库:

图片说明

1个回答

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问