您的位置:首页 > 其它

structs2 DefaultTypeConverter类型转换说明

2014-12-14 16:00 92 查看
 1.  创建含有需要类型转化对象的JSP页面(input.jsp,里面表单里含有自定义类)

<body>

  <h3><font color="red">使用逗号将点的两个坐标分隔开</font></h3>

    <s:form action="Register">

          

<s:textfield name="point" label="point"/>

       <s:textfield name="age" label="age"/>

       <s:textfield name="username" label="username"/>

       <s:textfield name="date" label="birthday"/>

          

<s:submit label="submit"></s:submit>

    </s:form>

    </body>

2.  创建信息输出JSP页面(out.jsp)

<html>

     <head>

        <title>My JSP 'output.jsp' starting page</title>

    </head>

     <body>

        point:<s:property value="point"/><br>

        age:<s:property value="age"/><br>

        username:<s:property value="username"/><br>

        date:<s:property value="date"/><br>

     </body>

</html>

3.       创建上面JSP页面中需要类型转换的对象所对应的类(Point.java)

public class Point

{

    private int x;

    private int y;

    //getter、setter方法

}

4.  创建form提交接受的Action(RegisterAction.java)

public class RegisterAction extends ActionSupport

{

    private Point point;

    private int age;

    private String username;

    private Date data;

//getter、setter方法

   

    @Override

    public String execute() throws Exception

    {

       return SUCCESS;

    }

}

5.  Struts.xml中配置RegisterAction

<action name="Register" class="com.test.action.RegisterAction">

<result name="success">/output.jsp</result>

</action>

6.  创建能转换Point的类(PointConvert.java)

注意:与StrutsTypeConverter方式的转换不同之处!

public class PointConverter extends DefaultTypeConverter

{

public Object convertValue(Map context, Object value, Class toType)

{

    if(Point.class==toType)

    {

        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)

    {

        Point point=(Point)value;

       

        int x=point.getX();

        int y=point.getY();

       

        String result="[x="+x+", y="+y+"]";

       

        return result;

    }

    return null;

}

}

引入参数中的toType表示的是目标类型(是字符串类型还是Point类型),

当客户端string提交到pointConvert这个action时候(即:toType==Point.class),value指的是textfield中填写的字符串。

对if(Point.class==toType)过程解释:

(1) String[] str=(String[])value;-----------------将value显示转化为字符数组后赋值为数组str[];

(2)String[] paramValues=str[0].split(",");-------取出str[]的第一个元素,即:str[0],当textfield里填写的是“20,40”,str[0]里的内容也就是一个字符串“20,30”,然后通过str[0].split(“,”)将字符串“20,30”以逗号为界限拆分成一个数组并赋值给paramValues[],这时paramValues[]里内容是{“20”,”30”};

(3) int x=Integer.parseInt (paramValues [0]);-----将字符串类型的“20”转换为integer型赋值给x;

7.       配置属性文件使得让Point属性由PointConvert处理:

⑴  在和要被convert的类的同目录下新建一个文件:

RegisterAction -conversion.properites。

RegisterAction --------要转换的属性所在的类的类名

-conversion.properies-----固定格式

⑵填写属性文件内容:

point=com.test.converter.PointConvert

point--------具体要转换哪一个属性

com.test.converter.PointConverter-----具体用那个convert类来转换point

注意:如果对多个属性进行转换可以添加多个如point=com.test.converter.PointConvert

形式的内容。 

8. 程序运行流程

运行流程分两步:一、字符串字段被转化为对应数据类型字段上传到action中;二、对应字段被转化为字符串类型字段显示在输出页面;

第一步:input.jsp提交表单---->进入表单提交的action(Register.action),在此action里接受表单穿过来的各个字段,是基本数据类型字段struts2会自动进行对其转换,如果遇到对象类型字段则进入此action所在的目录查找转换配置文件(RegisterAction-convertion.properties),在转换配置文件中查找处理此对象数据类型的类(PointConverter.java)---->对象类型转化类将form提交过来的与对象类型相关的字符串转化为对象实例->转化后的对象实例被设置(set)到action中;

第二步:action(Register.action)中的众多字段里,如果是基本数据类型字段的struts2会自动转换为字符串类型字段发送到输出页面(out.jsp), 如果是对象类型字段则会进入action(Register.action)中找转换配置文件(RegisterAction-convertion.properties),在转换配置文件中查找处理此对象数据类型的类(PointConverter.java)---->对象类型抓换类将action中的对象类型字段转换为相应字符串字段---->转换后的字符串实例被get到输出页面。

9.       定义全局类型转换

上面所演示的是局部类型转换方式,假如有四个action,分别为PointAction1、PointAction2、PointAction2、PointAction4,每个PointAction里面都有一个相同的point属性需要转换,所以必须在com.test.action包下面创建四个属性文件指定每个PointAction都需要ActionConvert类进行转换,即:

PointAction1-coversion.properties,PointAction2-coversion.properties, PointAction3-coversion.properties,PointAction4-coversion.properties

如果更多类需要转换,显然这种方式比较累人,这种情况可以考虑使用全局类型转换。

配置步骤:

(1)  在src目录下创建全局属性文件,文件名为:

xwork-conversion.properties 文件名是固定不变的。

(2) 全局属性文件内容:

com.test.bean.Point=com.test.converter.PointConverter2

等号左边是要转换的类所在的包名,右边是具体用那个convert类来转换point。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: