• Structs2笔记③--局部类型转换案例


    Structs2的类型转换—局部类型转换

    Ognl强大的表达式语言,在导入项目的时候我们导入了ognl.jar包,内有TypeConverter类,struct主要依赖于他进行类型转换。

    例子

     

    input.jsp

    <%@ taglib prefix="s" uri="/struts-tags"%>

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

        <s:form action="/new/pointConverter">

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

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

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

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

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

        </s:form>

     

    对应的坐标实体

    Point.Java

    package com.test.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;

        }

    }

     

     

     

    自定义的类型转换类PointConverter,继承DefaultTypeConverter,重写convertValue

    方法

    package com.test.convertr;

     

    import java.lang.reflect.Member;

    import java.util.Map;

     

    import com.test.bean.Point;

     

    import ognl.DefaultTypeConverter;

    /**

    * 自定义的转换类

    * @author Administrator

    *

    */

    public class PointConverter extends DefaultTypeConverter {

     

        @Override

        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;

        }

    }

     

    写与表单相对应的PointAction类

    package com.test.action;

     

    import java.util.Date;

     

    import com.opensymphony.xwork2.ActionSupport;

    import com.test.bean.Point;

     

    public class PointAction extends ActionSupport{

        private Point point;

        private int age;

        private String username ;

        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 getUsername() {

            return username;

        }

        public void setUsername(String username) {

            this.username = username;

        }

        public Date getBirthday() {

            return birthday;

        }

        public void setBrithday(Date birthday) {

            this.birthday = birthday;

        }

        @Override

        public String execute() throws Exception {

            return SUCCESS;

            

        }

    }

    告诉struct 我的Point属性使用我自己写的转换类转换。

    在Action目录下建立PointAction-conversion.properties文件

    point=com.test.convertr.PointConverter

     

    配置structs.xml

    <action name="pointConverter" class="com.test.action.PointAction">

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

    </action>

     

    建立output.jsp

    <%@ taglib prefix="s" uri="/struts-tags"%>

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

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

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

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

  • 相关阅读:
    sort
    Sicily--17956. Maximum Multiple
    代码1005
    487-3279的解法实例
    487-3279另一种解法
    487-3279
    人工智能--识别句子
    1003-Hangover
    推荐书单(转自GITHUB)
    转自微信号:测试那点事
  • 原文地址:https://www.cnblogs.com/chengzhipcx/p/4754277.html
Copyright © 2020-2023  润新知