• struts2中用xml配置文件去验证填写信息


    xml名字是这样的 actionName-validation.xml 每个action 对应一个xml文件

    xml文件和action放在同一个包下

    后台验证用户输入是否符合格式要求,不符合,提交后返回对应提示。

    //xml文件

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE validators PUBLIC
    "-//OpenSymphony Group//XWork Validator 1.0.2//EN"
    "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
    <validators>
    <validator type="requiredstring">
    <param name="fieldName">username</param>
    <param name="trim">true</param>
    <message>必须输入用户名</message>
    </validator>

    <validator type="regex">
    <param name="fieldName">username</param>
    <param name="expression"><![CDATA[(w{6,20})]]></param>
    <message>用户名长度必须为6至20之间</message>
    </validator>

    <validator type="requiredstring">
    <param name="fieldName">password</param>
    <param name="trim">true</param>
    <message>必须输入密码</message>
    </validator>

    <validator type="regex">
    <param name="fieldName">password</param>
    <param name="expression"><![CDATA[(w{6,20})]]></param>
    <message>密码长度必须为6至20之间</message>
    </validator>

    <validator type="requiredstring">
    <param name="fieldName">repassword</param>
    <param name="trim">true</param>
    <message>必须输入确认密码</message>
    </validator>

    <validator type="regex">
    <param name="fieldName">repassword</param>
    <param name="expression"><![CDATA[(w{6,20})]]></param>
    <message>确认密码长度必须为6至20之间</message>
    </validator>

    <validator type="fieldexpression">
    <param name="fieldName">repassword</param>
    <param name="expression"><![CDATA[(repassword==password)]]></param>
    <message>密码和确认密码必须一致</message>
    </validator>

    <validator type="requiredstring">
    <param name="fieldName">nickname</param>
    <param name="trim">true</param>
    <message>必须输入昵称</message>
    </validator>

    <validator type="regex">
    <param name="fieldName">nickname</param>
    <param name="expression"><![CDATA[([u4e00-u9fa5]{2,20})]]></param>
    <message>昵称长度必须为2至20之间</message>
    </validator>

    <validator type="requiredstring">
    <param name="fieldName">answer</param>
    <param name="trim">true</param>
    <message>必须输入密码保护答案</message>
    </validator>

    <validator type="regex">
    <param name="fieldName">answer</param>
    <param name="expression"><![CDATA[([u4e00-u9fa5]{5,50})]]></param>
    <message>密码保护答案长度必须为5至50之间</message>
    </validator>
    </validators>

     //HTML文件

    <table width="1000" cellpadding="0" cellspacing="0" border="0">
    <tr height="100">
    <td colspan="2">&nbsp;</td>
    </tr>
    <tr>
    <td align="right" width="300" valign="top"><img src="image/register.gif"></td>
    <td width="20">&nbsp;</td>
    <td valign="top">
    <table width="600" border="1" cellpadding="0" cellspacing="0" style="background-color:#FFFFFF;BORDER-RIGHT:#cccccc 1px solid; BORDER-TOP:#cccccc 1px solid; BORDER-LEFT:#cccccc 1px solid; BORDER-BOTTOM:#cccccc 1px solid; BORDER-COLLAPSE:collapse">
    <tr height="30">
    <td colspan="3" align="center" background="image/main/l-bg1.jpg"><font color="#ffff66"><b>新博客注册</b></font></td>
    </tr>
    <tr height="30">
    <td align="right" width="160">帐号&nbsp;</td>
    <td><input type="text" name="username"/>*</td>

    <!--username 就是xml文件中对应的  <param name="fieldName">username</param> 名字-->
    <td><s:fielderror><s:param>username</s:param></s:fielderror></td>
    </tr>
    <tr height="30">
    <td align="right">密码&nbsp;</td>
    <td><input type="password" name="password"/>*</td>
    <td><s:fielderror><s:param>password</s:param></s:fielderror></td>
    </tr>
    <tr height="30">
    <td align="right">确认密码&nbsp;</td>
    <td><input type="password" name="repassword"/>*</td>
    <td><s:fielderror><s:param>repassword</s:param></s:fielderror></td>
    </tr>
    <tr height="30">
    <td align="right">昵称&nbsp;</td>
    <td><input type="text" name="nickname"/>*</td>
    <td><s:fielderror><s:param>nickname</s:param></s:fielderror></td>
    </tr>
    <tr height="30">
    <td align="right">密码保护问题&nbsp;</td>
    <td>
    <select name="question">
    <option value="你的生日">你的生日</option>
    <option value="你的家乡">你的家乡</option>
    <option value="你最喜欢的食物">你最喜欢的食物</option>
    </select>*
    </td>
    <td><s:fielderror><s:param>question</s:param></s:fielderror></td>
    </tr>
    <tr height="30">
    <td align="right">密码保护的答案&nbsp;</td>
    <td><input type="text" name="answer"/>*</td>
    <td><s:fielderror><s:param>answer</s:param></s:fielderror></td>
    </tr>
    <tr height="30">
    <td colspan="3" align="center"><input type="submit" value="提交"/>
    <input type="reset" value="重置"/>
    </td>
    </tr>
    </table>
    </td>
    </tr>
    </table>

    //action代码

    public class Register extends ActionSupport{

    private String username;
    private String password;
    private String repassword;
    private String nickname;
    private String question;
    private String answer;
    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 String getRepassword() {
    return repassword;
    }
    public void setRepassword(String repassword) {
    this.repassword = repassword;
    }
    public String getNickname() {
    return nickname;
    }
    public void setNickname(String nickname) {
    this.nickname = nickname;
    }
    public String getQuestion() {
    return question;
    }
    public void setQuestion(String question) {
    this.question = question;
    }
    public String getAnswer() {
    return answer;
    }
    public void setAnswer(String answer) {
    this.answer = answer;
    }

    public String execute() throws Exception {
    System.out.println(this.username);
    System.out.println(this.password);
    System.out.println(this.repassword);
    System.out.println(this.nickname);
    System.out.println(this.question);
    System.out.println(this.answer);
    return SUCCESS;
    }
    }

  • 相关阅读:
    Asp.net 2.0 动态加载其他子目录用户控件问题
    C# 实现 类似Android的Toast
    C#编译成x86与x64区别
    C#封送回调函数和委托
    软键盘 WinCE Mobile
    JNI由C编译方式改成C++编译方式
    JNI itoa 不能使用的问题
    没有Root 时Data/Data文件夹不可见
    javah用法
    接口测试总结
  • 原文地址:https://www.cnblogs.com/laotan/p/3661581.html
Copyright © 2020-2023  润新知