您的位置:首页 > 编程语言 > Java开发

Struts2 自定义类型转换器的简单示例

2013-09-17 15:08 393 查看
1. Coordinate坐标实体:

package com.huey.entity;

/**
*
* @author Huey2672
*
*/
public class Coordinate {

private double x;
private double y;

public double getX() {
return x;
}

public void setX(double x) {
this.x = x;
}

public double getY() {
return y;
}

public void setY(double y) {
this.y = y;
}

public Coordinate() {
}

public Coordinate(double x, double y) {
this.x = x;
this.y = y;
}

@Override
public String toString() {
return "(" + x + ", " + y + ")";
}

}


2. 自定义类型转换器, 需要实现TypeConverter接口,TypeConverter的接口定义如下:

public abstract interface TypeConverter {
public Object convertValue(Map context, Object target, Member member,
String propertyName, Object value, Class toType);
}

但是接口中的方法太过复杂,可以通过TypeConverter接口的实现类DefaultTypeConverter来实现自定义类型转换器:

package com.huey.converter;

import java.util.Map;

import com.huey.entity.Coordinate;

import ognl.DefaultTypeConverter;

/**
* Coordinate的类型转换器
* @author Huey2672
*
*/

public class CoordinateConverter extends DefaultTypeConverter {
@Override
public Object convertValue(Map context, Object value, Class toType) {
if (toType == Coordinate.class) {
// 返回的请求参数是一个字符串数组
String[] params = ((String[])value)[0].split(",");
double x = Double.valueOf(params[0]);
double y = Double.valueOf(params[1]);
return new Coordinate(x, y);
} else if (toType == String.class) {
Coordinate coordinate = (Coordinate)value;
return coordinate.toString();
}
return null;
}
}

Struts2还提供了一个StrutsTypeConverter抽象类,这个抽象类是DefaultTypeConverter的子类,简化了类型转换器的实现:

package com.huey.converter;

import java.util.Map;

import org.apache.struts2.util.StrutsTypeConverter;

import com.huey.entity.Coordinate;

/**
* Coordinate的类型转换器
* @author Huey2672
*
*/
public class CoordinateConverter extends StrutsTypeConverter {

@Override
public Object convertFromString(Map context, String[] value, Class toClass) {
String[] params = value[0].split(",");
double x = Double.valueOf(params[0]);
double y = Double.valueOf(params[1]);
return new Coordinate(x, y);
}

@Override
public String convertToString(Map context, Object obj) {
Coordinate coordinate = (Coordinate)obj;
return coordinate.toString();
}

}


3. 注册类型转换器,方式有三种:

1) 局部注册:在CoordinateAction相同路径下编写配置文件CoordinateAction-conversion.properties:

coordinate=com.huey.converter.CoordinateConverter


2) 全局注册:在src目录下编写配置文件xwork-conversion.properties:

com.huey.entity.Coordinate=com.huey.converter.CoordinateConverter

3) Annotation注册:

4. CoordinateAction:

package com.huey.action;

import com.huey.entity.Coordinate;
import com.opensymphony.xwork2.ActionSupport;

/**
*
* @author Huey2672
*
*/
public class CoordinateAction extends ActionSupport {

private Coordinate coordinate;

public Coordinate getCoordinate() {
return coordinate;
}

public void setCoordinate(Coordinate coordinate) {
this.coordinate = coordinate;
}

public String show() throws Exception {
return "success";
}

}


5. 配置struts.xml文件:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>

<package name="coordinate" extends="struts-default">
<action name="show" class="com.huey.action.CoordinateAction" method="show">
<result name="success">show.jsp</result>
<!-- 类型转换失败时,将转入input视图 -->
<result name="input">index.jsp</result>
</action>
</package>

</struts>


6. index.jsp和show.jsp页面:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>Index</title>

</head>

<body>
<form action="show" method="post">
请输入坐标:<input type="text" name="coordinate"><br/>
<input type="submit" value="确定">
</form>
</body>
</html>

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>Show</title>

</head>

<body>
坐标:${coordinate}
</body>
</html>


7. 在index.jsp页面的输入框中输入“22.3,75”,然后提交,跳转到show.jsp页面中显示为:坐标:(22.3, 75.0)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: