struts2 自定义类型转换器


原理详述

  Struts2自定义类型转换器分为局部类型转换器和全局类型转换器

  (1)局部类型转换器

  如果页面传来一个参数reg.action?birthday=2010-11-12到后台action,然后属性用date类型是可以接收到的,但是如果传的是20101112这样类型的字符串,用date类型是获取不到,并且会出现错误的,struts2提供了一种类型转换器供我们使用。

  以下为局部类型转换器的开发步骤

  a.首先要写一个类来继承DefaultTypeConverter

  b.然后覆盖convertValue这个方法,在里面进行数据转型

  c.在action类所在的包下放置ActionClassName-conversion.properties文件,ActionClassName是类名,后面的-conversion.properties是固定的写法,

  如:HelloWorldAction-conversion.properties

  d.Properties文件里面的内容为:属性名称=类型转换器的全类名(既包名.类名)

  如:birthday=com.ljq.type.converter.DateTypeConverter

  (2)全局类型转换器

  如果业务需求所有的日期都要转换,则可以使用全局类型转换器,只要在src根目录下面放置xwork-conversion.properties文件,并且properties文件中的内容为:

  待转换的类型=类型转换器的全类名

  如:java.util.Date = com.type.Converter.DateTypeConverter 即可

  代码

  Action类

  1.   package com.ljq.action;  
  2.   import java.util.Date;  
  3.   public class HelloWorldAction {  
  4.   // 日期   
  5.   private Date birthday;  
  6.   // 枚举   
  7.   private Gender gender;  
  8.   public String execute() {  
  9.   return "success";  
  10.   }  
  11.   public Date getBirthday() {  
  12.   return birthday;  
  13.   }  
  14.   public void setBirthday(Date birthday) {  
  15.   System.out.println("birthday="+birthday);  
  16.   this.birthday = birthday;  
  17.   }  
  18.   // 自定义枚举   
  19.   public enum Gender {  
  20.   MAN,WOMEN  
  21.   }  
  22.   public Gender getGender() {  
  23.   return gender;  
  24.   }  
  25.   public void setGender(Gender gender) {  
  26.   System.out.println("gender="+gender);  
  27.   this.gender = gender;  
  28.   }  
  29.   }  
  30.   日期类型转换器  
  31.   package com.ljq.type.converter;  
  32.   import java.text.SimpleDateFormat;  
  33.   import java.util.Date;  
  34.   import java.util.Map;  
  35.   import com.opensymphony.xwork2.conversion.impl.DefaultTypeConverter;  
  36.   /** 
  37.   * 日期自定义类型转换器 
  38.   * 
  39.   * @author jiqinlin 
  40.   * 
  41.   */  
  42.   public class DateTypeConverter extends DefaultTypeConverter {  
  43.   @SuppressWarnings("unchecked")  
  44.   @Override  
  45.   public Object convertValue(Map<String, Object> context, Object value,  
  46.   Class toType) {  
  47.   SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd HH:mm:ss");  
  48.   try {  
  49.   if (toType == Date.class) { // 当字符串向Date类型转换时   
  50.   String[] params = (String[]) value;  
  51.   return sdf.parseObject(params[0]);  
  52.   } else if (toType == String.class) { // 当Date转换成字符串时   
  53.   Date date=(Date)value;  
  54.   return sdf.format(date);  
  55.   }  
  56.   } catch (java.text.ParseException e) {  
  57.   e.printStackTrace();  
  58.   }  
  59.   return null;  
  60.   }  
  61.   }  
  62.   枚举类型转换器  
  63.   package com.ljq.type.converter;  
  64.   import java.util.Map;  
  65.   import com.ljq.action.HelloWorldAction.Gender;  
  66.   import com.opensymphony.xwork2.conversion.impl.DefaultTypeConverter;  
  67.   /** 
  68.   * 枚举自定义类型转换器 
  69.   * 
  70.   * @author jiqinlin 
  71.   * 
  72.   */  
  73.   public class GenderTypeConverter extends DefaultTypeConverter{  
  74.   @Override  
  75.   public Object convertValue(Map<String, Object> context, Object value,  
  76.   Class toType) {  
  77.   if(toType==Gender.class){ //当字符串向Gender类型转换时   
  78.   String[] params=(String[])value;  
  79.   return Gender.valueOf(params[0]);  
  80.   }else if (toType==String.class) { //当Gender转换成字符串时   
  81.   Gender gender=(Gender)value;  
  82.   return gender.toString();  
  83.   }  
  84.   return null;  
  85.   }  
  86.   }  
  87.   配置类型转换器  
  88.   测试路径  
  89.   日期  
  90.   http://localhost:8083/struts2/control/employee/list_execute.do?birthday=20110315 23:34:55   
  91.   枚举  
  92.   http://localhost:8083/struts2/control/employee/list_execute.do?gender=WOMEN   
  93.   局部类型转换器: HelloWorldAction-conversion.properties  
  94.   birthday=com.ljq.type.converter.DateTypeConverter  
  95.   gender=com.ljq.type.converter.GenderTypeConverter  
  96.   全局类型转换器: xwork-conversion.properties  
  97.   java.util.Date=com.ljq.type.converter.DateTypeConverter  
  98.   在页面打印日期和枚举的值  
  99.   birthday=${birthday }  
  100.   gender=${gender }  

简单使用

代码:

  1. public class DateConverter extends DefaultTypeConverter {  
  2.                 @Override  public Object convertValue(Map context, Object value, Class toType) {  
  3.     SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");  
  4.     try {   
  5.         if(toType == Date.class){//当字符串向Date类型转换时   
  6.             String[] params = (String[]) value;// Request.getParameterValues()    
  7.             return dateFormat.parse(params[0]);  
  8.         }else if(toType == String.class){//当Date转换成字符串时   
  9.             Date date = (Date) value;  
  10.             return dateFormat.format(date);  
  11.         }  
  12.     } catch (Parse Exception e) {}  
  13.     return null;  
  14.     }  
  15. }  

将上面的类型转换器注册为局部类型转换器: 

在Action类所在的包下放置ActionClassName-conversion.properties文件,ActionClassName是Action的类名,后面的-conversion.properties是固定写法,对于本例而言,文件的名称应为HelloWorldAction-conversion.properties 。在properties文件中的内容为:属性名称=类型转换器的全类名 

对于本例而言, HelloWorldAction-conversion.properties文件中的内容为: 

createtime= cn.itcast.conversion.DateConverter 

相关内容