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類

複製代碼
package com.ljq.action;

import java.util.Date;

public class HelloWorldAction {
// 日期
private Date birthday;
// 枚舉
private Gender gender;

public String execute() {
return "success";
}

public Date getBirthday() {
return birthday;
}

public void setBirthday(Date birthday) {
System.out.println(
"birthday="+birthday);
this.birthday = birthday;
}

// 自定義枚舉
public enum Gender {
MAN,WOMEN
}

public Gender getGender() {
return gender;
}

public void setGender(Gender gender) {
System.out.println(
"gender="+gender);
this.gender = gender;
}

}
複製代碼

                

日期類型轉換器

複製代碼
package com.ljq.type.converter;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import com.opensymphony.xwork2.conversion.impl.DefaultTypeConverter;

/**
* 日期自定義類型轉換器

@author jiqinlin
*
*/
public class DateTypeConverter extends DefaultTypeConverter {

@SuppressWarnings(
"unchecked")
@Override
public Object convertValue(Map<String, Object> context, Object value,
Class toType) {
SimpleDateFormat sdf 
= new SimpleDateFormat("yyyyMMdd HH:mm:ss");
try {
if (toType == Date.class) { // 當字符串向Date類型轉換時
String[] params = (String[]) value;
return sdf.parseObject(params[0]);
else if (toType == String.class) { // 當Date轉換成字符串時
Date date=(Date)value;
return sdf.format(date);
}
catch (java.text.ParseException e) {
e.printStackTrace();
}
return null;
}
}
複製代碼

           

枚舉類型轉換器

複製代碼
package com.ljq.type.converter;

import java.util.Map;

import com.ljq.action.HelloWorldAction.Gender;
import com.opensymphony.xwork2.conversion.impl.DefaultTypeConverter;

/**
* 枚舉自定義類型轉換器

@author jiqinlin
*
*/
public class GenderTypeConverter extends DefaultTypeConverter{

@Override
public Object convertValue(Map<String, Object> context, Object value,
Class toType) {
if(toType==Gender.class){ //當字符串向Gender類型轉換時
String[] params=(String[])value;
return Gender.valueOf(params[0]);
}
else if (toType==String.class) { //當Gender轉換成字符串時
Gender gender=(Gender)value;
return gender.toString();
}
return null;
}
}
複製代碼

            

配置類型轉換器

複製代碼
測試路徑
日期
http:
//localhost:8083/struts2/control/employee/list_execute.do?birthday=20110315 23:34:55
枚舉
http:
//localhost:8083/struts2/control/employee/list_execute.do?gender=WOMEN


局部類型轉換器: HelloWorldAction
-conversion.properties
birthday
=com.ljq.type.converter.DateTypeConverter
gender
=com.ljq.type.converter.GenderTypeConverter


全局類型轉換器: xwork
-conversion.properties
java.util.Date
=com.ljq.type.converter.DateTypeConverter
複製代碼

      

在頁面打印日期和枚舉的值

  birthday=${birthday }
  gender=${gender }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章