Chinaunix首页 | 论坛 | 博客
  • 博客访问: 335282
  • 博文数量: 96
  • 博客积分: 2041
  • 博客等级: 大尉
  • 技术积分: 1080
  • 用 户 组: 普通用户
  • 注册时间: 2012-01-20 14:08
文章分类

全部博文(96)

文章存档

2015年(2)

2013年(1)

2012年(93)

分类: 系统运维

2012-01-21 21:25:48

Struts2的校验框架(有效的XML文件)。具体来说分为字段优先校验器(field)与校验器优先校验器(validator)。

xml命名格式如图:

1

RegisterAction-validation.xml内容:


        "-//Apache Struts//XWork Validator 1.0.2//EN"
        ">

   
       
            username can't be blank!
       

       
            4
            6
            length should between 4 and 6

    //或者用引用的方式 length should between ${minLength} and ${maxLength}
       
   

//requiredstring是字符串类型,required是除字符串以外的类型

     age can’t be blank


    10

    40

    age should be between …..

 


  
   birthday can't be blank!
  

  
   2005-1-1
   2007-12-31
   birthday should be between ${min} and ${max}
  

 

其中头文件可通过以下获得

近日

用这种xml校验可以取代原先用validate()编码进行校验:

RegisterAction.java

package com.shengsiyuan.struts2;

import java.util.Calendar;
import java.util.Date;

import com.opensymphony.xwork2.ActionSupport;
import com.sun.faces.taglib.html_basic.GraphicImageTag;

public class RegisterAction extends ActionSupport
{
    private String username ;
    private String password  ;
    private String repassword ;
    private int age ;
    private Date birthday ;
    private Date graduate ;
    public Date getGraduate()
    {
        return graduate;
    }
    public void setGraduate(Date graduate)
    {
        this.graduate = graduate;
    }
    public String getUsername()
    {
        return username;
    }
    public void setUsername(String username)
    {
        System.out.println("00");
        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 int getAge()
    {
        return age;
    }
    public void setAge(int age)
    {
        this.age = age;
    }
    public Date getBirthday()
    {
        return birthday;
    }
    public void setBirthday(Date birthday)
    {
        this.birthday = birthday;
    }

    @Override
    public String execute() throws Exception
    {
        return "SUCCESS" ;
    }
//    public void validate()
//    {
//        if(null == username || username.length() < 4 || username.length() >6)//取不合法的情况进行处理
//        {
//            this.addActionError("username invalid") ;
//            this.addFieldError("username", "username invalid in field") ;
//        }
//        if(null == password || password.length() <4 || password.length() >6 )
//        {
//            this.addActionError("password invalid") ;
//        }
//        else if(null == repassword ||repassword.length() <4 ||repassword.length() >6)
//        {
//            this.addActionError("repassword invalid") ;
//        }
//        else if(!password.equals(repassword))
//        {
//            this.addActionError("2ci buyi zhi") ;
//        }
//        if(age < 10 || age >50)
//        {
//            this.addActionError("age is not..") ;
//        }
//        if(null == birthday)
//        {
//            this.addActionError("birthday..") ;
//           
//        }
//        if(null == graduate)
//        {
//            this.addActionError("graduate...") ;
//        }
//        if(null != birthday && null !=graduate)
//        {
//            Calendar c1 = Calendar.getInstance() ;
//            c1.setTime(birthday) ;
//           
//            Calendar c2 = Calendar.getInstance() ;
//            c2.setTime(graduate) ;
//            if(!c1.before(c2)) //正确的取反,即找错误的
//            {
//                this.addActionError("birthday...") ;
//            }
//        }
//       
//    }
//
}

校验器如图:

2

也可以用 key属性设置:


       
            username can't be blank!
       

       
            4
            6
           

        
   

 

对于国际化的资源文件,其命名规则是:package_语言名_国家名,比如package_zh_CN,package_en_US

国际化举例:

其中shengsiyuan.properties是默认情况(即找不到US,CN……时使用)

一下是3个国际化例子:

I18NTest1

package com.shengsiyuan.i18n;

import java.util.Locale;

public class I18NTest1
{
 public static void main(String[] args)
 {
  Locale[] locales = Locale.getAvailableLocales();

  for (Locale locale : locales)
  {
   System.out.println(locale.getDisplayCountry() + " : "
     + locale.getCountry());
   
//   System.out.println(locale.getDisplayLanguage() + " : " + locale.getLanguage());
  }
 }
}

I18NTest2

package com.shengsiyuan.i18n;

import java.util.Locale;
import java.util.ResourceBundle;

public class I18NTest2
{
 public static void main(String[] args)
 {
  System.out.println(Locale.getDefault());
  
  ResourceBundle bundle = ResourceBundle.getBundle("shengsiyuan", Locale.FRANCE);
  
  String value = bundle.getString("hello");
  
  System.out.println(value);
 }
}
I18NTest3

package com.shengsiyuan.i18n;

import java.text.MessageFormat;
import java.util.Locale;
import java.util.ResourceBundle;

public class I18NTest3
{
 public static void main(String[] args)
 {
  Locale locale = Locale.CHINA;
  
  ResourceBundle bundle = ResourceBundle.getBundle("shengsiyuan", locale);
  
  String value = bundle.getString("hello");
  
  String result = MessageFormat.format(value, new Object[]{"圣思园"});
  
  System.out.println(result);
 }
}

阅读(735) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~