歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> struts2 自定義類型轉換器

struts2 自定義類型轉換器

日期:2017/3/1 10:09:33   编辑:Linux編程

原理詳述

  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

Copyright © Linux教程網 All Rights Reserved