动态登录功能实现以及注册 功能

Posted 池鱼i_

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了动态登录功能实现以及注册 功能相关的知识,希望对你有一定的参考价值。

登陆成功后会出现 这个页面会在   WEB-INF下     

添加用户界面在web下 

注册成功会跳转成功页面并显示添加的信息  

 

用户列表显示所有的用户信息

 

下面就是代码了 数据库简单 就自己建吧 :

用到的jar包有:

springMVC配置文件:

<?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:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx
        https://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/mvc
        https://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <context:component-scan base-package="cn.hp"></context:component-scan>

    <mvc:annotation-driven></mvc:annotation-driven>

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="viewResolver">
        <property name="prefix" value="/WEB-INF/pages/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>


    <bean class="org.apache.commons.dbcp.BasicDataSource" id="dataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://localhost:3306/user?characterEncoding=utf8"></property>
        <property name="username" value="root"></property>
        <property name="password" value="123456"></property>
    </bean>

    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" id="scannerConfigurer" >
        <property name="basePackage" value="cn.hp.dao"></property>
    </bean>


    <bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFactory">
        <property name="dataSource" ref="dataSource"></property>
        <property name="configLocation" value="classpath:mybatis_config.xml"></property>
    </bean>



</beans>

mybatis.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>


</configuration>

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <servlet>
        <servlet-name>springMVC</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring_config.xml</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>springMVC</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

首先写jsp登陆页面    index.jsp:


<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>登录页面</title>
  </head>
  <body>
  <form action="login">
      登录<br/>
      账号:<input type="text" name="account"> <br/>
      密码:<input type="password" name="password"> <br/>
      <input type="submit" value="登录">
  </form>
  </body>
</html>

controller包   UserController类:

package cn.hp.controller;

import cn.hp.model.User;
import cn.hp.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/login")
    public String login (String account, String password, HttpServletRequest request){
        boolean loginFlag = userService.login(account,password);
        if(loginFlag){
            request.getSession().setAttribute("account",account);
            return "success";
        }else{
            return "forward:index.jsp";
        }

    }
    @RequestMapping("/add")
    public String register(User user){
        boolean regFlag = userService.register(user);
        if (regFlag){

            return "reg_success";
        }

        return "forward:add.jsp";
    }

    @RequestMapping("/showAll")
    public String showAll(HttpServletRequest request){
        List<User> users = userService.showAll();
        request.getSession().setAttribute("users",users);
        return "showAll" ;
    }




}

dao包   有mapper.xml   UserMapper   这两个名字最好是一样的    :

 

首先是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="cn.hp.dao.UserMapper">

<select id="selectAccountByPassword" resultType="int" >
  select  count(*) from  users  where account = #{account} and password=#{password}
</select>

  <insert id="add" parameterType="cn.hp.model.User">
  insert into users values (null , #{account} ,#{password} ,#{name} ,#{address} ,#{phone} )
  </insert>

  <select id="selectAll" resultType="cn.hp.model.User">
        select * from users
    </select>

</mapper>


还有就是  UserMapper是实现类interface   

package cn.hp.dao;

import cn.hp.model.User;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface UserMapper {

    int selectAccountByPassword(@Param("account") String account, @Param("password") String password);
    int add(User user);

    List<User> selectAll();
}

model包     User类:

package cn.hp.model;

public class User {
    private String account;
    private String password;
    private String name;
    private String address;
    private String phone;

    @Override
    public String toString() {
        return "User{" +
                "account='" + account + '\\'' +
                ", password='" + password + '\\'' +
                ", name='" + name + '\\'' +
                ", address='" + address + '\\'' +
                ", phone='" + phone + '\\'' +
                '}';
    }

    public User(String account, String password, String name, String address, String phone) {
        this.account = account;
        this.password = password;
        this.name = name;
        this.address = address;
        this.phone = phone;
    }

    public String getAccount() {
        return account;
    }

    public void setAccount(String account) {
        this.account = account;
    }

    public String getPassword() {
        return password;
    }

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

    public String getName() {
        return name;
    }

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

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public User() {
    }
}

service类  UserService:

package cn.hp.service;

import cn.hp.dao.UserMapper;
import cn.hp.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserService {
    @Autowired
    private UserMapper userMapper;


    public boolean login(String account, String password) {
        int count=  userMapper.selectAccountByPassword(account,password);
        if(count>0){
            return true;
        }
        return false;

    }


    public boolean register(User user) {
        int i = userMapper.add(user);
        if (i > 0 ){
            return true;
        }
        return false ;
    }

    public List<User> showAll() {
        return userMapper.selectAll();
    }
}

登陆成功界面success.jsp    在WEB-INF下pages包下 创建的


<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<a href="add.jsp">添加用户</a><br/>
<a href="showAll">用户列表</a>

</body>
</html>

add.jsp添加页面

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2021/5/13
  Time: 19:20
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>添加页面</title>
</head>
<body>
<form action="add">
    用 户 名:<input type="text" name="account"> <br/>
    密    码:<input type="password" name="password"> <br/>
    真实姓名:<input type="text" name="name"> <br/>
    地    址:<input type="text" name="address"> <br/>
    电    话:<input type="text" name="phone"> <br/>
    <input type="submit" value="提交">
</form>
</body>
</html>

注册成功页面在WEB-INF下pages包下 创建的:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>Title</title>
</head>
<body>

<h1>用户添加成功,信息如下:</h1>
<span>添加用户成功 信息如下</span><br/>
<span>用户名 ${user.account}</span><br/>
<span>密码 ${user.password}</span><br/>
<span>真实姓名 ${user.name}</span><br/>
<span>地址 ${user.address}</span><br/>
<span>电话 ${user.phone}</span><br/>


<h1>注册成功,点击返回 <a href="index.jsp" >登录页</a></h1>
</body>
</html>

用户信息页面     WEB-INF下pages包下 创建的

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>用户信息</title>
</head>
<body>
<div align="center" >

    <table cellspacing="0" border="1px" width="60%">
        <tr>
            <th>用户名</th>
            <th>密码</th>
            <th>真实姓名</th>
            <th>地址</th>
            <th>手机</th>
        </tr>

        <c:forEach items="${users}" var="user"  >
            <tr>
                <td>${user.account }</td>
                <td>${user.password}</td>
                <td>${user.name}</td>
                <td>${user.address}</td>
                <td>${user.phone}</td>
            </tr>
        </c:forEach>

    </table>
</div>
</form>
</body>
</html>

有什么不足的请指出   ,虚心接受   记得一键三连哟!!!!  

以上是关于动态登录功能实现以及注册 功能的主要内容,如果未能解决你的问题,请参考以下文章

使用MVC实现登录注册功能(数据保存到数据库)详细讲解以及代码

利用PHP连接数据库——实现用户登录注册功能以及管理员对用户注册的审核功能

Vue实现登录以及登出

如何在注册页面实现动态检测用户名的功能?

冲刺结果(补补补)——登录界面,找回密码和注册界面

Flask博客实战 - 实现登录注册功能