您现在的位置是:首页 >学无止境 >用 Spring Web MVC 框架实现用户验证登录。程序从一个页面 login.jsp 进入,要求提供的服务是判断登录的用户和密码是否都是“admin“,若是跳转到页面 loginSucces网站首页学无止境

用 Spring Web MVC 框架实现用户验证登录。程序从一个页面 login.jsp 进入,要求提供的服务是判断登录的用户和密码是否都是“admin“,若是跳转到页面 loginSucces

小小的猫 2024-07-24 00:01:01
简介用 Spring Web MVC 框架实现用户验证登录。程序从一个页面 login.jsp 进入,要求提供的服务是判断登录的用户和密码是否都是“admin“,若是跳转到页面 loginSucces
用 Spring Web MVC 框架实现用户验证登录。程序从一个页面 login.jsp 进入,要求提
供的服务是判断登录的用户和密码是否都是"admin",若是跳转到页面 loginSuccess.jsp, 反
之跳转到 loginError.jsp。

这是基本架构

我这里使用的是maven,通过pom.xml来导包

e35bffe0b38c412bb64261d97fd821c7.png

因为代码过于简单,剩下的大家看着结构我上代码,看着代码理解SpringMVC的强大

Bean.java

package com.work.bean;

//user实体类【标准java类的定义】 com.work.bean.User
public class User {

    private int id;
    private String name;
    private String password;
    private int age;

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + ''' +
                ", password='" + password + ''' +
                ", age=" + age +
                '}';
    }

    public User() {
    }

    public User(int id, String name, String password, int age) {
        this.id = id;
        this.name = name;
        this.password = password;
        this.age = age;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

UserController.java
package com.work.controller;

import com.work.bean.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

//标志注解,用于标明当前类是控制的代码,交给ioc容器管理的
@Controller
public class UserController {
    //页面跳转的请求处理方法
    @RequestMapping("/login")
    public String login(User user) {
        //登录成功
        if (user.getName().equals("admin") && user.getPassword().equals("admin")) {
            return "loginSuccess";//  走视图解析器的 /loginSuccess.jsp
        } else {//登录失败
            return "loginError";//  走视图解析器的   /loginError.jsp
        }
    }
}

SpringMVC.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:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
    <!--启动注解识别 -->
    <context:annotation-config />
    <!--扫描controller包,将controller交给SpringIOC容器进行对象管理 -->
    <context:component-scan base-package="com.work.controller">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" /></context:component-scan>
    <!--开启注解驱动 -->
    <mvc:annotation-driven />
    <!-- 支持文件上传 -->
    <bean id="multipartResolver"
          class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="104857600" />
        <property name="maxInMemorySize" value="4096" />
        <property name="defaultEncoding" value="UTF-8"></property>
    </bean>
    <!--开通静态资源的访问 -->
    <mvc:default-servlet-handler />
    <!-- 视图解析器 -->
    <bean
            class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass"
                  value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>
</beans>

web.xml

<!-- 定义Spring MVC的前端控制器 -->
<servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>
        org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <!--初始化参数配置  配置xml文件路径-->
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:springMVC.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<!-- 让Spring MVC的前端控制器拦截所有请求 -->
<servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

pom.xml依赖坐标(有些没有用到)

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>SpringTest</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.2.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.2</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3.1</version>
        </dependency>
    </dependencies>
</project>

很关键的一步就是需要手动在idea里面导入maven的依赖

先点击项目结构,点击工件,点击WEB-INF,建立lib

444789536ce043fc91823cd0ab1ef1e3.png

 Next

0018bd6397c84460aaacbe9d0b52af9f.png

 32ea76e09d5b4e4c81ebbef4c378cd75.png

cd7ae0e855de483ea87d3d61b6a64578.png

login.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
  <title>Title</title>
</head>
<body>
<div align="center">
<form action="login">
  用户名:<input type="text" name="name"/><br>
  密码:<input type="password" name="password"/><br>
  <input type="submit" value="登录"/><br>
</form>
</div>
</body>
</html>

 loginSuccess.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>登录成功</title>
</head>
<body>
<div align="center"><h1>登录成功了</h1></div>
</body>
</html>

loginError.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>登录失败</title>
</head>
<body><div align="center"><h1>登录失败,请返回重新登录</h1></div>
</body>
</html>

以上用到的代码就这些 

风语者!平时喜欢研究各种技术,目前在从事后端开发工作,热爱生活、热爱工作。