Struts2 学习之三 数据验证

来源:互联网 发布:linux系统怎么进入 编辑:程序博客网 时间:2024/06/05 18:42

本资料全属个人原创,如有不懂之处可以共同探讨,联系qq:924160580,也可以索要源代码

 

第三节数据验证

校验分 : 代码校验

                   框架校验 :字段优先和校验器优先

1执行流程

1 首先进行类型转换(如果是java内置数据类型,自动就转换了,这里说的是我们自定义的数据类型)

2 然后进行输入校验(执行 validate 方法)

3 如果在上述过程中出现了任何错误,都不会再去执行 execute

方法,会转向 struts.xml中该 action 的名为 input result 所对

应的页面。

2举例说明

 代码验证还木有整理 请见谅,稍后很快补全!!

 

3框架校验之字段校验

1)       在对应的action包下新建一个actionname-validation.xml文件

2)       由于校验是由xwork提供所以以下内容都在xwork核心包里面

3)       找到相应的xwork核心包

 

  

可以引用任意一个dtd文件

但是内容不太一样

 

 

 

4)       actionname-validation.xml进行配置

 

 

验证类型可以从该包中找到default.xml文件内容如下

 

Class就是对应的类从该类中可以找到param节点的name(注意这里是set方法不是该类的属性如doTirm)

 

5)       message节点有key这么个属性,其实这个是可有可无的!目的就是为了国际化,何为国际化?也就是页面支持多个国家的语言,这是如何实现,这需要语言包,不同的语言有一个语言包:并且名字格式固定enzh表示语言名称USCN表示国家名称:,中文的写法;其中

:可以随便叫但是各个语言包的相对应的键要相同和message节点的key的值也要相同

:就是message要显示的值,用不同的语言描述;

 

4国际化i18N

1)jdk对国际化的支持

  Local

A Locale object represents a specific geographical, political, or cultural region. An operation that requires a Locale to perform its task is called locale-sensitive and uses the Locale to tailor information for the user. For example, displaying a number is a locale-sensitive operation--the number should be formatted according to the customs/conventions of the user's native country, region, or culture.

  其中相关方法:

Locale[] locales = Locale.getAvailableLocales();//Returns an array of all installed locales

                   for (Locale locale : locales) {

                            //System.out.println(locale.getDisplayCountry() + " : " +  locale.getCountry());

                            System.out.println(locale.getDisplayLanguage() +" : "+locale.getLanguage());

                   }

ResourceBoundle类是对资源搜索:

Resource bundles contain locale-specific objects. When your program needs a locale-specific resource, a String for example, your program can load it from the resource bundle that is appropriate for the current user's locale. In this way, you can write program code that is largely independent of the user's locale isolating most, if not all, of the locale-specific information in resource bundles.

 其中相关方法:

ResourceBundle rb = ResourceBundle.getBundle("ganggang"); //ganggang是语言包的基名

                   String value = rb.getString("hello");

                   System.out.println(value);

xxx_xx_xx.porperties文件的中使用占位符的操作

hello=helle:{0}

提供支持的类MessageFormat

        

MessageFormat provides a means to produce concatenated messages in a language-neutral way. Use this to construct messages displayed for end users.

其中重要的方法是format();

 

//Locale locale = Locale.US;

                   Locale locale = Locale.CHINA;

                   ResourceBundle rb = ResourceBundle.getBundle("ganggang", locale);

                   String value = rb.getString("hello");

                   //String val = MessageFormat.format(value, new Object[]{"wufagang"});

                   String val = MessageFormat.format(value, new Object[]{"吴法刚"});

                   System.out.println(val);

 

 

 

5 框架校验之校验器优先校验

其实校验器优先和字段优先没有本质的区别

字段优先是在以字段为基准,在字段上添加校验

校验器优先是以校验器为基准,同一个字段上有几个验证条件就添加几个校验器

         <validator type="requiredstring">

               <param name="fieldName[A1] ">username[A2] </param>

               <message>username can not blank!</message>

     </validator>

   <validator type="stringlength">

     <param name="fieldName">username</param>

     <param name="maxLength">6</param>

     <param name="minLength">4</param>

     <message>length of username between ${minLength}and ${maxLength}</message>

   </validator>

6Struts2 框架校验(代码校验,框架校验(字段优先,校验器优先))执行的先后顺序

Struts2 框架校验执行的先后顺序:

1 首先执行校验框架(xml 文件)

2 执行自定义方法的校验方法(validateMyExecute

北京圣思园科技有限公司

3 执行 validate 方法

 

7 struts 2中异常的处理

1)其中自定义的异常类的写法如下:

 

/**

 *@author 吴法刚

 *@date 2013-7-26

 */

package com.ganggang.exception;

 

/**

 * @author Administrator

 *

 */

public class UserNameException extends Exception{

         private String message;

 

         /**

          * @param message

          */

         public UserNameException(String message) {

                   super(message);

                   this.message = message;

         }

 

         /**

          * @return the message

          */

         public String getMessage() {

                   return message;

         }

 

         /**

          * @param message the message to set

          */

         public void setMessage(String message) {

                   this.message = message;

         }

        

        

}

2)对于 struts.xml文件的结果配置来说,局部要优于全局。

3).  我们既可以在 Action中定义异常与结果,也可以定义全局的异常

与结果,局部总是优于全局的,如果定义成全局,那么可以为所

有的 Action 所公用,而局部的异常与结果只能被当前的 Action

独享,不能为其他 Action所共享

4)struts.xml中的配置

<exception-mapping result="usernamevalidate" exception="com.ganggang.exception.UserNameException"/>

                   <exception-mapping result="passwordvalidate" exception="com.ganggang.exception.PassWordExcepion"/>

                   <result name="usernamevalidate">/usererror.jsp</result>

                   <result name="passwordvalidate">/passworderror.jsp</result>

这是局部的配置全局的其实很简单不再赘述!!


 [A1]这个名字必须是fielName

 

 [A2]这个是字段名称

原创粉丝点击