STRUTS Action 方法校验

单一方法校验

此记录是为了记录自己的代码,如果按照此教程去实验的话,可能无法得到正确的结果

一、显示层代码 (register.jsp)

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
    <title>用户注册</title>
</head>
<body>
<br/>
<center>
    ${tip}<br/>
    <s:form action="userRegister">
        <s:textfield name="username" label="用户名"/>
        <s:textfield name="phone" label="电话"/>
        <s:password name="password" label="密码"/>
        <s:password name="repassword" label="重复密码"/>
        <s:radio list="{'男','女'}" name="gender" value="男"/>
        <s:textfield name="birthday" label="生日(格式如:1990-01-01)"/>
        <s:checkboxlist list="{'读书','看电影'}" name="hobby" label="爱好"/>
        <s:textarea rows="3" cols="15" name="address" label="家庭住址"/>
        <s:submit value="注册"/>
    </s:form>
</center>
</body>
</html>

二、struts.xml

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

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

<struts>
    <!-- 支持动态调用 -->
    <constant name="struts.enable.DynamicMethodInvocation" value="true"/>
    <!-- 设置开发模式 -->
    <constant name="struts.devMode" value="true"/>

    <!-- 设置-->
    <constant name="struts.i18n.encoding" value="UTF-8"/>
    <constant name="struts.custom.i18n.resources" value="msg"/>
    <constant name="struts.locale" value="en_US"/>
    <package name="myPackage" extends="struts-default">
        <!-- 定义注册的 action-->
        <action name="userRegister" class="com.action.UserAction" method="register">
            <result name="success">register.jsp</result>
            <result name="input">register.jsp</result>
        </action>
        <!-- 字段检验 -->
        <action name="user*" class="com.action.UserAction" method="{1}">
            <result name="input">register.jsp</result>
        </action>
    </package>
</struts>

三、ACTION 类(UserAction.java)

package com.action;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import java.util.Arrays;
import java.util.Date;
import java.util.regex.Pattern;

/**
 * Created by hehe on 2017/3/3.
 */
public class UserAction extends ActionSupport {
    private String username = null;
    private String password = null;
    private String phone = null;
    private String repassword = null;
    private String gender = null;
    private Date birthday = null;
    private String[] hobby = null;
    private String address = null;


    public String getPhone() {
        return phone;
    }

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


    public String getRepassword() {
        return repassword;
    }

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

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public String[] getHobby() {
        return hobby;
    }

    public void setHobby(String[] hobby) {
        this.hobby = hobby;
    }

    public String getAddress() {
        return address;
    }

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

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

    public void validateRegister() {
       /* if ("".equals(username.trim()) || username == null) {
            this.addFieldError("username", "用户名不能为空");
        }*/
        if ("".equals(phone.trim()) || phone == null) {
            this.addFieldError("phone", "手机号不能为空");
        }else if (!Pattern.compile("^1[358]\\d{9}$").matcher(this.phone).matches()) {
            this.addFieldError("phone", "手机号格式不对");
        }
    }

    public String login() {
        // 返回值变量
        String strReturn = INPUT;
        // 业务逻辑判断
        if(this.username.equals("abc") && this.password.equals("123")) {
            strReturn = SUCCESS;
        }else {
            ActionContext.getContext().getSession().put("tip", "登录失败");
        }
        return strReturn;
    }

    public String register() {
        String strReturn  = SUCCESS;
        ActionContext.getContext().getSession().put("tip", "注册成功");

        return strReturn;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值