• struts2下 数据转换器


    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.DateTimeConvertor
    java.sql.Time=convertor.DateTimeConvertor
    java.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
    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE struts PUBLIC 
            "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" 
            "http://struts.apache.org/dtds/struts-2.0.dtd">
    <struts>
    
    
        <package name="main" extends="struts-default">
            <action name="convertor" class="action.ConvertorAction" converter="convertor.DateTimeConvertor">
                <result name="input">/convert.jsp</result>
                <result name="success">/convertSuccess.jsp</result>
            </action>
        </package>
    </struts>

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

    View Code
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.5" 
        xmlns="http://java.sun.com/xml/ns/javaee" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
        http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
      <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
      </welcome-file-list>
      
      <filter>
          <filter-name>struts</filter-name>
          <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
          <init-param>
              <param-name>struts.action.extension</param-name>
              <param-value>action</param-value>
          </init-param>
      </filter>
      <filter-mapping>
          <filter-name>struts</filter-name>
          <url-pattern>/*</url-pattern>
      </filter-mapping>
      
     
    </web-app>

    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+"/";
    %>
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <base href="<%=basePath%>">
        
        <title>My JSP 'convert.jsp' starting page</title>
        
        <meta http-equiv="pragma" content="no-cache">
        <meta http-equiv="cache-control" content="no-cache">
        <meta http-equiv="expires" content="0">    
        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
        <meta http-equiv="description" content="This is my page">
        <!--
        <link rel="stylesheet" type="text/css" href="styles.css">
        -->
    
      </head>
      
      <body>
              <struts:form action="convertor">
                   <struts:label label="转换器"></struts:label>
                   <struts:textfield name="sqlDate" label="SQL Date:"></struts:textfield>
                   <struts:textfield name="sqlTime" label="SQL Time:"></struts:textfield>
                   <struts:textfield name="utilDate" label="Util Date:"></struts:textfield>
                   <struts:submit value="提交" method="convert"></struts:submit>
              </struts:form>
      </body>
    </html>

    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+"/";
    %>
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <base href="<%=basePath%>">
        
        <title>My JSP 'convertSuccess.jsp' starting page</title>
        
        <meta http-equiv="pragma" content="no-cache">
        <meta http-equiv="cache-control" content="no-cache">
        <meta http-equiv="expires" content="0">    
        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
        <meta http-equiv="description" content="This is my page">
        <!--
        <link rel="stylesheet" type="text/css" href="styles.css">
        -->
    
      </head>
      
      <body>
              java.sql.Date:<struts:property value="sqlDate"/><br/>
              java.sql.Time:<struts:property value="sqlTime"/><br/>
              java.util.Date:<struts:property value="utilDate"/><br/><br/>
              <a href="convertor.action">重新转换</a>
      </body>
    </html>

    8,发布即可

  • 相关阅读:
    C++反汇编第一讲,认识构造函数,析构函数,以及成员函数
    cassert(assert.h)——1个
    1012 数字分类 (20 分)
    1011 A+B 和 C (15 分)
    1009 说反话 (20 分)
    1008 数组元素循环右移问题 (20 分)
    1006 换个格式输出整数 (15 分)
    1004 成绩排名 (20 分)
    1002 写出这个数 (20 分)
    1001 害死人不偿命的(3n+1)猜想 (15 分)
  • 原文地址:https://www.cnblogs.com/lpshou/p/2791188.html
Copyright © 2020-2023  润新知