常用了类型转换器有两种,一种是作用于Action的,另一种是作用于整个Application的,下面分别介绍。
1、作用于Action的转换器
TestAction.java
package com.bebig.action;
import java.awt.Point;
import java.util.List;
import java.util.Map;
import java.util.Set;
import com.opensymphony.xwork2.ActionSupport;
public class TestAction extends ActionSupport {
private String username;
private String password;
List<String> interests;
Set<String> m;
Map<String, String> o;
Point p;
List<Point> ps;
Map<String, Point> points;
public Map<String, Point> getPoints() {
return points;
}
public void setPoints(Map<String, Point> points) {
this.points = points;
}
public List<Point> getPs() {
return ps;
}
public void setPs(List<Point> ps) {
this.ps = ps;
}
public Point getP() {
return p;
}
public void setP(Point p) {
this.p = p;
}
public Map<String, String> getO() {
return o;
}
public void setO(Map<String, String> o) {
this.o = o;
}
public Set<String> getM() {
return m;
}
public void setM(Set<String> m) {
this.m = m;
}
public List<String> getInterests() {
return interests;
}
public void setInterests(List<String> interests) {
this.interests = interests;
}
@Override
public String execute() {
return SUCCESS;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
MyPointConverter.java
//自定义的类型转换器
package com.bebig.converter;
import java.awt.Point;
import com.opensymphony.xwork2.conversion.impl.DefaultTypeConverter;
/**//*不推荐继承自DefaultTypeConverter,因为在处理String、String[]、String Object时
还得自己去判断,再作相应处理。
下面是我模拟的处理方法,更完美的处理方法请查阅StrutsTypeConverter类的convertValue方法*/
public class MyPointConverter extends DefaultTypeConverter {
@Override
public Object convertValue(Object value, Class toType) {
if (toType == Point.class) {
Point p = new Point();
String[] strs = null;
String[] xy = null;
try {
strs = (String[]) value;
} catch (Exception e) {
e.printStackTrace();
}
if (strs == null) {
xy = value.toString().split(",");//处理String Object
} else {
xy = strs[0].split(",");//处理String[]
}
p.x = Integer.parseInt(xy[0]);
p.y = Integer.parseInt(xy[1]);
return p;
} else if (toType == String.class) {
return value.toString();//处理String
}
return super.convertValue(value, toType);//未知对象交给父类来处理
}
}
MyPointConverter2.java
package com.bebig.converter;
import java.awt.Point;
import java.util.Map;
import org.apache.struts2.util.StrutsTypeConverter;
//继承自StrutsTypeConverter
public class MyPointConverter2 extends StrutsTypeConverter {
@Override
public Object convertFromString(Map context, String[] values, Class toClass) {
Point p = new Point();
String[] strs = values;
String[] xy = strs[0].split(",");
p.x = Integer.parseInt(xy[0]);
p.y = Integer.parseInt(xy[1]);
return p;
}
@Override
public String convertToString(Map context, Object o) {
return o.toString();
}
}
struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.devMode" value="true" />
<package name="test" namespace="/" extends="struts-default">
<action name="test" class="com.bebig.action.TestAction">
<result>/test.jsp</result>
</action>
</package>
</struts>
test.jsp
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<%@taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>Insert title here</title>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<base href="<%=basePath%>" />
</head>
<body>
<ol>
<li>username:<s:property value="username" /></li>
<li>password:<s:property value="password" /></li>
<li>interests:<s:property value="interests" /></li>
<li>m:<s:property value="m" /></li>
<li>o:<s:property value="o" /></li>
<li>p:<s:property value="p" /></li>
<li>ps:<s:property value="ps" /></li>
<li>points:<s:property value="points" /></li>
</ol>
<s:debug></s:debug>
</body>
</html>
注册转换器配置文件必须存放在与XXXXAction.java相同路径,名称为XXXXAction-conversion.properties
TestAction-conversion.properties
# syntax: <propertyName> = <converterClassName>
p=com.bebig.converter.MyPointConverter
此方法主要针对于某些属性的类型转换,如果想针对某些类的类型进行转换,请使用第二种方法。
2、作用于Application的转换器除了注册转换器配置文件有点不一样,其它代码相同。
这里的配置文件必须命名为xwork-conversion.properties,必须存放在src目录,配置内容如下所示:
# syntax: <type> = <converterClassName>
java.awt.Point=com.bebig.converter.MyPointConverter2
推荐使用第二种方法。另:
如果遇到非常麻烦的映射转换,建议使用request.setAttribute()或者seesion来传参数。