Login and register Struts2 to implement Map as a database

introduction

         Getting started example, to achieve basic login and registration functions!

Design ideas

        First register an account. When it is not registered, an exception will be thrown. The reason is that the static collection Map is empty. We will judge in LoginActionModel.java. When registering an account, we save the data in the static collection Map of AdduserAction! Return to the main page after success, and then we log in, there are three situations! The registered account and password are correct, the account exists and the password is incorrect, the account does not exist, and so on!

Specific implementation code

V in MVC

Home interface

<%@ 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>

Registration interface

<%@ 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>

Return to the result interface

<%@ 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>

M in MVC

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;
    }

}

C in MVC

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;
    }

}

Directory Structure

Insert picture description here

Test Data

Account: ccit Password: 1
Account: ccit Password: 1123
Account: cc Password: Any

operation result

Insert picture description here

                                                                        Figure one

Account: ccit Password: 1

Insert picture description here

Account: ccit Password: 123

Insert picture description here

Account: cc Password: Any

Insert picture description here
The reason for this result is that you have not registered an account, and there is no data in the static map!

Rely on the textbook code, find loopholes in it, and improve your thinking!

Welcome to join the programmer technology exchange group to grow together!

Reference link

Map
static
try catch finally

Guess you like

Origin blog.csdn.net/qq_41827511/article/details/105448445