struts2 中轉換器,在action送出時所有送出的都是字元串,在送出到背景都要進行轉換,
而預設的轉換器就是XWorkConverter,而XWorkConverter 用的是XWorkBasicConverter這個轉換器,
XWorkBasicConverter提供了把字元串轉換為基本類型,Bigdecimal,
BigInteger,java.util.Date,java.sql.Time,java.sql.Timestamp的轉換,
這三種時間轉換都提供了幾個SimpleDateFormat來
把字元串轉換為時間,也提供了把這些轉換成字元串
轉換是根據你Action屬性的Class類型的,如果看過XWorkConver的源代碼你就會發現有這麼一段
@Inject
public void setObjectFactory(ObjectFactory factory){
讀取xwork-convertion.properties
}
也就是在用Container得到XworkConverter時,就已經讀取了xwork-convertion.properties(classes下面)這個檔案
這個檔案是用來放入放入你的類和轉換器的
比如 struts2.Point = struts2.PointConverter(實作ognl.TypeConverter)的
也就是說當你Action中有Point屬性point時,你在頁面
<s:textfield name="point" />送出時,就會去找PointConverter來把字元串轉化為Point對象,也把Poing對象
轉換為String
當然xwork-convertion.properties存的是全局的轉換器
還可以struts2.Point = struts2.PointConverter放入你要使用的Action類下面,名字為ActionName-conversion.properties中,作用是一樣的,隻是這個是局部的
是以,在Action送出時,對Action的屬性的轉換,首先根據屬性類在xwork-convertion.properties找轉換器,再到
ActionName-conversion.properties找轉換器,沒有的話用預設的轉換器
例子:
package struts2;
public class Point {
int x;
int y;
public int getX() { return x; }
public void setX(int x) { this.x = x; }
public int getY() { return y; }
public void setY(int y) { this.y = y; }
public String toString(){ return x+"-"+y; } }
轉換器:
package struts2;
import java.lang.reflect.Member;
import java.util.Map;
import ognl.TypeConverter;
public class PointerConverter implements TypeConverter {
public Object convertValue(Map context, Object target, Member member,
String propertyName, Object value, Class toType) {
if (toType == Point.class) {
try {
String[] proArray = (String[]) value;
System.out.println(proArray.length);
for (String s : proArray) {
value = s;
}
} catch (Exception e) {
}
Point p = new Point();
if (value != null) {
String str = (String) value;
String[] s = str.split("-");
int x, y;
x = Integer.valueOf(s[0].trim());
y = Integer.valueOf(s[1].trim());
p.setX(x);
p.setY(y);
}
return p;
}
else if(toType == String.class){
return value;
}
return null;
}
}
然後在xwork-conversion.properties寫入
struts2.Point = struts2.PointConverter
Action:
package struts2;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.RequestUtils;
import org.apache.struts2.StrutsStatics;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.util.ValueStack;
public class TestLoginAction extends ActionSupport {
private Point point;
public public Point getPoint() {
return point;
}
public void setPoint(Point point) {
this.point = point;
}
@Override
public String execute() throws Exception {
if(point != null)
System.out.println("x="+point.getX()+",y="+point.getY());
return SUCCESS;
}
}
jsp:
<%@ page language="java" import="java.util.*,java.io.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<s:head theme="ajax" />
</head>
<body>
登陸界面<br>
<s:form action="login">
<table>
<tr>
<td>
<s:textfield name="point" label="坐标"/>
</td>
</tr>
<tr>
<td>
<input type="submit" value="送出"/>
</td>
</tr>
</table>
</s:form>
</body>
</html>