Struts2的类型转换

来源:互联网 发布:jrtplib ubuntu 编辑:程序博客网 时间:2024/04/27 18:25

一、Struts内建的类型转换器
boolean和Boolean:字符串与布尔值之间的转换
Charater和char:字符串与字符之间的转换
Integer和int:字符串与整形之间的转换
Long和long:字符串与长整形之间的转换
Float和float:字符串与浮点型之间的转换
Double和double:字符串与双精度浮点型之间的转换
Date:完成字符串与日期行的转换,日期格式采用用户请求所有的Locale的SHORT格式

部分代码如下:

这里写代码片

这里写图片描述

这里写图片描述

二、基于OGNL的类型转换

1、部分代码如下:
这里写图片描述

这里写图片描述

这里写图片描述

这里写图片描述
注意:
(1)、使用OGNL表达式进行类型转换的时候,要求POJO对象:提供无参构造函数,要转换的属性必须提供setter方法。
(2)、要求Action类提供要转换对象的getter、setter方法。

这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述

user中toString()方法的作用明显,在重写了超类toString()方法后,执行输出函数println()方法后会自动调用该类重写后的方法。

这里写图片描述

三、自定义类型转换
1、部分代码如下:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%@ taglib prefix="s" uri="/struts-tags" %><%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>自定义转换器输入</title>  </head>  <body>    <h4>自定义类型转换器</h4>    <s:form action="customConversion" method="post">        <s:textfield name="point" label="坐标"></s:textfield>        <s:submit value="提交"></s:submit>    </s:form>  </body></html>
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE struts PUBLIC    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"    "http://struts.apache.org/dtds/struts-2.3.dtd"><struts>    <constant name="struts.custom.i18n.resources" value="message"></constant>    <constant name="struts.locale" value="zh_CN"></constant>    <package name="struts2" extends="struts-default">        <action name="customConversion" class="com.test.action.CustomConversionAction">            <result name="input">custom.jsp</result>            <result>customResult.jsp</result>        </action>    </package></struts>
package com.test.Conversion;import java.util.Map;import org.apache.struts2.util.StrutsTypeConverter;import com.test.POJO.Point;public class PointStrutsConversion extends StrutsTypeConverter {    @Override    public Object convertFromString(Map arg0, String[] arg1, Class arg2) {        //将字符串转换成Point,即将HTTP请求参数转换为指定类型。        System.out.println("we are in PointStrutsConversion1");        String[] params = arg1[0].split(",");         Point point = new Point();        point.setX(Integer.parseInt(params[0]));//将数组类型转换为整型        point.setY(Integer.parseInt(params[1]));        return point;    }    @Override    public String convertToString(Map arg0, Object arg1) {        //将Point转换成字符串,即将指定类型转换为HTTP请求参数。        System.out.println("we are in PointStrutsConversion2");        Point point = (Point)arg1;        return "坐标值 (" + point.getX() + "," + point.getY() + ")";    }}
package com.test.action;import com.opensymphony.xwork2.ActionSupport;import com.test.POJO.Point;public class CustomConversionAction extends ActionSupport {    private static final long serialVersionUID = 1L;    private Point point;    public Point getPoint() {        return point;    }    public void setPoint(Point point) {        this.point = point;        System.out.println("1-1");    }    @Override    public String execute() throws Exception {        System.out.print(this.point.getX());        System.out.print(this.point.getY());        return SUCCESS;    }}
package com.test.POJO;public class Point {    private int x;    private int y;    public int getX() {        System.out.println("1-3");        return x;    }    public void setX(int x) {        this.x = x;        System.out.println("1-2");    }    public int getY() {        return y;    }    public void setY(int y) {        this.y = y;    }}
0 0