[标题]:[原]Struts2类型转换
[时间]:2009-7-15
[摘要]:Struts2类型转换、全局类型转换、集合类型转换
[关键字]:浪曦视频,Struts2应用开发系列,WebWork,Apache,类型转换
[环境]:struts-2.1.6、JDK6、MyEclipse7、Tomcat6
[作者]:Winty (wintys@gmail.com) http://www.blogjava.net/wintys
[正文]:
在Struts Action中可以自动转换String,Date等类型,但是如果Action中用到自定义类型,就需要使用Struts类型转换。
1、Struts类型转换
自定义类型Point
public class Point {
private int x;
private int y;
//省略getter and setter
}
Struts Action:
public class PointAction extends ActionSupport {
private Point point;
......
}
客户端输入:
<s:textfield name="point" label="坐标" ></s:textfield>
这样,从客户端传过来point的String值,不能直接set到PointAction中的point对象中。同时,输出point对象时,直接调用getter也是不行的。这时,就需要一个类型转换器,实现Point类与String之间的转换。可以继承Struts中的ognl.DefaultTypeConverter实现。
/StrutsHelloWorld/src/wintys/struts2/converter/PointConverter.java:
import java.util.Map;
import ognl.DefaultTypeConverter;
public class PointConverter extends DefaultTypeConverter {
@SuppressWarnings("unchecked")
@Override
public Object convertValue(Map context, Object value, Class toType) {
//String => Point
if(toType == Point.class){
//获取request string的字符串数组
String[] params = (String[])value;
String param = params[0];
String[] points = param.split(",");
Point point = new Point();
point.setX(Integer.parseInt(points[0]));
point.setY(Integer.parseInt(points[1]));
return point;
}
//Point => String
if(toType == String.class){
Point point = (Point)value;
int x = point.getX();
int y = point.getY();
return "["+x+","+y+"]";
}
return null;
}
}
同时,还需要一个配置文件(须跟PointAction在同一目录下)PointAction-conversion.properties(名称为XXXAction-conversion.properties)来告诉Struts2 PointAction中属性使用哪一个转换器类。
PointAction-conversion.properties:
point=wintys.struts2.converter.PointConverter
2、Struts全局类型转换
对于每一个属性应用转换器,当属性过多时会显得很繁琐,这时可以使用Struts全局类型转换。全局类型转换器名称为xwork-conversion.properties,与struts.xml同在classes目录下。
#global converter settings
#class to be converted = converter
wintys.struts2.converter.Point=wintys.struts2.converter.PointConverter
3、Struts集合类型转换
当PointAction中的属性是集合类型,跟非集合类型类似。
public class PointAction extends ActionSupport {
private List<Point> points;
......
}
转换器类可以继承org.apache.struts2.util.StrutsTypeConverter,实际上StrutsTypeConverter继承于 ognl.DefaultTypeConverter。
/StrutsHelloWorld/src/wintys/struts2/converter/StrutsPointConverter.java:
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.struts2.util.StrutsTypeConverter;
/**
* Struts类型转换: StrutsTypeConverter 继承于 ognl.DefaultTypeConverter
* @author Winty
* @version 2009-07-14
*
*/
@SuppressWarnings("unchecked")
public class StrutsPointConverter extends StrutsTypeConverter {
@Override
public Object convertFromString(Map context, String[] values, Class toClass) {
List<Point> points = new ArrayList<Point>();
for(String value : values){
String[] p = value.split(",");
Point point = new Point();
point.setX(Integer.valueOf(p[0]));
point.setY(Integer.valueOf(p[1]));
points.add(point);
}
return points;
}
@Override
public String convertToString(Map context, Object o) {
List<Point> points = (List<Point>)o;
StringBuilder sb = new StringBuilder();
sb.append("[");
for(Point p : points){
sb.append("(");
sb.append(p.getX());
sb.append(",");
sb.append(p.getY());
sb.append(") ");
}
sb.append("]");
return sb.toString();
}
}
当然也需要在PointAction-conversion-properties中配置:
PointAction-conversion-properties:
point=wintys.struts2.converter.PointConverter
points=wintys.struts2.converter.StrutsPointConverter
4、详细内容:
/StrutsHelloWorld/WebRoot/converter/input.jsp:
其中<s:textfield name="point" label="坐标" ></s:textfield>
可以换成
<s:textfield name="point.x" label="坐标.x"/>
<s:textfield name="point.y" label="坐标.y"/>
此时,Point类无需类型转换,Struts会自己去找point对象的x与y的setter方法。
当输出时,只需重写Point类的toString()方法即可。
<%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
<h1>请以逗号分割输入点的坐标,如:5,3</h1>
<s:form action="pointConverter">
<s:textfield name="point" label="坐标" ></s:textfield>
<s:textfield name="points" label="points1"> </s:textfield>
<s:textfield name="points" label="points2"></s:textfield>
<s:textfield name="age" label="年龄"></s:textfield>
<s:textfield name="name" label="姓名"></s:textfield>
<s:textfield name="birthday" label="出生日期"></s:textfield>
<s:submit value="确定"></s:submit>
</s:form>
</body>
</html>
/StrutsHelloWorld/WebRoot/converter/output.jsp:
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
<s:property value="point"/><br/>
<s:property value="points"/><br/>
<s:property value="age"/><br/>
<s:property value="name"/><br/>
<s:property value="birthday"/> <br/>
</body>
</html>
/StrutsHelloWorld/src/struts.xml:
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="MyStruts" extends="struts-default">
<action name="pointConverter" class="wintys.struts2.converter.PointAction">
<result name="success">/converter/output.jsp</result>
</action>
</package>
</struts>
/StrutsHelloWorld/WebRoot/WEB-INF/web.xml:
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<filter>
<filter-name>MyStruts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>MyStruts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
/StrutsHelloWorld/src/wintys/struts2/converter/Point.java:
/**
*
* @author Winty
* @version 2009-07-12
*/
public class Point {
private int x;
private 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;
}
}
/StrutsHelloWorld/src/wintys/struts2/converter/PointAction.java:
import java.util.Date;
import java.util.List;
import com.opensymphony.xwork2.ActionSupport;
/**
*
* @author Winty
* @verion 2009-07-12
*/
@SuppressWarnings("serial")
public class PointAction extends ActionSupport {
private Point point;
private List<Point> points;//集合类型的转换
private int age;
private String name;
private Date birthday;
public Point getPoint() {
return point;
}
public void setPoint(Point point) {
this.point = point;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public List<Point> getPoints() {
return points;
}
public void setPoints(List<Point> points) {
this.points = points;
}
@Override
public String execute() throws Exception {
return SUCCESS;
}
}
[参考资料]:
《浪曦视频之Struts2应用开发系列》
[附件]:
源代码: StrutsTypeConverter.zip