1. 程式人生 > >簡單的基於Struts2的使用者註冊模組的實現

簡單的基於Struts2的使用者註冊模組的實現

利用Struts2實現簡單的使用者註冊功能,並具備輸入字元的校驗功能。

以下index.jsp為首頁,顯示登錄檔單:

<%--
  首頁,顯示登錄檔單
--%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Java web實驗五</title>
</head>
<body
>
<form action="register.action" method="post"> <table> <tr> <td>使用者名稱:</td> <td><input type="text" name="username"></td> </tr> <tr> <td>密碼:</td> <td>
<input type="password" name="upassword"></td> </tr> <tr> <td>確認密碼:</td> <td><input type="password" name="repassword"></td> </tr> <tr> <td>姓名:</td> <td
>
<input type="text" name="name"></td> </tr> <tr> <td>年齡:</td> <td><input type="text" name="age"></td> </tr> <tr> <td>出生日期:</td> <td><input type="text" name="birth"></td> </tr> <tr> <td>郵箱地址:</td> <td><input type="text" name="email"></td> </tr> <tr> <td><input type="submit" value="提交"></td> <td><input type="reset" value="重置"></td> </tr> </table> <s:actionerror/> </form> </body> </html>

以下部分為ShowUserInfo.jsp,顯示使用者註冊後的資訊:

<%--
  Created by IntelliJ IDEA.
  User: 23832
  Date: 2017/5/15
  Time: 19:33
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Java web實驗五</title>
</head>
<body>
使用者名稱:${username}<br>
密碼:${upassword}<br>
確認密碼:${repassword}<br>
姓名:${name}<br>
年齡:${age}<br>
出生日期:${birth}<br>
郵箱地址:${email}<br>
</body>
</html>

以下為RegisterAction.java,註冊的業務邏輯部分:

package com;

import com.opensymphony.xwork2.ActionSupport;

import java.util.regex.Pattern;

public class RegisterAction extends ActionSupport {
    private String username;
    private String upassword;
    private String repassword;
    private String name;
    private String age;
    private String birth;
    private String email;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getUpassword() {
        return upassword;
    }

    public void setUpassword(String upassword) {
        this.upassword = upassword;
    }

    public String getRepassword() {
        return repassword;
    }

    public void setRepassword(String repassword) {
        this.repassword = repassword;
    }

    public String getName() {
        return name;
    }

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

    public String getAge() {
        return age;
    }

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

    public String getBirth() {
        return birth;
    }

    public void setBirth(String birth) {
        this.birth = birth;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    @Override
    public void validate() {//字元校驗
        if (upassword == null || "".equals(upassword)) {//判斷輸入密碼是否為空
            this.addActionError("密碼必須輸入");
        }
        if (repassword == null || "".equals(repassword)) {//判斷確認密碼是否為空
            this.addActionError("確認密碼必須輸入");
        }
        if (upassword != null && repassword != null && !repassword.equals(upassword)) {//判斷密碼和確認密碼是否相同
            this.addActionError("密碼必須和確認密碼相同");
        }
        Pattern pattern = Pattern.compile("[0-9]*");
        if (age==null||"".equals(age)){//判斷年齡是否為空
            this.addActionError("年齡不能為空");
        }
        if (age!=null&&!pattern.matcher(age).matches()) {//利用正則表示式判斷年齡是否為數字
            this.addActionError("年齡必須為數字");
        }

        if (email==null||"".equals(email)){//判斷郵箱是否為空
            this.addActionError("郵箱不能為空");
        }
        if (email!=null&&!Pattern.matches("[a-zA-Z0-9_-]*@([a-zA-Z0-9-_]+"+"\\.)+(com|gov|net|com\\.cn|edu\\.cn)",email)) {//利用正則表示式判斷郵箱格式是否正確
            this.addActionError("郵箱格式不正確");
        }

    }

    public String execute() throws Exception {
        return "success";
    }
}

以下為Struts2的配置部分:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
        "http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
    <package name="struts2" extends="struts-default">
        <action name="register" class="com.RegisterAction">
            <!--定義處理結果與試圖資源之間的關係-->
            <result name="success">/ShowUserInfo.jsp</result>
            <result name="input">/index.jsp</result>
        </action>
    </package>
</struts>

web.xml檔案配置就不列出了。
這裡寫圖片描述

這裡寫圖片描述