Struts2的类型转换(一)基本数据类型和抽象数据类型

来源:互联网 发布:网页游戏网站源码 编辑:程序博客网 时间:2024/06/01 18:42

1、为什么要使用数据类型的转化?

          我们从表单中接收用户的输入全部都是字符串(String)类型的数据,要把字符串(String)类型的数据封装到Action的属性中,就要对非字符串的数据进行转换。比如String-->Integer。同样,我们要取出Action属性的值显示到页面中,我们就需要把其它的数据类型转换成字符串(String)类型。

2、如何进行转换?

         Struts2为我们提供了基本的数据类型的转换,也就是说String类型可以自动转换成Integer类型,不需要我们手动实现转换。包括Date抽象数据类型也可以为我们自动进行转换。那么我们所要关心的数据类型的转换无非就是一些抽象数据类型,比如我们定义的Person,或者是我们经常用到的集合。

3、我们来实现一个最简单的类型转换,实现基本数据类型以及简单的抽象数据类型的转换。

3.1 、写一个input.jsp页面接收用户的数据(这里我们使用了struts2的标签,所以我们在JSP页面中要引入struts2的标签库)。我们这里接受的四个参数分别对应抽象数据类型,int类型,String类型,Date类型

<body>   <s:form action="pointConverter">   <s:textfield  name="point" label="point"></s:textfield>   <s:textfield label="age" name="age"></s:textfield>   <s:textfield label="username" name="username"></s:textfield>   <s:textfield label="birthday" name="date"></s:textfield><s:submit label="submit"></s:submit>   </s:form></body>

3.2、写一个实体Point用来封装用户输入的point的信息。点我们把它看成平面的点,因此只有两个属性就够了,分别是x坐标和y坐标。

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;}}

3.3、我们在3.1中指定的Action为PointAction,那么我们就来完成PointAction这个类。这个类中总共定义了四个成员属性,int类型的age,String类型的username,Date类型的birthday以及Point类型的point

public class PointAction extends ActionSupport{private Point point;private int age;private String username;private Date birthday;public String execute(){return "success";}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 setBirthday(Date birthday) {this.birthday = birthday;}}

3.4、写好了我们的Action后我们在struts.xml文件中对其进行配置(action的name属性为我们在3.1指定的action,class属性为我们action类的全路径,成功之后跳转到output.jsp)。

<action name="pointConverter" class="com.guanhua.action.PointAction">    <result name="success">/output.jsp</result></action>

3.5、写一个output.jsp来显示用户的输入(这里我们仅仅是为了进行测试,实际开发中根据需求来完成。跟input.jsp相同,这里我们也使用到了struts2的标签,因此也需要在JSP中引入struts2的标签库)

<body>     <!--取出表单中输入的值 -->    point:<s:property value="point"/>    age:<s:property value="age"/>    birthday:<s:property value="birthday"/>    username:<s:property value="username"/></body>

3.6、到了这一步,其实像age,birthday,username这些个属性都可以实现类型转换了,我们现在唯一关注的就是Point这个属性该如何进行类型转换。我们要对Point这个类型和String类型相互转换,首先需要写一个转换类PointConverter继承DefaultTypeConverter,然后覆盖它的convertValue方法。最后我们需要在PointAction这个类所在的包下创建一个PointAction-convertion.properties文件。好了,我们来看一下具体的代码:

package com.guanhua.converter;import java.util.Map;import ognl.DefaultTypeConverter;import com.guanhua.domain.Point;public class PointConverter extends DefaultTypeConverter{        <pre name="code" class="html">         /** * @param value  这是要转换的值 * @param toType 这是要转换的类型 我们要把String-->Point 那么我们的toType就是Point类型 */@Overridepublic Object convertValue(Map context, Object value, Class toType) {// TODO Auto-generated method stubif(Point.class==toType){String [] str = (String[])value;Point point = new Point();String s = str[0];String ss[] = s.split(",");point.setX(Integer.parseInt(ss[0]));point.setY(Integer.parseInt(ss[1]));return point;}if(String.class==toType){Point point = (Point) value;String ss =point.getX()+ ","+point.getY();return ss;}return null;}}

3.7、properties文件代码如下(point就是我们在action中定义的point属性,等号后面跟的就是我们写好的类型转换器的全路径):

point=com.guanhua.converter.PointConverter

大功告成!启动tomcat来运行一下我们的项目吧!


0 0
原创粉丝点击