博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
struts2下 数据转换器
阅读量:4489 次
发布时间:2019-06-08

本文共 5300 字,大约阅读时间需要 17 分钟。

1,创建struts2 工程,工程结构图如下:

2,convertor.DateTimeConvertor.java代码如下:

View Code
package convertor;import java.text.DateFormat;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Map;import ognl.DefaultTypeConverter;public class DateTimeConvertor extends DefaultTypeConverter {    //日期格式化器,完成格式如2007-08-06,2007/08/06,07-08-06的定义    private DateFormat[] dateFormat={ new SimpleDateFormat("yyyy-MM-dd"),            new SimpleDateFormat("yyyy/MM/dd"),new SimpleDateFormat("yy-MM-dd"),};    //时间格式化器,完成格式如12:30:1100,12:30的定义    private DateFormat[] timeFormat={ new SimpleDateFormat("HH:mm:ssss"),            new SimpleDateFormat("HH:mm"),};        @SuppressWarnings("unchecked")    public Object convertValue(Map context,Object value,Class toType){        if(toType.equals(java.sql.Date.class)){//如果是java.sql.Date.class类型            String[]parameterValues=(String[])value;//获取原始字符串数据            for(DateFormat format:dateFormat)                try{                            //使用3种格式化器转换日期                    return new java.sql.Date(format.parse(parameterValues[0]).getTime());                }catch(ParseException e){}        }else if(toType.equals(java.sql.Time.class)){//如果是java.sql.Time.class类型            String[]parameterValues=(String[])value;//获取原始字符串数据            for(DateFormat format:timeFormat)                try{                            //使用2种格式化器转换日期                    return new java.sql.Time(format.parse(parameterValues[0]).getTime());                }catch(ParseException e){}        }else if(toType.equals(java.util.Date.class)){//如果是java..Date.class类型            String[]parameterValues=(String[])value;//获取原始字符串数据            for(DateFormat format:dateFormat)                try{                            //使用3种格式化器转换日期                    return format.parse(parameterValues[0]);                }catch(ParseException e){}        }else if(toType.equals(String.class)){    //如果时字符串            if(value instanceof java.sql.Date){            }else if(value instanceof java.sql.Time){            }else if(value instanceof java.util.Date){                return dateFormat[0].format((java.util.Date)value);            }                                    //将Date类型转换为String类型        }        return super.convertValue(context, value, toType);//否则调用父类方法                }}

3,创建struts2的配置文件xwork-conversion.properties,放在src路径下,具体代码如下:

View Code
java.sql.Date=convertor.DateTimeConvertorjava.sql.Time=convertor.DateTimeConvertorjava.util.Date=convertor.DateTimeConvertor

4,创建action.ConvertorAction.java代码如下:

View Code
package action;import java.sql.Date;import java.sql.Time;import com.opensymphony.xwork2.ActionSupport;public class ConvertorAction extends ActionSupport {    private Date sqlDate;    private Time sqlTime;    private java.util.Date utilDate;        public Date getSqlDate() {        return sqlDate;    }    public void setSqlDate(Date sqlDate) {        this.sqlDate = sqlDate;    }    public Time getSqlTime() {        return sqlTime;    }    public void setSqlTime(Time sqlTime) {        this.sqlTime = sqlTime;    }    public java.util.Date getUtilDate() {        return utilDate;    }    public void setUtilDate(java.util.Date utilDate) {        this.utilDate = utilDate;    }    /**     * @return     */    public String execute() {        // TODO Auto-generated method stub        return INPUT;    }    public String convert(){        return SUCCESS;    }}

5,创建struts.xml,

View Code
/convert.jsp
/convertSuccess.jsp

6,web.xml文件配置如下:

View Code
index.jsp
struts
org.apache.struts2.dispatcher.FilterDispatcher
struts.action.extension
action
struts
/*

7,用户界面包括convert.jsp  convertSuccess.jsp

convert.jsp如下:

View Code
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%@taglib uri="/struts-tags" prefix="struts" %><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%>              My JSP 'convert.jsp' starting page        

convertSuccess.jsp如下:

View Code
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%@taglib uri="/struts-tags" prefix="struts" %><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%>              My JSP 'convertSuccess.jsp' starting page        
java.sql.Date:
java.sql.Time:
java.util.Date:
重新转换

8,发布即可

转载于:https://www.cnblogs.com/lpshou/archive/2012/11/27/2791188.html

你可能感兴趣的文章
NPOI导出excel表格应用
查看>>
tensorflow从入门到放弃-0
查看>>
解锁scott用户
查看>>
多态的理解
查看>>
AspNet Core 发布到Linux系统和发布IIS 注意项
查看>>
Windows添加.NET Framework 3.0 NetFx3 失败 - 状态为:0x800f0950
查看>>
隐藏显示终端的光标(shell echo,linux c printf)
查看>>
SQL Server 存储过程
查看>>
JSP 标准标签库(JSTL)(JSP Standard Tag Library)
查看>>
导入项目遇到的问题: Some projects cannot be imported because they already exist in the workspace....
查看>>
华为:字符集合
查看>>
用Okhttp框架登录之后的Cookie设置到webView中(转)
查看>>
Java_Activiti5_菜鸟也来学Activiti5工作流_之入门简单例子(一)
查看>>
设计模式(一)工厂模式Factory(创建型)
查看>>
linux中安装软件的集中方法
查看>>
Express中间件,看这篇文章就够了(#^.^#)
查看>>
《构建之法》(五)
查看>>
创建django项目
查看>>
Linux Bash基本功能
查看>>
一则小脚本(工作中用)
查看>>