登录与注册 Struts2实现 Map充当数据库

引言

         入门实例,实现基本的登录和注册功能!

设计思想

        &nbsp首先注册账号,当未注册时,将有异常抛出,原因,静态集合Map为空,我们在LoginActionModel.java进行判断。注册账号时我们将数据保存在AdduserAction的静态集合中 Map!成功后返回主页面,然后我们登录,分三种情况!注册账号和密码都正确,账号存在密码不正确,账号不存在等等!

具体实现代码

MVC中的V

首页界面
<%@ taglib prefix="S" uri="/struts-tags" %>
<%--
  Created by IntelliJ IDEA.
  User: gao
  Date: 2020/4/10
  Time: 17:47
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>login</title>
  </head>
  <body>
  <S:form action="loginActionModel">
    <S:textfield name="userName" label="Username"></S:textfield>
    <S:password name="password" label="Password"></S:password>
    <S:submit></S:submit>
  </S:form>
  <S:a href="regist.jsp">注册</S:a>

  </body>
</html>

注册界面
<%@ taglib prefix="S" uri="/struts-tags" %>
<%--
  Created by IntelliJ IDEA.
  User: gao
  Date: 2020/4/10
  Time: 19:53
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<S:form action="adduser">
    <S:textfield name="userName" label="Username"></S:textfield>
    <S:password name="password" label="Password"></S:password>
    <S:submit></S:submit>
</S:form>
</body>
</html>

返回结果界面
<%@ taglib prefix="S" uri="/struts-tags" %>
<%--
  Created by IntelliJ IDEA.
  User: gao
  Date: 2020/4/10
  Time: 17:54
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
${message}

</body>
</html>

MVC中的M

User.java
public class User {
    private String username;
    private String password;
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }

}

MVC中的C

AdduserAction.java
import com.opensymphony.xwork2.Action;
import java.awt.event.ActionEvent;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AdduserAction implements Action {
    private static Map<String,String> map=new HashMap<String,String>();
    private static int flag;
    private static String userName;
    private static String password;
    public static Map<String, String> getMap() {
        return map;
    }
    public static void setMap() {
        try{
           map.put(getUserName(),getPassword());
            flag=1;
        }catch (Exception e){
            flag=0;
            System.out.println("存储失败");
        }
    }
    public static String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        AdduserAction.userName = userName;


    }
    public static String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        AdduserAction.password = password;
    }

    @Override
    public String execute() throws Exception {
        setMap();
        if(flag==1){
            return SUCCESS;
        }else{
            return ERROR;
        }

    }
}

LoginActionModel.java
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ModelDriven;

public class LoginActionModel implements Action {
    private String userName;
    private String password;
    private String message;

    public String getMessage() {
        return message;
    }

    @Override
    public String execute() {
        try{
            if(AdduserAction.getMap().get(getUserName()).equals(getPassword())==true){

                message="欢迎您"+getUserName();
                return "success";
            }else if(AdduserAction.getMap().get(getUserName()).equals(getPassword())==false){
                message="密码错误";
                return "success";
            }else{
                return "success";
            }
        }catch (Exception e){
            message="您未注册";
            return "success";

        }
    }
    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

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

}

目录结构

在这里插入图片描述

测试数据

账号:ccit密码:1
账号:ccit密码:1123
账号:cc密码:任意

运行结果

在这里插入图片描述

                                                                        图一

账号:ccit密码:1

在这里插入图片描述

账号:ccit密码:123

在这里插入图片描述

账号:cc密码:任意

在这里插入图片描述
出现这种结果的原因就是,您未注册账号,静态Map中未含有数据!

依赖教材代码,从中找出漏洞,提高自己的思维!

欢迎加入程序员技术交流群一起成长!

参考链接

Map
静态
try catch finally

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值