Struts2之数据校验起名

[发现]各大IT公司的起名缘由这一个内容蛮有意思的,我就把她拉下来保存,讲述了微软,GOOGLE,APPLE,SONY等大公司名字起名的来由,看看这个霸气的名字是怎么来的,或者未来开个集团,会有所用啊,呵呵,不精晓ICEBIN算不算一个品牌,但名字虽好,一个集团的产品,文化,人才等才是真的有竞争力的元素。
得逞不单单是从起名起头的。很四个人有一致的名字,却走了不同的路。

 

Yahoo!

时间:2017-1-11 11:09

The word was invented by Jonathan Swift and used in his book Gulliver’s
Travels. It represents a person who is repulsive in appearance and
action and is barely human. Yahoo! founders Jerry Yang and David Filo
selected the name because they considered themselves yahoos.

 

Xerox

——Struts2提供的数据校验

The Greek root “xer” means dry. The inventor, Chestor Carlson, named his
product Xerox as it was dry copying, markedly different from the then
prevailing wet copying.

在付出中,请求参数是需要校验的:
    客户端:过滤正常用户的错误操作,通过JS代码完成
    服务器:Java代码

Sun
Microsystems

Struts2提供的校验是劳动器端校验。

Founded by four Stanford University buddies, Sun is the acronym for
Stanford University Network.

校验情势分为两种:
    1、手动校验(编码校验)
    2、配置校验(自动校验,基于Annotation,基于XML)

Sony

1、手动校验:
   
要求:Action类必须继承ActionSupport,需要重写validate()方法(是Validateable接口中的一个格局)

From the Latin word ‘sonus’ meaning sound, and ‘sonny’ a slang used by
Americans to refer to a bright youngster.

   
通过测试发现在Action中重写的validate()方法执行了,并且是在乞请处理方法execute()方法在此之前实施的。

Sony

    对于Struts2提供的校验,也是经过拦截器实现的:

“Systems, Applications, Products in Data Processing”, formed by four
ex-IBM employees who used to work in the ‘Systems/Applications/Projects’
group of IBM.

        起名 1

Red Hat

    问题:咋样在validate()方法中储存校验错误音讯?
        在validate()方法中:this.addFieldError(String name, String
value)
        当集合不为空时,转向错误页面。

Company founder Marc Ewing was given the Cornell lacrosse team cap (with
red and white stripes) while at college by his grandfather. He lost it
and had to search for it desperately. The manual of the beta version of
Red Hat Linux had an appeal to readers to return his Red Hat if found by
anyone!

    问题:如何在页面上赢得错误消息(在input视图中)?
        导入标签库:<%@ taglib prefix=”s” uri=”/struts-tags” %>
        使用标签:
            显示所有错误音讯:<s:fielderror/>
            显示特定的错误消息:<s:fielderror fieldName=”name”>
name是this.add菲尔德(Field)Error()方法中保留的name值。

Oracle

   
问题:在同一个Action中存在三个请求处理办法(regist()、login()),有些措施是急需校验的,有些措施是不需要校验的,如何处理?
        定义一个称号为:validate+请求处理情势名。
        例如:请求处理措施:regist,那么校验的方法名就是:validateRegist(),只针对regist请求方法举办校验。
        validateRegist(){ …
}:请求regist()方法时会被调用,而请求login()方法时不会被调用。
       
不是由此配备文件也不是通过声明,而是通过一种“约定标准”来起名的。

Larry Ellison and Bob Oats were working on a consulting project for the
Central Intelligence Agency (CIA). The code name for the project was
called Oracle (the CIA saw this as the system to give answers to all
questions or something such).

       
当同时设有validateRegist()方法和validate()方法时,会先执行validateRegist()再实施validate(),可以将一些国有校验放到validate()方法中。

Motorola

    示例代码:

Founder Paul Galvin came up with this name when his company started
manufacturing radios for cars. The popular radio company at the time was
called Victrola.

        public class UserAction extends ActionSupport implements
ModelDriven {

Microsoft

            private User user = new User();

It was coined by Bill Gates to represent the company that was devoted to
MICROcomputer SOFTware. Originally christened Micro-Soft, the ‘-‘ was
removed later on.

 

Lotus

            public Object getModel() {

Mitch Kapor got the name for his company from the lotus position or
‘padmasana.’ Kapor used to be a teacher of Transcendental Meditation of
Maharishi Mahesh Yogi.

                return this.user;

Intel

            }

Bob Noyce and Gordon Moore wanted to name their new company ‘Moore
Noyce’ but that was already trademarked by a hotel chain, so they had to
settle for an acronym of INTegrated ELectronics.

            public void validateRegist() { 
                ….
            } 

Hewlett-Packard

 

Bill Hewlett and Dave Packard tossed a coin to decide whether the
company they founded would be called Hewlett-Packard or Packard-Hewlett.

            @Override

Hotmail

            public void validate() {

Founder Jack Smith got the idea of accessing email via the web from a
computer anywhere in the world. When Sabeer Bhatia came up with the
business plan for the mail service, he tried all kinds of names ending
in ‘mail’ and finally settled for Hotmail as it included the letters
“html” – the programming language used to write web pages. It was
initially referred to as HoTMaiL with selective upper casings.

                System.out.println(“validate…”);

Google

                if (user.getUsername() == null ||
user.getUsername().trim().length() == 0) {

The name started as a jockey boast about the amount of information the
search-engine would be able to search. It was originally named ‘Googol’,
a word for the number represented by 1 followed by 100 zeros. After
founders – Stanford graduate students Sergey Brin and Larry Page
presented their project to an angel investor, they received a cheque
made out to ‘Google

                    // 表明用户名为空

Cisco

                    this.add菲尔德(Field)Error(“username.meeage”,
“用户名不能够为空”);

The name is not an acronym but an abbreviation of San Francisco. The
company’s logo reflects its San Francisco name heritage. It represents a
stylized Golden Gate Bridge.

                }

Apple Computers

 

Favourite fruit of founder Steve Jobs. He was three months late in
filing a name for the business, and he threatened to call his company
Apple Computers if the other colleagues didn’t suggest a better name by
5 o’clock.

                if (user.getPassword() == null ||
user.getPassword().trim().length() == 0) {

Apache

                    // 密码为空

It got its name because its founders got started by applying patches to
code written for NCSA’s httpd daemon. The result was ‘A PAtCHy’ server –
thus, the name Apache.

                    this.add菲尔德(Field)(Field)Error(“password.message”,
“密码不可以为空”);

Adobe

                }

The name came from the river Adobe Creek that ran behind the house of
founder John Warnock.

            }

        }

    手动校验的流水线
       
1)类型转换器对请求参数执行类型转换,并把转换后的值赋给Action中的属性。
       
2)假诺在履行类型转换的进程中出现非凡,系统会将十分信息保存到ActionContext中,conversionError拦截器将异常音讯打包到fieldErrors中,然后执第3步,假诺没有出现类型转换非凡,则直接进入第3步。

        3)系统经过反射调用Action中的validateXxx()方法,Xxx为艺术名。
        4)调用Action中的validate()方法。
       
5)经过地点4步,假诺系统中的fieldErrors存在错误音信(即存放错误信息的成团的size大于0),系统将自动请求转发至input视图,要是系统中的fieldErrors没有此外错误信息,系统将执行Action中的处理措施。

2、配置校验(XML格式)
   
也叫作Struts2的校验框架,Struts2框架已经完结了校验操作(完成了许多校验方法),而大家在利用时,只需要将它们调用就足以了。(需要通过配备文件)

    配置校验器之后,Action中不需要再写validate()方法了。

    要求:Action类必须继承ActionSupport类

    问题:配置文件怎么样安排?
        1)配置文件的岗位
            *   XML文件要与Action类在同一个包下
        2)配置文件的名称
            *   Action类名-validation.xml
        3)配置文件的羁绊
            *  
在xwork-core-2.3.15.1.jar包下的xwork-validator-1.0.3.dtd约束文件中检索

            *   <!DOCTYPE validators PUBLIC “-//Apache Struts//XWork
Validator 1.0.3//EN”
http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd“>

        4)配置文件的书写
            *   根元素:<validators>
                >   子元素:<field>(单值校验)
                    <field
name=”属性名称”></field>,指定Action中要校验的性能,name属性指定将被声明的表单字段的名字。
                    驱动模型属性下仍然只写属性名,不写对象名。
                    >  
子元素:<field-validator type=”校验器”></fieldvalidator>
                        指定校验器,type表示指定验证规则。
                        >  
子元素:<message>用户名无法为空</message>
                            用于定义错误信息
                        >  
子元素:<param name=””>值</param>:
                            用于指定校验器参数(属性)的。

    问题:校验器有什么样?
       
在这多少个路子中寻觅:/com/opensymphony/xwork2/validator/validators/default.xml

        <validators>

            <validator name=”required”
class=”com.opensymphony.xwork2.validator.validators.RequiredFieldValidator”/>

            <validator name=”requiredstring”
class=”com.opensymphony.xwork2.validator.validators.RequiredStringValidator”/>

            <validator name=”int”
class=”com.opensymphony.xwork2.validator.validators.IntRangeFieldValidator”/>

            <validator name=”long”
class=”com.opensymphony.xwork2.validator.validators.LongRangeFieldValidator”/>

            <validator name=”short”
class=”com.opensymphony.xwork2.validator.validators.ShortRangeFieldValidator”/>

            <validator name=”double”
class=”com.opensymphony.xwork2.validator.validators.DoubleRangeFieldValidator”/>

            <validator name=”date”
class=”com.opensymphony.xwork2.validator.validators.DateRangeFieldValidator”/>

            <validator name=”expression”
class=”com.opensymphony.xwork2.validator.validators.ExpressionValidator”/>

            <validator name=”fieldexpression”
class=”com.opensymphony.xwork2.validator.validators.FieldExpressionValidator”/>

            <validator name=”email”
class=”com.opensymphony.xwork2.validator.validators.EmailValidator”/>

            <validator name=”url”
class=”com.opensymphony.xwork2.validator.validators.URLValidator”/>

            <validator name=”visitor”
class=”com.opensymphony.xwork2.validator.validators.VisitorFieldValidator”/>

            <validator name=”conversion”
class=”com.opensymphony.xwork2.validator.validators.ConversionErrorFieldValidator”/>

            <validator name=”stringlength”
class=”com.opensymphony.xwork2.validator.validators.StringLengthFieldValidator”/>

            <validator name=”regex”
class=”com.opensymphony.xwork2.validator.validators.RegexFieldValidator”/>

            <validator name=”conditionalvisitor”
class=”com.opensymphony.xwork2.validator.validators.ConditionalVisitorFieldValidator”/>

        </validators>

        *   required:必填校验器,要求被校验的性能值不可能为null。
        *  
requiredstring:必填校验器,要求被校验的属性值不可能为null,并且长度大于0,默认情状下会去除字符串前后空格。
            >  
在reqyuredstring所对应的类中,trim默认为true,假诺想要修改其值,可以在校验器下添加子元素:<param
name=”trim”>false</param>
            >   public class RequiredStringValidator extends
FieldValidatorSupport {

                     private boolean trim = true;

                     ……

                 } 

        *  
stringlength:字符串长度校验器要求被校验的属性值长度必须在指定范围内,否则校验失利,minLength参数指定最小长度,maxLength参数指定最大尺寸,trim参数指定校验field从前是不是去除字符串前后空格。
        *  
regex:正则表明式校验器,检查被校验的属性值是否匹配一个正则表明式,expression参数指定正则表达式,caseSensitive参数指定举办正则表达式匹配时,是否区分轻重缓急写,默认为true。
        *  
int:整数校验器,要求field的整数值必须在指定范围内,min指定最小值,max指定最大值。
        *  
double:双精度浮点数校验器,要求field的双精度浮点数必须在指定范围内,main指定最小值,max指定最大值。
        *  
fieldexpression:字段OGNL说明式校验器,要求field满意一个OGNL表明式,expression参数指定OGNL表明式与该逻辑表明式基于ValueStack举办求值,重回true时校验通过,否则失败。
        *  
email:邮件地址校验器,要求如若被校验的属性值为非空,必须是合法的邮件地址。
        *  
url:网址校验器,要求即便被校验的属性值非空,必须是合法的url地址。
        *  
date:日期校验器,不是校验日期格式,而是务求field的日期值必须在指定范围内,min指定最小值,max指定最大值。

    示例代码:

        <?xml version=”1.0″ encoding=”UTF-8″?>

        <!DOCTYPE validators PUBLIC “-//Apache Struts//XWork
Validator 1.0.3//EN”
http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd“>

 

        <validators>

            <!– 对username属性进行校验 –>

            <field name=”username”>

                <!– 指定username不可能为空 –>

                <field-validator type=”requiredstring”>

                    <!– 设置错误音讯 –>

                    <message>用户名不能够为空2</message>

                </field-validator>
 

                <!– 长度校验,规定用户名必须在6-10位期间 –>

                <field-validator type=”stringlength”>

                    <!–
maxLength、minLength是stringlength对应类中的属性 –>

                    <param name=”maxLength”>10</param>

                    <param name=”minLength”>6</param>

                    <!– 想取得属性值,可以使用ognl表达式 –>

                    <message>用户名必须在${minLength}-${maxLength}位期间</message>

                </field-validator>

            </field>
 

            <!– 对age举办校验 –>

            <field name=”age”>

                <!– 规定age必须在10-40之间 –>

                <field-validator type=”int”>

                    <param name=”min”>10</param>

                    <param name=”max”>40</param>

                    <message>年龄必须在${min}-${max}之间</message>

                </field-validator>

            </field>

 

            <!– 对birthday举办校验 –>

            <field name=”birthday”>

                <field-validator type=”date”>

                    <param name=”max”>2000-1-1</param>

                    <param name=”min”>1000-1-1</param>

                    <message>生日必须在${min}-${max}之间</message>

                </field-validator>

            </field>

 

            <!– 校验邮箱格式 –>

            <field name=”email”>

                <field-validator type=”email”>

                    <message>邮箱格式不得法</message>

                </field-validator>

            </field>

 

            <!– url格式校验,要求是一个不易的url网络路径 –>

    <field name=”url”>

                <field-validator type=”url”>

                    <message>url格式不得法</message>

                </field-validator>

            </field>

 

            <!– 使用正则表达式验证tel –>

            <field name=”tel”>

                <field-validator type=”regex”>

                    <!– 188开头,长度11位 –>

                    <!– 在对应类中有regexExpression属性 –>

                    <!– CDATA表示该区域字符串不会被分析 –>

                    <param
name=”regexExpression”><![CDATA[^188[0-9]{8}$]]></param>

                    <message>电话号码必须是188上马,长度11位</message>

                </field-validator>

            </field>

 

            <!– 判断密码是否一致 –>

            <field name=”repassword”>

                <field-validator type=”fieldexpression”>

                    <param
name=”expression”><![CDATA[(password==repassword)]]></param>

                    <message>密码不一样</message>

                </field-validator>

            </field>

        </validators>

   
问题:通过安排校验,咋样处理在同一个Action中存在四个请求处理措施校验问题?
        有两个点子,其中一些方法不需要校验,如何兑现?
        只需要将校验XML文件名称修改即可:
            针对Action的校验配置文件:Action类名-validation.xml
            现在只期待对Action类中某一个方法举行校验:
               
Action类名-action名称(action的name属性)-validation.xml

   
    当Action提供了ActionClassName-validation.xml和ActionClassName-ActionName-validation.xml二种规则的校验文件时,系统会遵守以下顺序查找校验文件:
        1)ActionClassName-validation.xml
        2)ActionClassName-ActionName-validation.xml

        先找大的,再找小的。
   
系统查找到第一个校验文件时还会持续搜寻前边的校验文件,当搜索到具有校验文件时,会把校验文件里的有所校验规则汇总,然后所有用于拍卖措施的校验,尽管多少个校验文件中指定的校验规则争持,则未来面的公文中的校验规则为准。

   
当Action继承了另一个Action,父类Action的校验文件会先被寻找到,假如UserAction继承了BaseAction,UserAction在struts.xml中的配置如下:
        <action name=”user” class=”com.wyc.action.UserAction”
method=”execute”>
            ……
        </action>
   
访问下面名为User的Action,系统先找找到BaseAction-validation.xml,BaseAction-user-validation.xml,然后搜索到UserAction-validation.xml,UserAction-user-validation.xml,校验规则是这六个公文的总和。

    编写校验文件时,不能出现指示音信:
       
在编写ActionClassName-validation.xml校验文件时,假使出现持续协助音信,可以按下边情势解决:

windwos->preferences->myeclipse->files and
editors->xml->xmlcatalog点“add”,在出现的窗口中的location中选“File
system”,然后在xwork-2.1.2解压目录的src\java目录中拔取xwork-validator-1.0.3.dtd,回到设置窗口的时候不要急着关闭窗口,应把窗口中的Key
Type改为URI
。Key改为http://www.opensymphony.com/xwork/xwork-validator-1.0.3.dtd

 

3、自定义校验规则(了然)
    步骤一:自定义校验器,必须实现Validator接口
        经常自定义校验器继承ValidatorSupport和菲尔德(Field)ValidatorSupport
        *  
ValidatorSipport针对不是一个的输入字段(比如比较两个密码是否一律)
        *  
Field(Field)ValidatorSupport针对一个输入字段(比如判断用户名是否为空)

    步骤二:
        在工程的srx下创立validators.xml文件
        引入xword-core-2.3.7.jar中的xword-validator-config-1.0.dtd

    步骤三:使用校验器
        在Action所有包中创建:Action类名-validation.xml

    实际付出中很少用到自定义校验器。

——总结

1、Struts2呼吁参数校验
    *   手动编码校验(精通)
        >   Action继承ActionSupport类
        >   重写validate()方法
        >   使用addFieldError(String name, String msg)添加错误音讯
        >   在页面中使用<s:fielderror>显示错误消息
    *   XML的布置校验
        >   地点:与Action类在平等包下
        >   名称:ActionClassName-validation.xml
        >   约束:在xwork-core包中的约束文件中检索
        >  
书写:校验器在xwork-core包中的validator-validators包中的default.xml文件中寻觅

    *   校验器的介绍

 

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图