struts数据类型转换
struts的数据类型转换器,把表单提交的参数映射到bean对象对应属性的数据类型,但对于一些特殊数据类型还无法转换,比如坐标,解决的方法如下:
第一步:对于无法转化的数据类型,单独设计一个继承了ognl.DefaultTypeConverter类的子类,在这个子类中覆盖它的“public Object convertValue(Map context, Object value, Class toType){ } ”方法,把具体转化该类型的操作写在该方法中,注意,有两个转换动作,一个是把表单接收到的参数转化成bean对象对应属性的数据类型,另一个是把bean对象对应属性的数据类型转换成String类型,用于返回给显示页面。
第二步:为该类型转换类配置信息,让struts知道,当表单提交该属性名称的参数时,自动调用已设置好的类型转化类进行转换动作。需要在当前action类的根目录下新建一个文件,文件名是有固定格式的:“action的名称-conversion.properties”,在该文件中配置如下信息:“需要转换的属性名称=转换类的class路径”。
这样,在表单提交该属性名参数的时候,struts会自动调用自定义设置的数据转换类对其进行转化成对应属性的数据类型。
简单实例(处理坐标的数据类型转换,struts2.2.1版本下):
①坐标实体bean
package com.hzp.bean;
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;
}
}
②当前表单业务处理(action)类
package com.hzp.action;
import com.hzp.bean.Point;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport{
private Point point;
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Point getPoint() {
return point;
}
public void setPoint(Point point) {
this.point = point;
}
public String execute() throws Exception {
return "success";
}
}
③自定义point(坐标)类型转换类
package com.hzp.converter;
import java.util.Map;
import com.hzp.bean.Point;
import ognl.DefaultTypeConverter;
public class ConvertPoint extends DefaultTypeConverter {
public Object convertValue(Map context, Object value, Class toType) {
//从表单转换成对应属性类型
if (Point.class == toType) {
String[] str = (String[]) value;
String p = str[0];
String[] strPoint = p.split(",");
Point point = new Point();
point.setX(Integer.parseInt(strPoint[0]));
point.setY(Integer.parseInt(strPoint[1]));
return point;
}
//从对应属性类型转换成字符串给显示页面
else if(String.class == toType) {
Point point = (Point) value;
String x = Integer.toString(point.getX());
String y = Integer.toString(point.getY());
String result = "(" + x + "," + y + ")";
return result;
} else {
return null;
}
}
}
④配置文件LoginAction-conversion.properties的设置(LoginAction是我当前表单提交到的业务处理类名称)
point=com.hzp.converter.ConvertPoint
⑤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>
<package name="struts2" extends="struts-default">
<action name="login" class="com.hzp.action.LoginAction">
<result name="success">/result.jsp</result>
</action>
</package>
</struts>
⑥web.xml配置文件内容
<?xml version="1.0" encoding="UTF-8"?>
<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>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>