• 吴裕雄--天生自然轻量级JAVA EE企业应用开发Struts2Sping4Hibernate整合开发学习笔记:struts2基于注解的输入校验


    <?xml version="1.0" encoding="GBK"?>
    <project name="struts" basedir="." default="">
        <property name="dist" value="classes"/>
        <property name="src" value="src"/>
        
        <path id="classpath">
            <fileset dir="lib">
                <include name="*.jar"/>
            </fileset>
            <pathelement path="${dist}"/>
        </path>
    
        <target name="compile" description="Compile all source code">
            <delete dir="${dist}"/>
            <mkdir dir="${dist}"/>
            <copy todir="${dist}">
                <fileset dir="${src}">
                    <exclude name="**/*.java"/>
                </fileset>        
            </copy>
            <javac destdir="classes" debug="true" includeantruntime="yes"
                deprecation="false" optimize="false" failonerror="true">
                <src path="${src}"/>
                <classpath refid="classpath"/>
            </javac>
        </target>
    
    </project>
    <?xml version="1.0" encoding="GBK"?>
    
    <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
        http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
        <!-- 定义Struts 2的核心Filter -->
        <filter>
            <filter-name>struts2</filter-name>
            <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
        </filter>
        <!-- 让Struts 2的核心Filter拦截所有请求 -->
        <filter-mapping>
            <filter-name>struts2</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
    </web-app>
    <%--
    网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
    author  yeeku.H.lee kongyeeku@163.com
    version  1.0
    Copyright (C), 2001-2016, yeeku.H.Lee
    This program is protected by copyright laws.
    Program Name:
    Date: 
    --%>
    
    <%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %>
    <%@taglib prefix="s" uri="/struts-tags"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        
    <title>请输入您的注册信息</title>
        <s:head/>
    
    </head>
    
    
    <body>
    <h2>请输入您的注册信息</h2>
    <s:fielderror/>
    <s:form action="regist">
        <s:textfield name="name" label="用户名"/>
        <s:textfield name="pass" label="密码"/>
        <s:textfield name="age" label="年龄"/>
        <s:textfield name="birth" label="生日"/>
        <s:submit value="注册"/>
    </s:form>
    
    </body>
    
    </html>
    <%--
    网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
    author  yeeku.H.lee kongyeeku@163.com
    version  1.0
    Copyright (C), 2001-2016, yeeku.H.Lee
    This program is protected by copyright laws.
    Program Name:
    Date: 
    --%>
    
    <%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %>
    <%@taglib prefix="s" uri="/struts-tags"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        
    <title>校验成功</title>
    
    </head>
    
    
    <body>
    <h3>校验成功</h3>
        用户名:<s:property value="name"/><br/>
        密码:<s:property value="pass"/><br/>
        年龄:<s:property value="age"/><br/>
        生日:<s:property value="birth"/><br/>
    
    </body>
    
    </html>
    <?xml version="1.0" encoding="GBK"?>
    <!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
        "http://struts.apache.org/dtds/struts-2.3.dtd">
    <struts>
        <constant name="struts.i18n.encoding" value="GBK"/>
    
        <package name="lee" extends="struts-default">
        <!-- 用户注册的Action -->
        <action name="regist" class="org.crazyit.app.action.RegistAction">
            <!-- 类型转换失败、输入校验失败,转入input对应的视图 -->
            <result name="input">/WEB-INF/content/registForm.jsp</result>
            <result>/WEB-INF/content/show.jsp</result>
        </action>
            <action name="*">
                <result>/WEB-INF/content/{1}.jsp</result>
            </action>
        </package>
    </struts>
    package org.crazyit.app.action;
    
    import com.opensymphony.xwork2.ActionSupport;
    import com.opensymphony.xwork2.validator.annotations.*;
    
    import java.util.Date;
    /**
     * Description:
     * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
     * <br/>Copyright (C), 2001-2016, Yeeku.H.Lee
     * <br/>This program is protected by copyright laws.
     * <br/>Program Name:
     * <br/>Date:
     * @author  Yeeku.H.Lee kongyeeku@163.com
     * @version  1.0
     */
    public class RegistAction extends ActionSupport
    {
        // 定义4个成员变量封装请求参数
        private String name;
        private String pass;
        private int age;
        private Date birth;
    
        // name的setter和getter方法
        // 使用注解指定必填、正则表达式两个校验规则
        @RequiredStringValidator(key = "name.requried"
            , message = "")
        @RegexFieldValidator(regex = "\w{4,25}"
            ,key = "name.regex" , message = "")
        public void setName(String name)
        {
            this.name = name;
        }
        public String getName()
        {
            return this.name;
        }
    
        // pass的setter和getter方法
        @RequiredStringValidator(key = "pass.requried"
            ,message = "")
        @RegexFieldValidator(regex = "\w{4,25}"
            ,key = "pass.regex" ,message = "")
        public void setPass(String pass)
        {
            this.pass = pass;
        }
        public String getPass()
        {
            return this.pass;
        }
    
        // age的setter和getter方法
        @IntRangeFieldValidator(message = ""
            , key = "age.range", min = "1"
            , max = "150")
        public void setAge(int age)
        {
            this.age = age;
        }
        public int getAge()
        {
            return this.age;
        }
    
        // birth的setter和getter方法
        @DateRangeFieldValidator(message = ""
            , key = "birth.range", min = "1900/01/01"
            , max = "2050/01/21")
        public void setBirth(Date birth)
        {
            this.birth = birth;
        }
        public Date getBirth()
        {
            return this.birth;
        }
    }
    #违反用户名必须输入的提示信息
    name.requried=您必须输入用户名!
    #违反用户名必须匹配正则表达式的提示信息
    name.regex=您输入的用户名只能是字母和数字,且长度必须在4到25之间!
    #违反密码必须输入的提示信息
    pass.requried=您必须输入密码!
    #违反密码必须匹配正则表达式的提示信息
    pass.regex=您输入的密码只能是字母和数字,且长度必须在4到25之间!
    #违反年龄必须在指定范围的提示信息
    age.range=您的年龄必须在${min}和${max}之间!
    #违反生日必须在指定范围的提示信息
    birth.range=您的生日必须在${min}和${max}之间!
    #u8fddu53cdu7528u6237u540du5fc5u987bu8f93u5165u7684u63d0u793au4fe1u606f
    name.requried=u60a8u5fc5u987bu8f93u5165u7528u6237u540d!
    #u8fddu53cdu7528u6237u540du5fc5u987bu5339u914du6b63u5219u8868u8fbeu5f0fu7684u63d0u793au4fe1u606f
    name.regex=u60a8u8f93u5165u7684u7528u6237u540du53eau80fdu662fu5b57u6bcdu548cu6570u5b57uff0cu4e14u957fu5ea6u5fc5u987bu57284u523025u4e4bu95f4!
    #u8fddu53cdu5bc6u7801u5fc5u987bu8f93u5165u7684u63d0u793au4fe1u606f
    pass.requried=u60a8u5fc5u987bu8f93u5165u5bc6u7801!
    #u8fddu53cdu5bc6u7801u5fc5u987bu5339u914du6b63u5219u8868u8fbeu5f0fu7684u63d0u793au4fe1u606f
    pass.regex=u60a8u8f93u5165u7684u5bc6u7801u53eau80fdu662fu5b57u6bcdu548cu6570u5b57uff0cu4e14u957fu5ea6u5fc5u987bu57284u523025u4e4bu95f4!
    #u8fddu53cdu5e74u9f84u5fc5u987bu5728u6307u5b9au8303u56f4u7684u63d0u793au4fe1u606f
    age.range=u60a8u7684u5e74u9f84u5fc5u987bu5728${min}u548c${max}u4e4bu95f4!
    #u8fddu53cdu751fu65e5u5fc5u987bu5728u6307u5b9au8303u56f4u7684u63d0u793au4fe1u606f
    birth.range=u60a8u7684u751fu65e5u5fc5u987bu5728${min}u548c${max}u4e4bu95f4!
  • 相关阅读:
    阿里妈妈又做了新工具,帮你把 Vue2 代码改成 Vue3 的
    gulp安装出错
    js操作select
    linux基本命令说明参数
    Linux基础——vim编辑器的使用
    常见报错——Uncaught TypeError: document.getElementsByClassName(...).addEventListener is not a function
    IE浏览器hack方法总结之条件注释判断
    浏览器兼容
    安好
    jQuery调整表列(左右拉动调整列宽)插件__colResizable,动态列如何使用
  • 原文地址:https://www.cnblogs.com/tszr/p/12366664.html
Copyright © 2020-2023  润新知