首先jsp頁面
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'input.jsp' starting page</title>
</head>
<body>
<h3><font color="red">使用逗號將點的兩個座標分割開</font></h3>
<s:form action="pointConverter">
<s:textfield name="point" label="point"></s:textfield>
<s:textfield name="age" label="age"></s:textfield>
<s:textfield name="username" label="username"></s:textfield>
<s:textfield name="date" label="birthday"></s:textfield>
<s:submit label="submit"></s:submit>
</s:form>
</body>
</html>
然後是bean層
然後**Action,且對應Action目錄下有屬性檔案告訴Action裡的屬性值需要用自己寫的哪個類型轉換檔轉換
package com.test.converter;
import java.util.Map;
import ognl.DefaultTypeConverter;
import com.test.bean.Point;
public class PointConverter extends DefaultTypeConverter
{
@Override
public Object convertValue(Map context, Object value, Class toType)
{
if(Point.class == toType) //先是由PointAction-conversion.properties設定檔
{
Point point = new Point();
String[] str = (String[])value;
String[] paramValues = str[0].split(",");
int x = Integer.parseInt(paramValues[0]);
int y = Integer.parseInt(paramValues[1]);
point.setX(x);
point.setY(y);
return point;
}
if(String.class == toType) //在頁面上需要顯示時候才調用,然後再返回string類型
{
Point point = (Point)value;
int x = point.getX();
int y = point.getY();
String result = "[x=" + x + " , y=" + y + "]";
return result;
}
return null;
}
}
還有全域類型轉換設定檔xwork-conversion.properties