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即可


2011031623074589.png

代碼

Action類

複製代碼
packagecom.ljq.action;

importjava.util.Date;

publicclassHelloWorldAction{
//日期
privateDatebirthday;
//枚舉
privateGendergender;

publicStringexecute(){
return"success";
}

publicDategetBirthday(){
returnbirthday;
}

publicvoidsetBirthday(Datebirthday){
System.out.println(
"birthday="+birthday);
this.birthday=birthday;
}

//自定義枚舉
publicenumGender{
MAN,WOMEN
}

publicGendergetGender(){
returngender;
}

publicvoidsetGender(Gendergender){
System.out.println(
"gender="+gender);
this.gender=gender;
}

}
複製代碼

日期類型轉換器

複製代碼
packagecom.ljq.type.converter;

importjava.text.SimpleDateFormat;
importjava.util.Date;
importjava.util.Map;

importcom.opensymphony.xwork2.conversion.impl.DefaultTypeConverter;

/**
*日期自定義類型轉換器
*
*
@authorjiqinlin
*
*/
publicclassDateTypeConverterextendsDefaultTypeConverter{

@SuppressWarnings(
"unchecked")
@Override
publicObjectconvertValue(Map<String,Object>context,Objectvalue,
ClasstoType){
SimpleDateFormatsdf
=newSimpleDateFormat("yyyyMMddHH:mm:ss");
try{
if(toType==Date.class){//當字符串向Date類型轉換時
String[]params=(String[])value;
returnsdf.parseObject(params[0]);
}
elseif(toType==String.class){//當Date轉換成字符串時
Datedate=(Date)value;
returnsdf.format(date);
}
}
catch(java.text.ParseExceptione){
e.printStackTrace();
}
returnnull;
}
}
複製代碼

枚舉類型轉換器

複製代碼
packagecom.ljq.type.converter;

importjava.util.Map;

importcom.ljq.action.HelloWorldAction.Gender;
importcom.opensymphony.xwork2.conversion.impl.DefaultTypeConverter;

/**
*枚舉自定義類型轉換器
*
*
@authorjiqinlin
*
*/
publicclassGenderTypeConverterextendsDefaultTypeConverter{

@Override
publicObjectconvertValue(Map<String,Object>context,Objectvalue,
ClasstoType){
if(toType==Gender.class){//當字符串向Gender類型轉換時
String[]params=(String[])value;
returnGender.valueOf(params[0]);
}
elseif(toType==String.class){//當Gender轉換成字符串時
Gendergender=(Gender)value;
returngender.toString();
}
returnnull;
}
}
複製代碼

配置類型轉換器

複製代碼
測試路徑
日期
http:
//localhost:8083/struts2/control/employee/list_execute.do?birthday=2011031523: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}

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