原理
struts2的自定義類型轉(zhuǎn)換機(jī)制為復(fù)雜類型的輸入輸出處理提供了便捷.struts2已經(jīng)為我們提供了幾乎所有的primitive類型以及常用類型(如Date)的類型轉(zhuǎn)換器,我們也可以為我們自定義類添加自定義類型轉(zhuǎn)化器.
struts2為我們提供了一個(gè)類型轉(zhuǎn)化器的入口: ognl.DefaultTypeConverter,或繼承org.apache.struts2.util.StrutsTypeConverter,由于StrutsTypeConverter提供了更好的封裝,所以建議大家在寫轉(zhuǎn)換器時(shí)通常采用繼承StrutsTypeConverter方式來實(shí)現(xiàn).
StrutsTypeConverter類實(shí)質(zhì)上是DefaultTypeConverter的擴(kuò)展
publicabstractclass StrutsTypeConverter extends DefaultTypeConverter
{
}
|
StrutsTypeConverter中的兩個(gè)核心方法
publicabstract Object convertFromString(Map context, String[] values, Class toClass);
publicabstract String convertToString(Map context, Object o);
|
convertFromString方法用于從前臺(tái)頁面獲取字符串,將字符串轉(zhuǎn)化為對(duì)象
convertToString方法用于將對(duì)象以字符串的方式輸出到頁面
我們?cè)趯?/span>struts2自定義類型轉(zhuǎn)換類的時(shí)候主要就是覆蓋上面兩個(gè)方法
分類
struts2自定義類型轉(zhuǎn)換從大的方面來講分兩種:
u 局部類型轉(zhuǎn)換
u 全局類型轉(zhuǎn)換
局部類型轉(zhuǎn)換又分為三種:
² 普通實(shí)體bean的自定義類型轉(zhuǎn)換
² 基于領(lǐng)域模型的自定義類型轉(zhuǎn)換
² 基于模型驅(qū)動(dòng)的自定義類型轉(zhuǎn)換
無論是全局類型轉(zhuǎn)換還是局部類型轉(zhuǎn)換,轉(zhuǎn)換器與Action之間是用properties文件來關(guān)聯(lián)的,properties文件指明了轉(zhuǎn)換規(guī)則
全局類型轉(zhuǎn)換規(guī)則:
在classpath下新建文件xwork-conversion.properties(固定名稱)
其內(nèi)容為:目標(biāo)轉(zhuǎn)換對(duì)象=轉(zhuǎn)換器類(包名+類名)
局部類型轉(zhuǎn)換規(guī)則:
在對(duì)應(yīng)的Action的同級(jí)目錄下新建Action名-conversion.properties(一定要與Action類名對(duì)應(yīng))
其內(nèi)容為: 目標(biāo)轉(zhuǎn)換對(duì)象=轉(zhuǎn)換器類(包名+類名)
在局部類型轉(zhuǎn)換中又存在一種特殊情況
基于領(lǐng)域模型的自定義類型轉(zhuǎn)換
它不但要在對(duì)應(yīng)的Action的同級(jí)目錄下新建Action名-conversion.properties(一定要與Action類名對(duì)應(yīng))文件,還需在引用模型同級(jí)目錄下建properties文件取名規(guī)則為引用名- conversion.properties
這塊不好用文字描述,舉個(gè)列子:
需求是這樣的:
在User類中有個(gè)Point對(duì)象的引用,現(xiàn)在要基于Point來做自定義類型轉(zhuǎn)換,這里Point與User之間的這層關(guān)系就叫做領(lǐng)域模型,在操作User時(shí)需要對(duì)Point進(jìn)行自定義類型轉(zhuǎn)換,這時(shí)就必須在User類的同級(jí)目錄下新建User-conversion.properties文件,在文件中指明point對(duì)象需要用什么類來進(jìn)行轉(zhuǎn)換.
我們約定Point類的對(duì)象名就為point,而對(duì)應(yīng)的轉(zhuǎn)換類為com.dl.convertor.PointConvertor,對(duì)應(yīng)的Action類為PointUserAtion, PointUserAtion中有一個(gè)User類型的屬性名為user
那么在PointUserAtion的同級(jí)目錄中會(huì)存在一個(gè)名為PointUserAtion-conversion.properties的文件其內(nèi)容為:
user.point= com.dl.convertor.PointConvertor
//因?yàn)樵?/span>Action中引用的對(duì)象名為user而現(xiàn)在要處理的是user中的point屬性,所以這里需要使用user.point來指明
|
同樣在User類的同級(jí)目錄會(huì)存在一個(gè)名為User-conversion.properties的文件內(nèi)容為
point=com.dl.convertor.PointConvertor
//因?yàn)樵撐募会槍?duì)user,所以只需指明User中的point對(duì)象即可不需在添加user否則會(huì)出現(xiàn)預(yù)想不到的結(jié)果
|
針對(duì)局部類型轉(zhuǎn)換三種情況的例子
² 普通實(shí)體bean類型轉(zhuǎn)換
實(shí)體bean(Point)
/**
*普通的javabean封裝坐標(biāo)
*/
publicclass Point {
privateintx;
privateinty;
//省略set get方法
}
|
對(duì)應(yīng)的Action(PointAction)
publicclass PointAction extends ActionSupport{
private Point point;
//省略set get方法
public String execute() throws Exception {
returnsuper.execute();
}
}
|
對(duì)應(yīng)的轉(zhuǎn)換類(PointConvertor)
publicclass PointConvertor extends StrutsTypeConverter{
/**
*從表單中的string到Point對(duì)象
*我們約定以,來分隔這里為了嚴(yán)謹(jǐn)期間最好要進(jìn)行輸入數(shù)據(jù)的驗(yàn)證
*/
@Override
public Object convertFromString(Map context, String[] str, Class c) {
Point point=null;
if(str!=null||str.length>0){
String[] s=str[0].split(",");
point=new Point();
int x=Integer.parseInt(s[0]);
int y=Integer.parseInt(s[1]);
point.setX(x);
point.setY(y);
}
return point;
}
/**
*從對(duì)象到字符串
*比如頁面輸出
*/
@Override
public String convertToString(Map context, Object o) {
Point point=(Point)o;
return"("+point.getX()+","+point.getY()+")";
}
}
|
需要做的配置:在PointAction的同級(jí)目錄下新建PointAction-conversion.properties,文件內(nèi)容為
#目標(biāo)轉(zhuǎn)換對(duì)象=轉(zhuǎn)換器
point=com.dl.convertor.PointConvertor
|

² 領(lǐng)域模型自定義類型轉(zhuǎn)換
實(shí)體bean(Point,User)
publicclass User {
private String name;
private Point point;
//省略set get方法
}
|
publicclass Point {
privateintx;
privateinty;
//省略set get方法
}
|
對(duì)應(yīng)的Action(PointAction)
public class UserAction extends ActionSupport{
private User user;
//省略set get方法
@Override
public String execute() throws Exception {
return super.execute();
}
}
|
對(duì)應(yīng)的轉(zhuǎn)換類(PointConvertor)
publicclass PointConvertor extends StrutsTypeConverter{
/**
*從表單中的string到Point對(duì)象
*我們約定以,來分隔這里為了嚴(yán)謹(jǐn)期間最好要進(jìn)行輸入數(shù)據(jù)的驗(yàn)證
*/
@Override
public Object convertFromString(Map context, String[] str, Class c) {
Point point=null;
if(str!=null||str.length>0){
String[] s=str[0].split(",");
point=new Point();
int x=Integer.parseInt(s[0]);
int y=Integer.parseInt(s[1]);
point.setX(x);
point.setY(y);
}
return point;
}
/**
*從對(duì)象到字符串
*比如頁面輸出
*/
@Override
public String convertToString(Map context, Object o) {
Point point=(Point)o;
return"("+point.getX()+","+point.getY()+")";
}
}
|
需要做的配置:
在UserAction的同級(jí)目錄下新建UserAction-conversion.properties,文件內(nèi)容為
#目標(biāo)轉(zhuǎn)換對(duì)象=轉(zhuǎn)換器
user.point=com.dl.convertor.PointConvertor
|
在User的同級(jí)目錄下新建User-conversion.properties,文件內(nèi)容為
point=com.dl.convertor.PointConvertor
|

² 模型驅(qū)動(dòng)自定義類型轉(zhuǎn)換
實(shí)體bean(Point)
/**
*普通的javabean封裝坐標(biāo)
*/
publicclass Point {
privateintx;
privateinty;
//省略set get方法
}
|
對(duì)應(yīng)的Action(PointModelDrivenAction)
/**
* 基于模型驅(qū)動(dòng)的自定義類型轉(zhuǎn)換
* @author Administrator
*
*/
@SuppressWarnings("serial")
public class PointModelDrivenAction extends ActionSupport implements ModelDriven<Point>{
private Point point;
public Point getPoint() {
return point;
}
public void setPoint(Point point) {
this.point = point;
}
public Point getModel() {
return point;
}
@Override
public String execute() throws Exception {
return super.execute();
}
}
注:這里切記要生成point的set get方法要不值不能自動(dòng)進(jìn)行封裝
|
對(duì)應(yīng)的轉(zhuǎn)換類(PointConvertor)
publicclass PointConvertor extends StrutsTypeConverter{
/**
*從表單中的string到Point對(duì)象
*我們約定以,來分隔這里為了嚴(yán)謹(jǐn)期間最好要進(jìn)行輸入數(shù)據(jù)的驗(yàn)證
*/
@Override
public Object convertFromString(Map context, String[] str, Class c) {
Point point=null;
if(str!=null||str.length>0){
String[] s=str[0].split(",");
point=new Point();
int x=Integer.parseInt(s[0]);
int y=Integer.parseInt(s[1]);
point.setX(x);
point.setY(y);
}
return point;
}
/**
*從對(duì)象到字符串
*比如頁面輸出
*/
@Override
public String convertToString(Map context, Object o) {
Point point=(Point)o;
return"("+point.getX()+","+point.getY()+")";
}
}
|
需要做的配置:
在PointModelDrivenAction的同級(jí)目錄下新建PointModelDrivenAction-conversion.properties,文件內(nèi)容為
#目標(biāo)轉(zhuǎn)換對(duì)象=轉(zhuǎn)換器
point=com.dl.convertor.PointConvertor
|
在User的同級(jí)目錄下新建User-conversion.properties,文件內(nèi)容為
point=com.dl.convertor.PointConvertor
|


至此Struts2的自定義類型轉(zhuǎn)換你基本已經(jīng)掌握了,還不趕緊動(dòng)手練練.遇到什么問題,請(qǐng)及時(shí)遇我聯(lián)系
QQ:184675420 Blog:http://www.tkk7.com/sxyx2008/
Email:sxyx2008@gmail.com
demo下載:http://www.tkk7.com/Files/sxyx2008/struts2typeconvertor.zip
電子文檔下載:點(diǎn)我下載電子版文檔.pdf