引言
入门实例,实现基本的登录和注册功能!
设计思想
 首先注册账号,当未注册时,将有异常抛出,原因,静态集合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中未含有数据!
依赖教材代码,从中找出漏洞,提高自己的思维!
欢迎加入程序员技术交流群一起成长!